Hardware Reference
In-Depth Information
processed gets silently dropped. This can be a source of frustration because it simply
looks like a device is not responding.
The correct sequence of operations is to send a request and wait for the appopriate
callback. For example, you would send a read request to the remote device to read a
particular characteristic. After the device responds, the
BleWrapper
will issue a callback
to
uiNewValueForCharacteristic
with the characteristic information. By doing so,
you are implementing the
Read chacteristic value
GATT feature explained in
“Reading
Characteristics and Descriptors” on page 70
.
This code requests a read of the accelerometer's configuration characteristic:
BluetoothGatt
gatt
;
BluetoothGattCharacteristic
c
;
gatt
=
mBleWrapper
.
getGatt
();
c
=
gatt
.
getService
(
UUID_ACC_SERV
).
getCharacteristic
(
UUID_ACC_CONF
);
mBleWrapper
.
requestCharacteristicValue
(
c
);
Once the the request is issued, the device will respond with the characteristic's data. In
this case, you're going to dump each byte of the characteristic's raw value to
logcat
:
@Override
public
void
uiNewValueForCharacteristic
(
BluetoothGatt
gatt
,
BluetoothDevice
device
,
BluetoothGattService
service
,
BluetoothGattCharacteristic
ch
,
String
strValue
,
int
intValue
,
byte
[]
rawValue
,
String
timestamp
)
{
super
.
uiNewValueForCharacteristic
(
gatt
,
device
,
service
,
ch
,
strValue
,
intValue
,
rawValue
,
timestamp
);
Log
.
d
(
LOGTAG
,
"uiNewValueForCharacteristic"
);
for
(
byte
b:
rawValue
)
{
Log
.
d
(
LOGTAG
,
"Val: "
+
b
);
}
}
It's important to remember that each read or write must be requested. There are func‐
tions in Android's BLE library to get and set a value for characteristics. These operate
only on the locally stored values and not on the remote device. In most cases, any
interaction with the remote device will require the use of callbacks.
Before you read any of the sensor's data, you first need to enable them. To do this, you
have to write a value to their configuration characteristic (those are proprietary char‐
acteristics to enable sensors,
not to be confused with CCCDs
). In most cases, you'd just