Android Question Bluetooth createbond and removebond

SteveTerrell

Active Member
Licensed User
Longtime User
Hi,

does anyone have any experience using these api's to programatically pair and unpair Bluetooth devices. Presumably they can be accessed with the reflection library or perhaps even better with inline java code?

The resulting effects paired/unpaired look easy to catch with the broadcast receiver.

Are there any tricky bits to watch out for?
For instance, does the createbond api pop up the pairing code window?
 

SteveTerrell

Active Member
Licensed User
Longtime User
For those that are interested, this code unpairs a device when its mac address is known. It works on my HC-06 module.
It has a lot of my debugging Log's in which will not be needed and the mac address will usually not be a literal string.
The "removeBond" method is accessible but not public according to my research on the web.

B4X:
Dim joa As JavaObject
    joa = joa.InitializeStatic("android.bluetooth.BluetoothAdapter").RunMethod("getDefaultAdapter", Null)

    Log(joa.RunMethod("getName", Null)) ' gets my BT name - just checking
   
Dim job As JavaObject
    job = joa.RunMethod("getRemoteDevice", Array As String ("98:D3:31:30:2C:04")) ' mac address of remote BT
    ' after this "job" has the object that is the remote device

    Log(job.RunMethod("getName", Null)&" is "&BondStateToString(job.RunMethod("getBondState", Null)))
    ' gets the friendly name of the remote BT as a check

    ' see if unpair call works
    If job.RunMethod("removeBond", Null) Then
        Log("Remove true")
    Else
        Log("Remove false")
    End If

If anyone wants to offer a more elegant version of this I would be only too pleased to see it.
 
Upvote 0
Top