Hardware Reference
In-Depth Information
- ( void ) centralManager: ( CBCentralManager *) central
didDiscoverPeripheral: ( CBPeripheral *) peripheral
advertisementData: ( NSDictionary *) advertisementData
RSSI: ( NSNumber *) RSSI
{
NSString * localName = [ advertisementData objectForKey:
CBAdvertisementDataLocalNameKey ];
if ([ localName length ] > 0 )
{
NSLog ( @ "Found the battery level monitor: %@" , localName );
// found peripheral so stop scanning
[ self . centralManager stopScan ];
// return peripheral object
self . batteryLevelPeripheral = peripheral ;
peripheral . delegate = self ;
// connect peripheral to centralManager
[ self . centralManager connectPeripheral: peripheral options: nil ];
}
}
It is good practice to stop scanning (using [self.centralManager stopScan] ) after
the peripheral of interest is found, again because it preserves battery power. Of course,
if you need to find more than one peripheral, modify the logic in this example.
Looking Up Services Associated with a Remote Peripheral
After the peripherals are discovered and the peripheral object instantiated, you need to
create related service objects and associated characteristics:
- ( void ) centralManager: ( CBCentralManager *) central
didConnectPeripheral: ( CBPeripheral *) peripheral
{
// establish peripheral delegate
[ peripheral setDelegate: self ];
// discover available services
[ peripheral discoverServices: nil ];
// log results
self . connected = [ NSString stringWithFormat: @ "Connected: %@" ,
peripheral . state == CBPeripheralStateConnected ? @ "YES" : @ "NO" ];
NSLog ( @ "%@" , self . connected );
}
In this code, the centralManager calls didConnectPeripheral and instantiates it when
a connection has been established. The delegate is set for the peripheral and noted in
the log.
Search WWH ::




Custom Search