Android Question Bluetooth barcode scanner resets global variables when turned on/off

JimAvanti

Member
Licensed User
Longtime User
I am using a Bluetooth barcode scanner with my APP to scan serial number barcodes and display the information about the scanned code. The database it searches is loaded into arrays which are initialized in Sub Globals. The arrays are loaded from a file when the program starts in Sub Activity_Create(FirstTime As Boolean).

My problem is every time the barcode scanner is either turned on, or times out and turns itself off the program re-initializes the arrays. I tried loading the database in Activity_Create in the “If FirstTime” and I ended up with empty arrays. I later took it out of the “If FirstTime” and it re-loads the database from the file, but then I would lose any changes unless I remember to save them. I am trying to find a way where the Bluetooth scanner will not cause the arrays to reinitialize when turned on/off.
 

DonManfred

Expert
Licensed User
Longtime User
Use a KeyValueStore to easily storing the Values (and sure easy get them back).
Also consider using a SQLite Database.

The question is why your array gets cleared by the Scanner?
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
Don,

The barcode scanner is Bluetooth, but it installs as a Bluetooth keyboard. Every time it turns on/off it resets the program for some reason. Originally I used a ListView instead of arrays, but I ran into issues that were easier to figure out using arrays so I switched to arrays to get the program completed quicker. Maybe the ListView would have kept the loaded database without re-initializing like the arrays do.

I don’t know what you mean by KeyValueStore and I never used a SQLite Database. I could save the changes after making them so the saved database will always load back after a reset without losing anything, but I really don’t want to save any changes unless the Save button is pressed. I like being able to make changes to the data to test things without having to worry about overwriting the saved data.

Is it common to have a program restart when a Bluetooth keyboard is turned on/off? Has anyone heard of this before?


Use a KeyValueStore to easily storing the Values (and sure easy get them back).
Also consider using a SQLite Database.

The question is why your array gets cleared by the Scanner?
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
The program already uses Bluetooth COM ports to sync the database with the PC. Also, I only use b4A once in a great while to do what I need so I am not really too good at it. I am still using version 2.5.


Never had this issue. Maybe you want to switch to SPP (serial). Search for my example.
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
I just made a very simple program that loads an array of 10 items and allows you to press buttons to display each item one at a time in a label. It also has a textbox that allows you to type in a string to assign to the current item and display it. This was a test to see if turning the Bluetooth keyboard on/off would reset the program and I found that it does still reset.

When I run the program this is the log:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **

Every time I turn the Bluetooth keyboard on or off I get this:
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **


B4X:
Sub Process_Globals

End Sub

Sub Globals
    Dim TestArray(10) As String
    Dim CurrentItem As Int = 0
    Dim Button1 As Button
    Dim Button2 As Button
    Dim Label1 As Label
    Dim Text1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("TestArray")
    Button1.Text = "Prev"
    Button2.Text = "Next"
    TestArray = Array As String ("Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10")
    CurrentItem = 0
    Label1.Text = TestArray(CurrentItem)
End Sub

Sub Text1_EnterPressed   ' BARCODE SCANNER ENTERED TEXT
    TestArray(CurrentItem) = Text1.Text       
    Label1.Text = TestArray(CurrentItem)
    Text1.Text = ""
End Sub

Sub Button1_Click
    CurrentItem = CurrentItem - 1
    If CurrentItem < 0 Then CurrentItem = 9
    Label1.Text = TestArray(CurrentItem)
    Text1.RequestFocus()
End Sub

Sub Button2_Click
    CurrentItem = CurrentItem + 1
    If CurrentItem > 9 Then CurrentItem = 0
    Label1.Text = TestArray(CurrentItem)
    Text1.RequestFocus()
End Sub


Use a KeyValueStore to easily storing the Values (and sure easy get them back).
Also consider using a SQLite Database.

The question is why your array gets cleared by the Scanner?
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
I just found the solution: Using the manifest editor I needed to add the following line:

SetActivityAttribute(Main, android:configChanges,"keyboard|keyboardHidden|navigation")

Now the program doesn't reset when turning the keyboard/scanner on/off.
 
Upvote 0
Top