I was trying to pair my android with a BLE device. The Problem is when I call for the pairing request activity, The dialog box appeared. But when I entered my password , it is not being paired or onActivityResult is not being called. So what to do for pairing successfully ?
private void startBluetoothPairing(BluetoothDevice device) {
String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
Intent intent = new Intent(ACTION_PAIRING_REQUEST);
String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
intent.putExtra(EXTRA_DEVICE, device);
String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
int PAIRING_VARIANT_PIN = 0;
intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) appContext).startActivityForResult(intent,1);
}
OnActivityResult is not being called.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("TAG","Bluetooth Device!!");
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
BluetoothDevice bluetoothDevice = data.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcel parcel = Parcel.obtain();
data.getParcelableExtra(BluetoothDevice.EXTRA_PAIRING_KEY).writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
bluetoothDevice.setPin(bytes);
bluetoothDevice.createBond();
}
}
}
Problem solved : Updated code :
Registered broadCasterReciever during application launching
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
appContext.getApplicationContext().registerReceiver(broadCastReceiver,intentFilter);
Implementation of broadcastReciever.
private String BLE_PIN= "000012";
private BroadcastReceiver broadCastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action))
{
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
bluetoothDevice.setPin(BLE_PIN.getBytes());
Log.e("TAG","Auto-entering pin: " + BLE_PIN);
}
}
};
And I called device.createBond() after discovering the device.