I did not work with tablets, but I wrote an application using SPP for Android phones. I found that in order for Bluetooth to be stable, I need to manually pair it with the device I want to communicate with. We used the code below to initiate communication from the application, and it should maintain the connection as if you had manually mated in the settings menu.
Here is the general stream: 1) Register BroadcastReceiver to listen to BluetoothDevice.ACTION_BOND_STATE_CHANGED
2) After detecting the device, you should have a BluetoothDevice object.
3) Use reflection to call the createBond method on the BluetoothDeviceObject device
3a) Wait until the communication state changes before opening the connectors.
BluetoothDevice device = {obtained from device discovery}; Method m = device.getClass().getMethod("createBond", (Class[])null); m.invoke(device, (Object[])null); int bondState = device.getBondState(); if (bondState == BluetoothDevice.BOND_NONE || bondState == BluetoothDevice.BOND_BONDING) { waitingForBonding = true;
4) Wait for the link state to change from BOND_BONDING to BOND_BONDED
Inside BroadcastReciever:
public void onReceive(Context context, Intent intent) { if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) { int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1); int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1); if (waitingForBonding) { if (prevBondState == BluetoothDevice.BOND_BONDING) {
5) Open the sockets and inform
You can also use the removeBond method via reflection to remove the device from the pairing list.
Hope this helps!
source share