Android Question BlueTooth Device Selection

andyp

Member
Licensed User
Hi.

I am using AsyncStreamsText to receive data over BT.

If I manually name the BT device in my code, all works great.

But I would like to allow user selection of the BT device.

This is the code in Starter:

B4X:
Private Sub admin_DeviceFound (Name As String, MacAddress As String)
    
    Log($"Device found: ${Name}"$)
    
    Dim selectedname As String = "BT2"
    
    If Name = selectedname Then
        Log("Trying to connect...")
        admin.CancelDiscovery
        serial.Connect(MacAddress)
    End If
End Sub
Any thoughts?

Thank you
Andrew
 

emexes

Well-Known Member
Licensed User
1/ Display an initially-empty selection list of Bluetooth devices to select from
2/ Start Bluetooth scan

and then:

DeviceFound:
- add it to the now-no-longer-empty list (if it is plausibly one of your devices)

User clicks device on list:
- highlight device
- enable Ok/Select button

User clicks Ok/Select button:
- stop scan
- close list
- return name and address of selected device

User clicks Cancel button:
- stop scan
- close list
- return "cancelled"

You might want to add a timer to stop the search process after 10-30 seconds, which could reassign/reuse the Ok/Select button as a ScanAgain button.

Some (many?) device types can't be determined just from the name; you have to connect to them and retrieve some identifying/confirmatory attributes to make sure that device name 7736628 is actually your relay board and not some other type of device. You can gradually finetune your selection list by saving to file a list of the last x device addresses and their device type (which you might only find out when you try connect to it the first time).

I have a recollection that some Androids will continue the Bluetooth scan even whilst you connect to devices, but other Androids won't. Thus I would be inclined to do things the slow-but-certain way of collecting a list of Bluetooth addresses first (over 10-30 seconds) and then stop the scan and go through the list and connect to each (unknown) device to identify it.
 

emexes

Well-Known Member
Licensed User
Thus I would be inclined to do things the slow-but-certain way of collecting a list of Bluetooth addresses first (over 10-30 seconds) and then stop the scan and go through the list and connect to each (unknown) device to identify it.
Actually, I did things the slack way: list all Bluetooth device name+addresses (some don't have names), except for device (addresses) that I *know* from my "seen this before" list are *not* devices of the type I am connecting to.

Thus, the list includes devices that:
1/ are of the type I am connecting to (because they are on my "seen this before" list as such)
2/ *might* be of the type I am connecting to (because they are *not* on my "seen this before" list)

and when the user selects a device to connect to, it might be one of the maybe-it-is-or-maybe-it-isn't types, and when I go to connect to it, then I find out:
- yes, it's the type I am connecting to, and I add it to the list as "this address is a device of the right type", and all is good
- no, it's not the type I am connecting to, and I add it to the list as "this address is *not* a device of the right type" and fall back to showing the device select activity again

So what will happen is that sometimes the user will select a device that is not a relay board, and when they press the Ok/Select button, they'll be returned to the selection list to have another go, but the nice-try-no-cigar not-a-relay-board device is now not on the list.

A long press of the Cancel button clears the "seen this before" list, just in case somehow an address is accidentally blacklisted, but that problem has never actually occurred (yet). I'm pretty certain that if I had not implemented this reset/fix feature, then the problem would have occurred ;-)
 
Last edited:
Top