I am currently trying to get a BLE app on the android to work. Specifically, I am trying to pickup a BLE signal sent from an iOS device. Below is the code relevant to my problem.
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.w(tag, "State: " + newState+" status: "+ status + " (0 is success)");
if (newState == BluetoothProfile.STATE_CONNECTED) {
String intentAction = "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
final Intent intent = new Intent(intentAction);
callbackActivity.sendBroadcast(intent);
Log.w(tag, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.w(tag, "Attempting to start service discovery:" +
mConnectedGatt.discoverServices());
// Clear data to prepare for new data reading
bluetoothData.clear();
} else {
Log.w(tag, "GATT connection unsuccessful.");
restartSearch(gatt);
}
}
/**
* Called when a service has been discovered in the connected device
*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.w(tag, "Services Discovered! "+status);
if(gatt.getService(SERVICE_UUID)!=null){
Log.w(tag, "Not null!");
BluetoothGattCharacteristic characteristic=gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTC_UUID);
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
Log.w(tag, "Writing to descriptor: "+mConnectedGatt.writeDescriptor(descriptor));
}
else{
Log.w(tag, "Services discovered unsuccessful");
restartSearch(gatt);
}
When I activate both devices, the android device always pickups the connection. The connection is always a success, and the call to discoverServices() always results in a callback to onServiceDiscovered. The characteristic I am searching for is found, and the call mConnectedGatt.writeDescriptor(descriptor) always returns true. However, my onCharacteristicChanged callback is never called. Instead, the program waits for about 10 seconds after the writeDescriptor call, and then I get a call to onConnectionStateChanged specifying that the connection has been lost. This happens 95% of the time. 5% of the time, everything works perfectly and I get the call to onCharacteristicChanged.
Are there any reasons for this timeout that keeps happening? As a side note, I believe this is a problem within my android code as I can perfectly send and receive this BLE signal between iOS devices.