Hardware Reference
In-Depth Information
Once the sensors are enabled, it's possible to read the sensor. To manually read the sensor,
you'll issue a read request to the characteristic you want to read and wait for the response
in the callback (see
“ATT operations” on page 26
and
“Reading Characteristics and
Descriptors” on page 70
for further details). The read can be triggered by an event, such
as the push of a button or a timer that periodically polls the sensor. In this case, a test
button created in the Options menu triggers events. That button's
onClick
method calls
the following function to generate a read request:
// Start the read request
private
void
testButton
()
{
BluetoothGatt
gatt
;
BluetoothGattCharacteristic
c
;
if
(!
mBleWrapper
.
isConnected
())
{
return
;
}
Log
.
d
(
LOGTAG
,
"testButton: Reading acc"
);
gatt
=
mBleWrapper
.
getGatt
();
c
=
gatt
.
getService
(
UUID_ACC_SERV
).
getCharacteristic
(
UUID_ACC_DATA
);
mBleWrapper
.
requestCharacteristicValue
(
c
);
mState
=
ACC_READ
;
}
// Get the read response inside this callback
@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
);
// decode current read operation
switch
(
mState
)
{
case
(
ACC_READ
):
Log
.
d
(
LOGTAG
,
"uiNewValueForCharacteristic: Accelerometer data:"
);
break
;
}
// dump data byte array
for
(
byte
b:
rawValue
)
{