Hardware Reference
In-Depth Information
{
// Get the battery level
[ self getBatteryLevelData: characteristic error: error ];
}
}
To this point, the code has created services and characteristics objects associated with
the remote peripheral. The next section uses those objects to supply data to the rest of
the application.
Methods for Reading and Decoding Characteristics
This next section of code retreives the notified battery level value from the characteristic
objects for use elsewhere in the app:
- ( void ) getBatteryLevelData: ( CBCharacteristic *) characteristic
error: ( NSError *) error
{
// Get the Battery Level
NSData * data = [ characteristic value ];
const uint8_t * reportData = [ data bytes ];
uint16_t level = 0 ;
if (( reportData [ 0 ] & 0x01 ) == 0 )
{
// Retrieve the level value for the Battery
level = reportData [ 1 ];
}
else
{
level = CFSwapInt16LittleToHost (*( uint16_t *)(& reportData [ 1 ]));
}
// Display the battery level value to the UI if no error occurred
if ( ( characteristic . value ) || ! error )
{
self . batteryLevel = level ;
self . batteryLevel . text = [ NSString stringWithFormat: @ "%i %" ,
batteryLevel ];
}
return ;
}
And this code retrieves the manufacturing data for use in the app:
- ( void ) getManufacturerName: ( CBCharacteristic *) characteristic
{
NSString * manufacturerName = [[ NSString alloc ]
initWithData: characteristic . value
encoding: NSUTF8StringEncoding ];
self . manufacturer = [ NSString stringWithFormat: @ "Manufacturer: %@" ,
Search WWH ::




Custom Search