Hardware Reference
In-Depth Information
write a
0x01
to this characteristic to enable them. As mentioned before, you actually
send a write request (
Write characteristic value
in
“Writing Characteristics and De‐
scriptors” on page 71
, with the operation listed in
“ATT operations” on page 26
) to the
peer device and then wait for the callback.
This code enables the accelerometer by writing
0x01
to the accelerometer configuration
characteristic on the remote device:
BluetoothGattCharacteristic
c
;
c
=
gatt
.
getService
(
UUID_ACC_SERV
).
getCharacteristic
(
UUID_ACC_CONF
);
mBleWrapper
.
writeDataToCharacteristic
(
c
,
new
byte
[]
{
0x01
});
mState
=
ACC_ENABLE
;
// keep state context for callback
As before, you need to wait for the callback to know if the write operation was successful.
In cases where you have to wait for things to happen, it's best to use a separate thread
or a state machine. A separate thread would allow you to block the thread as you wait
for callback without holding up the rest of the system. The state machine allows you to
remain in the same thread and keep track of the current context for the operation being
performed.
For a write operation, Application Accelerator has two useful callbacks:
@Override
public
void
uiSuccessfulWrite
(
BluetoothGatt
gatt
,
BluetoothDevice
device
,
BluetoothGattService
service
,
BluetoothGattCharacteristic
ch
,
String
description
)
{
BluetoothGattCharacteristic
c
;
super
.
uiSuccessfulWrite
(
gatt
,
device
,
service
,
ch
,
description
);
switch
(
mState
)
{
case
ACC_ENABLE:
Log
.
d
(
LOGTAG
,
"uiSuccessfulWrite: Successfully enabled
accelerometer"
);
break
;
}
}
@Override
public
void
uiFailedWrite
(
BluetoothGatt
gatt
,
BluetoothDevice
device
,
BluetoothGattService
service
,
BluetoothGattCharacteristic
ch
,
String
description
)
{
super
.
uiFailedWrite
(
gatt
,
device
,
service
,
ch
,
description
);