I am currently trying to run the BLE application on Android. In particular, I am trying to get a BLE signal sent from an iOS device. Below is the code related 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.");
Log.w(tag, "Attempting to start service discovery:" +
mConnectedGatt.discoverServices());
bluetoothData.clear();
} else {
Log.w(tag, "GATT connection unsuccessful.");
restartSearch(gatt);
}
}
@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);
}
, Android . , discoverServices() onServiceDiscovered. , , mConnectedGatt.writeDescriptor() true. callback onCharacteristicChanged . 10 writeDescriptor, onConnectionStateChanged, , . 95% . 5% , , onCharacteristicChanged.
, ? , , Android, BLE iOS.