This B4A library allows an app to pair with a Bluetooth device without human intervention. The user need not have enter the pairing key at the system generated dialog, the app will provide the pairing key.
The reason I needed auto-pairing is because I have a Bluetooth network where new devices continously enter and exit the Bluetooth network. All the devices have different mac address but the same pin. If you are in similar situation, you might find this library useful.
DISCLAIMER:
- I wrote this simple library for my own use, so it doesn't quite conform to the proper documentation.
- I am not responsible for your security and privacy.
- I am not responsible for damages to your software and hardware.
CONTEXT:
1) Tested to work for Android phone and Android TV Box using USB bluetooth dongle.
2) Tested for Android API 14.
DESCRIPTION:
When an app attempts to connect to a Bluetooth device that has not been previously paired, the system generated dialog will normally appear prompting the user to enter the pairing key. At this point, the app calls an internal method in android.bluetooth.BluetoothDevice, supplying the pairing key as a parameter. If you go to Android>Setting>Bluetooth, you will notice the device is listed as "paired". The dialog may still remain on the screen but it seems to disappear by itself.
HOW TO USE:
1) Include BtAutoPair library into your B4A IDE.
2) Include BluetoothAdmin and BtSerial libraries and program them for your purpose.
3) Include Broadcast library and code it to handle PAIRING_REQUEST. See the sample codes below.
4) You might want to implement a List or Map to store the device name, mac and the corresponding pairing-key. This is to ensure the correct pairing-key is being sent to the correct device.
For example, (pseudo-codes)
if device="samsunk" then
pinBytes = Array as Byte {0x31, 0x32, 0x33, 0x34}
device="nokick" then
pinBytes = Array as Byte {0x39, 0x39, 0x39, 0x39}
end if
Caller
The reason I needed auto-pairing is because I have a Bluetooth network where new devices continously enter and exit the Bluetooth network. All the devices have different mac address but the same pin. If you are in similar situation, you might find this library useful.
DISCLAIMER:
- I wrote this simple library for my own use, so it doesn't quite conform to the proper documentation.
- I am not responsible for your security and privacy.
- I am not responsible for damages to your software and hardware.
CONTEXT:
1) Tested to work for Android phone and Android TV Box using USB bluetooth dongle.
2) Tested for Android API 14.
DESCRIPTION:
When an app attempts to connect to a Bluetooth device that has not been previously paired, the system generated dialog will normally appear prompting the user to enter the pairing key. At this point, the app calls an internal method in android.bluetooth.BluetoothDevice, supplying the pairing key as a parameter. If you go to Android>Setting>Bluetooth, you will notice the device is listed as "paired". The dialog may still remain on the screen but it seems to disappear by itself.
HOW TO USE:
1) Include BtAutoPair library into your B4A IDE.
2) Include BluetoothAdmin and BtSerial libraries and program them for your purpose.
3) Include Broadcast library and code it to handle PAIRING_REQUEST. See the sample codes below.
4) You might want to implement a List or Map to store the device name, mac and the corresponding pairing-key. This is to ensure the correct pairing-key is being sent to the correct device.
For example, (pseudo-codes)
if device="samsunk" then
pinBytes = Array as Byte {0x31, 0x32, 0x33, 0x34}
device="nokick" then
pinBytes = Array as Byte {0x39, 0x39, 0x39, 0x39}
end if
Caller
B4X:
Dim Broadcast As BroadCastReceiver
...
Broadcast.Initialize("BroadCastReceiver")
Broadcast.addAction("android.bluetooth.device.action.PAIRING_REQUEST")
Broadcast.SetPriority(2147483647)
Broadcast.registerReceiver("")
...
Sub BroadCastReceiver_OnReceive (Action As String, i As Object) 'Event handler
If (Action = "android.bluetooth.device.action.PAIRING_REQUEST") Then
Dim intnt As Intent = i
Dim autopair As BtAutoPair
'The pairing pin
Dim pinBytes() As Byte = Array as Byte {0x31, 0x32, 0x33, 0x34}
autopair.Start4(intnt, pinBytes)
End If
End Sub
Attachments
Last edited: