Android Question Bluetooth Discovery has stopped finding devices

Robert Grabowski

Member
Licensed User
Longtime User
The following extracted code worked as expected in November of 2014:
B4X:
Sub Process_Globals

    Dim BTAdmin As BluetoothAdmin
    Dim BTSerial1 As Serial
    Dim BTFoundDevices As List
    Type BTNameAndMac (Name As String, MAC As String)
    Dim BTConnectedDevice As BTNameAndMac
    Dim BTAStream As AsyncStreams
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)

    'Set up the Bluetooth reader
    If FirstTime Then
        BTAdmin.Initialize("BTAdmin")
        BTSerial1.Initialize("BTSerial1")
    End If
   
End Sub

B4X:
Sub btnBTConnect_Click
    BTFoundDevices.Initialize
    If BTAdmin.StartDiscovery = False Then
        ToastMessageShow("Error starting discovery process.", True)
    Else
        ProgressDialogShow("Searching for devices...")
    End If   
End Sub
Sub BTAdmin_DiscoveryFinished
    ProgressDialogHide
    If BTFoundDevices.Size = 0 Then
        ToastMessageShow("No device found.", True)
    Else
        Dim l As List
        l.Initialize
        For i = 0 To BTFoundDevices.Size - 1
            Dim nm As BTNameAndMac
            nm = BTFoundDevices.Get(i)
            l.Add(nm.Name)
        Next
        Dim res As Int
        res = InputList(l, "Choose device to connect.", -1)
        If res <> DialogResponse.CANCEL Then
            BTConnectedDevice = BTFoundDevices.Get(res)
            ProgressDialogShow("Trying to connect to: " & BTConnectedDevice.Name & " (" & BTConnectedDevice.Mac & ")")
            BTSerial1.Connect(BTConnectedDevice.Mac)
        End If
    End If
End Sub
Sub BTAdmin_DeviceFound (Name As String, MacAddress As String)
    Log(Name & ":" & MacAddress)
    Dim nm As BTNameAndMac
    nm.Name = Name
    nm.Mac = MacAddress
    BTFoundDevices.Add(nm)
    ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", BTFoundDevices.Size))
End Sub

Device: Nexus 7 (2013)
Android version: 6.0
Android security patch level: November 1, 2015
B4A Version: 5.50
Serial library version: 1.23

Since the code was originally written and tested, there have been multiple version updates to Android and B4A.

Current issue summary:
Between the time that BTAdmin.StartDiscovery starts and BTAdmin_DiscoveryFinished fires, no devices are found. BTAdmin_DeviceFound never fires. No error messages are logged. Blue Tooth is powered on, initialized, and the native Android Blue Tooth settings dialogs show that there are 3 devices that are discoverable. This behavior is consistent on 4 identically configured tablets.

How do I get the current serial library to discover Blue Tooth devices?
 

Robert Grabowski

Member
Licensed User
Longtime User
I added the recommended line to the manifest and now discoverable Bluetooth devices fire the BTAdmin_DeviceFound event as expected. Erel, thanks for pinpointing the cause of this issue and providing the solution so quickly. Issue resolved!
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Question. Since this is using a location permission now, What happens if you have location disabled on your device?
 
Upvote 0
Top