Hardware Reference
In-Depth Information
reliable way to know when notifications have been successfully enabled on the peer
device is to get the
onDescriptorWrite()
callback after the CCCD has been modified
and the GATT server has acknowledged the write operation. This example adds the
onDescriptorWrite()
callback to this implementation of Application Accelerator in
the
BleWrapper class
, where it implements the
BluetoothGattCallback
code:
/* callbacks called for any action on particular Ble Device */
private
final
BluetoothGattCallback
mBleCallback
=
new
BluetoothGattCallback
()
{
...
// Added by Akiba
@Override
public
void
onDescriptorWrite
(
BluetoothGatt
gatt
,
BluetoothGattDescriptor
descriptor
,
int
status
)
{
String
deviceName
=
gatt
.
getDevice
().
getName
();
String
serviceName
=
BleNamesResolver
.
resolveServiceName
(
\
descriptor
.
getCharacteristic
().
getService
().
getUuid
().
\
toString
().
toLowerCase
(
Locale
.
getDefault
()));
String
charName
=
BleNamesResolver
.
resolveCharacteristicName
(
\
descriptor
.
getCharacteristic
().
getUuid
().
toString
().
\
toLowerCase
(
Locale
.
getDefault
()));
String
description
=
"Device: "
+
deviceName
+
" Service: "
\
+
serviceName
+
" Characteristic: "
+
charName
;
// we got response regarding our request to write new value to
// the characteristic, let's see if it failed or not
if
(
status
==
BluetoothGatt
.
GATT_SUCCESS
)
{
mUiCallback
.
uiSuccessfulWrite
(
mBluetoothGatt
,
mBluetoothDevice
,
mBluetoothSelectedService
,
descriptor
.
getCharacteristic
(),
description
);
}
else
{
mUiCallback
.
uiFailedWrite
(
mBluetoothGatt
,
mBluetoothDevice
,
mBluetoothSelectedService
,
descriptor
.
getCharacteristic
(),
description
+
" STATUS = "
+
status
);
}
};
...
}
The
onDescriptorWrite()
method will then call Application Accelerator's
uiSuccess
fulWrite()
method if the notification was written properly.
You can enable notifications for all of the sensor characteristics in the same way you
enabled the sensors themselves, using a state machine to sequentially process one after