I am using the broadcast receiver below to catch a communication request and link it without requesting a popup.
private static BroadcastReceiver pairingBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
Toast.makeText(context, "broadcast type "+type, Toast.LENGTH_LONG).show();
if (type == BluetoothDevice.PAIRING_VARIANT_PIN) {
if(devicePin != null) {
CommonStuff.Log("bond pin "+devicePin);
Toast.makeText(context, "broadcast bond pin "+devicePin, Toast.LENGTH_LONG).show();
byte[] pin = new byte[10];
try {
pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class)
.invoke(BluetoothDevice.class, devicePin);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
device.setPin(pin);
abortBroadcast();
}
}
} else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
if(state == BluetoothDevice.BOND_BONDING)
return;
if(state == BluetoothDevice.BOND_BONDED) {
Toast.makeText(context, "Bond success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Bond failed", Toast.LENGTH_LONG).show();
}
}
}
};
Suppose devicePin is initialized globally. This works for most phones. But in coolpad 3 note, I got the value BluetoothDevice.EXTRA_PAIRING_VARIANT as 1 . And when I try to bind this device using setPin () without considering the value of EXTRA_PAIRING_VARIANT , the failure fails.
Has anyone encountered the same problem? Please help me sort it out.