title: Android: Catching BLE Connection Fails/Disconnects?
body: <p>So I'm able to connect to a BLE device just fine under normal circumstances. What I want to do is handle abnormal circumstances, like when the connection to a device fails or an established connection is lost (maybe it got thrown off a cliff or hit by a bus)</p>
  
  <p>I'm using a CyPress BLE module to test this, and one of the tests I'm doing is to remove power from the module. However, <code>onConnectionStateChange</code> never gets called! All I ever see it respond to is successful connections. It'll spend hours trying to connect and never give up evidently. I would do a delayed cancellation of the connection attempt, but there is no way to cancel the connection attempt on a Bluetooth device (that I know of)! As far as I can tell it'll keep trying until the battery runs down.</p>
  
  <p>Here's what my <code>onConnectionStateChange</code> looks like right now, inside the Gatt Callback. Note that I'm trying to catch and log ANY kind of callback involving ANY kind of connection state change... which never gets called unless connection success. Note that this is code is not on the activity itself. It's in an object held by a singleton. (I want to maintain connection across multiple activities)</p>
  
  <pre class="lang-java prettyprint-override"><code>        @Override
          public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
              mGatt = gatt;
              Logy.CallPrint(LOGY_ENABLE, CLASSNAME, "Status: "+status+ " Newstate: "+newState);
              switch(status)
              {
                  case BluetoothGatt.GATT_SUCCESS:
                      mReconnectAttempts = MAX_ATTEMPTS;
  
                      if(newState == BluetoothGatt.STATE_CONNECTED)
                      {
                          DispatchEvent(Event.Type.BT_ON_CONNECT);
                          bIsConnected = true;
                          gatt.discoverServices();
                      } else if (newState == BluetoothGatt.STATE_DISCONNECTED)
                      {
                          DispatchEvent(Event.Type.BT_ON_DISCONNECT);
                          bIsConnected = false;
                      }
                      break;
                  default:
                      if(newState == BluetoothGatt.STATE_DISCONNECTED)
                      {
                          bIsConnected = false;
                          if(mReconnectAttempts &gt; 0)
                          { // if we have attempts left, auto attempt to reconnect
                              DispatchEvent(Event.Type.BT_RECONNECTING);
                              mReconnectAttempts--;
                              gatt.connect();
                              bIsConnected = false;
                          }
                          else
                          {
                              mReconnectAttempts = MAX_ATTEMPTS;
                              DispatchEvent(Event.Type.BT_ON_CONNECT_FAIL);
                              bIsConnected = false;
                          }
                      } else {
                          Logy.CallPrint(LOGY_ENABLE, CLASSNAME, "onConnectionStateChange: Failed?");
                      }
              }
  
              super.onConnectionStateChange(gatt, status, newState);
          }
  </code></pre>
  
  <p>Not being able to detect disconnects is an issue elsewhere in my code, like where I show a Progress Dialog indicating the app is connecting to a BLE device. Well, that dialog never goes away because the event "On Connect Fail" never gets thrown.</p>