title: BLE Device Bonding Remove Automatically in Android
body: <p>We are doing below process to do pair with BLE Device.</p>
  <p>Connect() + discoverServices() + Pairing(Bonding) .</p>
  <p>Sometimes Android OS unpaired our BT device in a weird way, that is:</p>
  <ul>
  <li>without sending broadcast notification that bonding state has changed</li>
  <li>even system Bluetooth settings app thinks that device is still paired</li>
  <li>only bt restart (turning off and on via settings app) refreshes state and shows that device is not paired any longer</li>
  </ul>
  <p><strong>When Device is Successfully Paired the ACTION_BOND_STATE is change as below.</strong></p>
  <p>[6:19:28 PM] Himen Patel: 04-09 18:18:27.325: D/BluetoothGatt(8380): onCharacteristicWrite() - Device=C2:69:E9:57:93:A4 UUID=860b2c07-e3c5-11e2-a28f-0800200c9a66 Status=5
  04-09 18:18:27.365: E/millisUntilFinished(8380): millisUntilFinished = 15
  04-09 18:18:28.105: E/BelwithDeviceActor(8380): <strong>Bond state changed for: C2:69:E9:57:93:A4 new state: 11 previous: 10</strong></p>
  <p>04-09 18:18:28.105: E/millisUntilFinished(8380): millisUntilFinished = 20
  04-09 18:18:29.135: E/millisUntilFinished(8380): millisUntilFinished = 18
  04-09 18:18:30.135: E/millisUntilFinished(8380): millisUntilFinished = 17
  04-09 18:18:31.145: E/millisUntilFinished(8380): millisUntilFinished = 16
  04-09 18:18:32.145: E/millisUntilFinished(8380): millisUntilFinished = 15</p>
  <p>04-09 18:18:33.105: D/BluetoothGatt(8380): onCharacteristicWrite() - Device=C2:69:E9:57:93:A4 UUID=032a0000-0000-0000-0000-000000000000 Status=137
  04-09 18:18:33.115: E/BelwithDeviceActor(8380): <strong>Bond state changed for: C2:69:E9:57:93:A4 new state: 12 previous: 11</strong></p>
  <p>04-09 18:18:33.115: I/System.out(8380): unregisterReceiver true</p>
  <p>Now when Pairing is removed by OS in weird way the ACTION_BOND_STATE is change as below.
  .
  .
  .
  .
  <strong>Bond state changed for: C2:69:E9:57:93:A4 new state: 10.</strong></p>
  <p>we also get  immediate event of act=android.bluetooth.device.action.ACL_DISCONNECTED flg=0x4000010  in our APP.</p>
  <p>what's important here, at this point we just lost pairing with the device and protected characteristics don't work for us any longer.
  if we restart bt using system settings app or BluetoothAdapter::disable() and enable() we can see that we are not paired with the device.</p>
  <p>what's funny, without the bt restart, system settings app still thinks and shows that we are paired with the device.</p>
  <p>tested with nexus 4 running 4.4.2, nexus 5 running 4.4.2 and even Samsung galaxy s4 running 4.3.</p>
  <p>our expectation is that:</p>
  <ul>
  <li>in case of unpairing there should be system broadcast</li>
  <li>system preferences app should show current paring status even without bt restart</li>
  </ul>
  <p>We have also Observed and get the sniffed data in which we found that our encryption is set to 0x000000 when our bonding is removed by OS in weird way.</p>