Hardware Reference
In-Depth Information
Scanning for Remote Peripherals
To get started, you'll first need to allocate an instance of the CBCentralManager and start
the BLE scanning (see “Advertising and Scanning” on page 19 ) operation for the battery
level service:
// using the predefined BLE UUIDs for the battery level service
# define BATTERY_LEVEL_SERVICE_UUID 0x180f
# define BATTERY_DEVICE_INFO_SERVICE_UUID 0x180a
// build an array of services UUIDs of interest
NSArray * services = @ [[ CBUUID UUIDWithString: BATTERY_LEVEL_SERVICE_UUID ],
[ CBUUID UUIDWithString: BATTERY_DEVICE_INFO_SERVICE_UUID ]];
// instantiate and start Central Manager object
CBCentralManager * centralManager = [[ CBCentralManager alloc ]
initWithDelegate: self queue: nil ];
// scan for peripherals with services listed in services UUID array
[ centralManager scanForPeripheralsWithServices: services options: nil ];
self . centralManager = centralManager ;
First, you use Core Bluetooth framework to create a list ( NSarray ) of the service UUIDs
of interest, named services in this case. You will need to convert the UUID strings into
CBUUID objects corresponding to UUIDs using the UUIDWithString method.
CBUUID also converts the predefined 16-bit service and characteristic UUIDS into the
their fully formed 128-bit equivalents (for more information, see “UUIDs” on page
52 ). Next, the CBCentralManager object is instantiated and the list of UUID objects is
provided as the first parameter for its scanForPeripheralsWithServices method. In
this case, the app scans for just two services, the device info service ( UUID 16 =0x180A ) and
the battery level service ( UUID 16 =0x180f ). Only peripherals that include the required
service UUIDs in their advertising data (see Table 3-3 ) will be returned as found.
Note that if the scanForPeripheralsWithSevices method were given nil for the first
parameter instead of the list of CBUUIDs of interest ( services ), it would return all BLE
peripherals in range. This is generally not a good practice, unless you really want to
explore all the possible peripherals with services, because it works the BLE radio hard‐
ware more, which decreases battery life.
Connecting to Remote Peripherals
When a peripheral of interest is discovered in the scanning process, the centralManag
er invokes didDiscoverPeripheral , which is where you include the delegate method
connectPeripheral . This method returns associated instances of peripheral objects
that can be examined later using CBPeripheral methods:
Search WWH ::




Custom Search