Hardware Reference
In-Depth Information
characteristic (again, because the manufacturing data is static, it makes sense to use
readValueForCharacteristic:
for that):
-
(
void
)
peripheral:
(
CBPeripheral
*)
peripheral
didDiscoverCharacteristicsForService:
(
CBService
*)
service
error:
(
NSError
*)
error
{
// Retrieve Service for the Voltage Level
if
([
service
.
UUID
isEqual:
[
CBUUID
UUIDWithString:
BATTERY_LEVEL_SERVICE_UUID
]])
{
for
(
CBCharacteristic
*
aChar
in
service
.
characteristics
)
{
// Request level notifications
if
([
aChar
.
UUID
isEqual:
[
CBUUID
UUIDWithString:
BATTERY_LEVEL_MEASUREMENT_CHARACTERISTIC_UUID
]])
{
[
self
.
batteryPeripheral
setNotifyValue:
YES
forCharacteristic:
aChar
];
NSLog
(
@
"Found battery level measurement characteristic"
);
}
}
}
if
([
service
.
UUID
isEqual:
[
CBUUID
UUIDWithString:
BATTERY_LEVEL_DEVICE_INFO_SERVICE_UUID
]])
{
for
(
CBCharacteristic
*
aChar
in
service
.
characteristics
)
{
if
([
aChar
.
UUID
isEqual:
[
CBUUID
UUIDWithString:
BATTERY_LEVEL_MANUFACTURER_NAME_CHARACTERISTIC_UUID
]])
{
[
self
.
batteryPeripheral
readValueForCharacteristic:
aChar
];
NSLog
(
@
"Found a device manufacturer name characteristic"
);
}
}
}
}
Now, the
didUpdateValueForCharacteristic
method will be invoked whenever the
peripheral has sent a notification, allowing the app to read the value of the battery level
characteristic only when it changes:
-
(
void
)
peripheral:
(
CBPeripheral
*)
peripheral
didUpdateValueForCharacteristic:
(
CBCharacteristic
*)
characteristic
error:
(
NSError
*)
error
{
// Updated value for battery level measurement received
if
([
characteristic
.
UUID
isEqual:
[
CBUUID
UUIDWithString:
BATTERY_LEVEL_MEASUREMENT_CHARACTERISTIC_UUID
]])