@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) { BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR); byte[] pinBytes; Log.d(TAG, "------------" + intent); switch (type) { case BluetoothDevice.PAIRING_VARIANT_PIN: pinBytes = BluetoothDevice.convertPinToBytes(mDefaultCode); bluetoothDevice.setPin(pinBytes); break; case BluetoothDevice.PAIRING_VARIANT_PASSKEY: int passkey = Integer.parseInt(mDefaultCode); bluetoothDevice.setPasskey(passkey); break; case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION: case BluetoothDevice.PAIRING_VARIANT_CONSENT: { bluetoothDevice.setPairingConfirmation(true); pinBytes = BluetoothDevice.convertPinToBytes(mDefaultCode); bluetoothDevice.setPin(pinBytes); } break; case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY: case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN: int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR); if (type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) { bluetoothDevice.setPairingConfirmation(true); } else { String pinCode = String.format("%04d", pairingKey); pinBytes = BluetoothDevice.convertPinToBytes(pinCode); bluetoothDevice.setPin(pinBytes); } break; case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT: bluetoothDevice.setRemoteOutOfBandData(); break; } } else if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); if (state == BluetoothAdapter.STATE_ON) { mAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 0); mAdapter.setDiscoverableTimeout(0); } } else if (intent.getAction().equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() == BluetoothDevice.BOND_BONDED) { mServerThread.cancel(); mServerThread.start(); } } }