Android Question Problem pairing with Samsung Galaxy Tab A / Android 9

doncx

Active Member
Licensed User
Longtime User
This is a second attempt to articulate a problem I'm having pairing a Bluetooth device to a Samsung tablet. This code works on other tablets and devices, including my phone, which is also running Android 9.

The problem is that on the Samsung Galaxy Tab A the process errors with a timeout before the pairing code can be inputted. The code is attached. Below are two logs: The first is a successful pairing on an older Samsung tablet. The second is the same code running on the problem tablet in the subject line.

I'd appreciate any help you can offer. I'm out of ideas.

B4X:
#
#Region  Activity Attributes 
    #FullScreen: True
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim admin As BluetoothAdmin
    Dim serial1 As Serial
    Dim foundDevices As Map
    Type NameAndMac (Name As String, Mac As String)
    Dim connectedDevice As NameAndMac
End Sub

Sub Globals
    Dim btnSearchForDevices As Button
    Dim btnDeleteDevices As Button
    Dim btnDone As Button
    Dim lblCurrentDevice As Label
    Dim lstBT As List
End Sub

Sub Activity_Create(FirstTime As Boolean)

    If FirstTime Then
        admin.Initialize("admin")
        serial1.Initialize("serial1")
    End If

    Activity.LoadLayout("configbt")

End Sub

Sub Activity_Resume
    
    ShowDevices

    btnSearchForDevices.Enabled = False

    If admin.IsEnabled = False Then
        If admin.Enable = False Then
            ToastMessageShow("Error enabling Bluetooth adapter.", True)
        Else
            ToastMessageShow("Enabling Bluetooth adapter...", False)
        End If
    Else
        Admin_StateChanged(admin.STATE_ON, 0)
    End If

End Sub

Sub Admin_StateChanged (NewState As Int, OldState As Int)
    btnSearchForDevices.Enabled = (NewState = admin.STATE_ON)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
        serial1.Disconnect
    End If
End Sub

Sub btnSearchForDevices_Click

    foundDevices.Initialize

    If admin.StartDiscovery    = False Then
        ToastMessageShow("Error starting discovery process.", True)
    Else
        ProgressDialogShow("Searching for devices...")
    End If

End Sub

Sub Admin_DeviceFound (Name As String, MacAddress As String)

    Log("device found " & Name & " : " & MacAddress)

    Dim mapValue As NameAndMac
    mapValue.Name = Name
    mapValue.Mac = MacAddress
    foundDevices.Put(Name, mapValue)

End Sub

Sub Admin_DiscoveryFinished

    ProgressDialogHide

    If foundDevices.Size = 0 Then
        ToastMessageShow("No device found.", True)
    Else
    
        Private lstFound As List
        lstFound.Initialize
        
        For Each nm As NameAndMac In foundDevices.Values
            lstFound.Add(nm.name)
        Next

        InputListAsync(lstFound, "Choose device to connect", -1, True)
        Wait For InputList_Result (res As Int)

        If res <> DialogResponse.CANCEL Then
            Log("selected device name = " & lstFound.Get(res))
            connectedDevice = foundDevices.GetValueAt(res)
            ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
            serial1.Connect(connectedDevice.mac)
        End If

    End If

End Sub

Sub Serial1_Connected (Success As Boolean)

    ProgressDialogHide

    If Success = False Then
        Log(LastException.Message)
        ToastMessageShow("Error connecting: " & LastException.Message, True)
    Else
        lstBT.Add( connectedDevice.Mac )
        File.WriteList( File.DirDefaultExternal, "BT-Vemco-FieldReader.txt", lstBT )
        Log("Stored to external file: " & connectedDevice.Mac)
        ShowDevices
        ToastMessageShow( "Bluetooth Configuration Saved", False )
        serial1.Disconnect
    End If

End Sub
Sub btnDeleteDevices_Click

    If lstBT.Size > 0 Then
        For i = 0 To lstBT.Size - 1
            UnpairDevice(lstBT.Get(i))
        Next
    End If

    If File.Exists( File.DirDefaultExternal, "BT-Vemco-FieldReader.txt" ) Then
        File.Delete(File.DirDefaultExternal, "BT-Vemco-FieldReader.txt")
        lstBT.Initialize
    End If

    ShowDevices

    ToastMessageShow( "Bluetooth Devices Removed", False )

End Sub

Sub UnpairDevice(Address As String)

    Dim adapter As JavaObject
    adapter = adapter.InitializeStatic("android.bluetooth.BluetoothAdapter").RunMethod("getDefaultAdapter", Null)
    Dim device As JavaObject = adapter.RunMethod("getRemoteDevice", Array(Address))
    Log(device.RunMethod("removeBond", Null))

End Sub

Sub btnDone_Click

    serial1.Disconnect
    Activity.Finish

End Sub

Sub ShowDevices


    If File.Exists( File.DirDefaultExternal, "BT-Vemco-FieldReader.txt" ) Then
                    
        Private DeviceList = "" As String
        Private FoundThisOne = False As Boolean

        serial1.Initialize("serial1")
        Private PairedDevices As Map
        PairedDevices = serial1.GetPairedDevices

        lstBT = File.ReadList(File.DirDefaultExternal, "BT-Vemco-FieldReader.txt")

        For i = 0 To lstBT.Size - 1
            For pd = 0 To PairedDevices.Size - 1
                If PairedDevices.GetValueAt(pd) = lstBT.Get(i) Then
                    DeviceList = DeviceList & PairedDevices.GetKeyAt(pd) & " (" & lstBT.Get(i) & ")" & CRLF
                    FoundThisOne = True
                End If
            Next
            If FoundThisOne = False Then
                DeviceList = DeviceList & "Unpaired device at " & lstBT.Get(i) & CRLF
            End If
        Next

        lblCurrentDevice.Text = "Current Device(s): " & CRLF & CRLF & DeviceList

    Else
        
        lstBT.Initialize
        lblCurrentDevice.Text = "No Devices"
        
    End If

End Sub

Log from Samsung Galaxy Tab S SM-T800 running Android 6.0.1
Pairing successful
--------------------------------------------------------
device found : 5F:20:89:85:DA:2B
device found Se3a4cf18020f8a9eC : 84:EE:03:49:1F:59
device found : 44:31:69:4C:6B:58
device found 0503:00061BDF:Vemco Field Reader : 10:00:E8:C2:E2:A4
selected device name = 0503:00061BDF:Vemco Field Reader
** Activity (configbt) Pause, UserClosed = false **
** Activity (configbt) Resume **
Stored to external file: 10:00:E8:C2:E2:A4
** Activity (configbt) Pause, UserClosed = true **
** Activity (main) Resume **



Log from Samsung Galaxy Tab A SM-T510 running Android 9
Pairing NOT successful - failed before pairing code could be entered
-----------------------------------------------------------
device found : 44:31:69:4C:6B:58
device found Se3a4cf18020f8a9eC : 84:EE:03:49:1F:59
device found 0503:00061BDF:Vemco Field Reader : 10:00:E8:C2:E2:A4
device found 0503:00061BDF:Vemco Field Reader : 10:00:E8:C2:E2:A4
device found 0503:00061BDF:Vemco Field Reader : 10:00:E8:C2:E2:A4
device found 0503:00061BDF:Vemco Field Reader : 10:00:E8:C2:E2:A4
device found 0503:00061BDF:Vemco Field Reader : 10:00:E8:C2:E2:A4
device found : 6C:DA:AF:9B:8B:AE
device found 0503:00061BDF:Vemco Field Reader : 10:00:E8:C2:E2:A4
device found TVBluetooth : C4:73:1E:5F:DF:F6
device found 0503:00061BDF:Vemco Field Reader : 10:00:E8:C2:E2:A4
device found TVBluetooth : C4:73:1E:5F:DF:F6
selected device name = 0503:00061BDF:Vemco Field Reader
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
** Activity (configbt) Pause, UserClosed = true **
** Activity (main) Resume **
 
Last edited:

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have a Samsung Galaxy Tab A with Android 9, and by chance I have an older Samsung tablet (Nexus 10) running Android 5.1.1. I also have a Bluetooth app that I wrote a couple of years ago - not sure of its current condition.

I copied your code into B4A and it looks okay (of course) but I can't do much more because I don't have the Layout file. If you post your complete project I will run it on my Tab A and let you know if I get the same failure.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I am not sure if this is good news or bad news. I have run your code on both the Tab A and the Nexus 10 with essentially identical results. I had a couple of false starts - on the first run of the app no device name is shown in the "devices found" list - instead there is a single blank line that I did not notice. Clicking on the blank line does in fact start the pairing process with the test device, as I discovered later, but re-running "Search for devices" also shows the device name correctly. Anyway, my test device now shows in the "Current Device(s):" list on the Tab A.
 
Upvote 0

doncx

Active Member
Licensed User
Longtime User
Brian -

Thank you. I appreciate your efforts.

I've had that nameless listing behavior ever since the early versions of this code were written over five years ago. I think it's something in the lower level functions. Upon re-scan, the missing name(s) usually show up.

But as for the timeout, there must be something about the SM-T510 tablet...

The organization I'm consulting for has purchased a number of them, so it's unfortunate.

Still open for any ideas folks may have.

- Don
 
Upvote 0

doncx

Active Member
Licensed User
Longtime User
This problem persists. Does anyone have more feedback? I've uploaded code, Brian says it works, but it doesn't on the Samsung SM-T510 tablet.

Is there possibly a tablet setting that needs to be changed? I can't see one.

Is it possible that the tablet type is flawed? We see the problem on all tablets of this model.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Brian says it works, but it doesn't on the Samsung SM-T510 tablet.

Yes - it does work, and it does work on a Samsung SM-T510; that is what I am using. This has been updated to Android 10 since you first posted this thread, and I have just reinstalled and run your sample test package and it still works, but the process is inconsistent. Perhaps by saying your software "works" I gave the wrong impression. Yes - it can pair with a Bluetooth device, but I am not sure that an average customer would be successful.

My first attempt this afternoon was flawless - the empty line in the "Devices Found" list did not occur, and I did not even have to acknowledge the pairing code. After that, though, I ran into a string of different situations. If I unpaired the device then a second attempt to pair was very problematic - the device would be recognised, the pairing code would, apparently, be accepted but the pairing would usually not complete. In that situation reinstalling the app was the quickest path to success - starting from a clean install always worked.

Here is a typical Log sequence, starting from a system restart on the SM-T510 and a reinstall of the test app. The first pairing attempt succeeds, as expected, but after unpairing a second attempt fails.
Logger connected to: samsung SM-T510
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
1
2
** Activity (main) Pause, UserClosed = false **
Paused
** Activity (configbt) Create, isFirst = true **
** Activity (configbt) Resume **
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
selected device name = NXT
Stored to external file: 00:16:53:12:ED:4D <=== SUCCESSFUL PAIRING
true <=== "UNPAIR DEVICES" GOES HERE
device found NXT : 00:16:53:12:ED:4D
device found NXT : 00:16:53:12:ED:4D
selected device name = NXT <=== SOMETHING CHANGES HERE IN THE PROGRAM FLOW
** Activity (configbt) Pause, UserClosed = false **
** Activity (configbt) Resume **
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
I hope that this is helpful.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have been thinking about this problem so I have tried running your app against another device - a Google Pixel 'phone also with Android 10. I ran an identical test to the one that I ran yesterday with the Samsung tablet. Here is the log ...
Logger connected to: Google Pixel
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Paused
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
1
2
** Activity (main) Pause, UserClosed = false **
Paused
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
3
4
** Activity (main) Pause, UserClosed = false **
Paused
** Activity (configbt) Create, isFirst = true **
** Activity (configbt) Resume **
device found : 00:16:53:12:ED:4D
selected device name =
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
** Activity (configbt) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Paused
** Activity (configbt) Create, isFirst = false **
** Activity (configbt) Resume **
device found NXT : 00:16:53:12:ED:4D
selected device name = NXT
** Activity (configbt) Pause, UserClosed = false **
** Activity (configbt) Resume **
Stored to external file: 00:16:53:12:ED:4D
** Activity (configbt) Pause, UserClosed = false **
This was the first trial on this device, so the permissions sequence was run. After that the pairing procedure was not straightforward; the first pass produced an (apparently) empty device name list and a timeout failure, but an immediate retry was successful. One noticeable difference in the log is that the "device found" entry only occurs once, whereas with the SM-T510 it was repeated several times.

As another test, I reinstalled the test app on my old Samsung Nexus 10, where I had run tests before. Again, here is the log ...
Logger connected to: samsung Nexus 10
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
1
2
** Activity (main) Pause, UserClosed = false **
Paused
** Activity (configbt) Create, isFirst = true **
** Activity (configbt) Resume **
device found NXT : 00:16:53:12:ED:4D
selected device name = NXT
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
device found NXT : 00:16:53:12:ED:4D
selected device name = NXT
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
device found NXT : 00:16:53:12:ED:4D
selected device name = NXT
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
As you can see, although the device was found the pairing failed to complete.

I am not knowledgeable on Bluetooth because the only Bluetooth app that I have written has always run without any hitch. However I cannot help but be struck by the variability in the pairing process demonstrated by these three logs from three different devices.
 
Upvote 0

doncx

Active Member
Licensed User
Longtime User
Brian -

Many thanks for your time and experiments. You're right that the results are inconsistent.
The code that I'm using is right from the Bluetooth examples posted on this website.
That leaves me uncertain about what to do.
However I'm grateful for your efforts thank you very much.
 
Upvote 0
Top