A look at the CBCentralManager class of iOS

The CBCentralManager class of the CoreBluetooth is as the name says the central of the Bluetooth Smart (BLE) API of iOS and Mac OS X. It is a Hardware Abstraction Layer (HAL) for the Bluetooth Smart hardware, and it also feedbacks to the users program when different events relating to Bluetooth Smart is triggered.

CBCentralManager needs to have a delegate, the delegate could either be a class which you write to handle the events directly, or it could be your viewcontroller directly. The most clean solution would be to implement it in a new class.

 

CBCentralManager has a delegate protocol named “CBCentralManagerDelegate” it has the following delegate functions :

Examples of their usage is in the next sections.

Initializing :

Initializing the CBCentralManager class is pretty straight forward :

Firstly you need to set up the class receiving the events from the CBCentralManager :

You have now created a class that is able to receive events from the CBCentralManager.  Next you will need to instantiate the class and set the delegate.

When these statements are runned, they will be followed by CBCentralManager calling the centralManagerDidUpdateState delegate function being called.

Here is an implementation of the delegate :

Basically the centralManagerDidUpdateState delegate function is used to communicate the status of the BLE hardware on the current platform. The code above stores cBReady = true only if the BLE hardware is on and ready.

Now we have the BLE HW initialized and ready, it is time for one of two things :

1) Scan for peripherals
2) Connect to a previously known peripheral

Scanning can be done this way :

The scanForPeripheralWithServices function has two parameters which both can be set to nil. When they are set to nil, all peripherals discovered will generate the didDiscoverPeripheral event.

The Services parameter can be used to only find peripherals with certain services, it is done in the following way by using CBUUID’s :

Here we only scan for peripherals that have a service with UUID 0xFFE0.

We are now scanning, every time a peripheral with this service is found we will get a didDiscoverPeripheral delegate call :

The didDiscoverPeripheral is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral. It also includes a dictionary with the advertisement data included in the advertisement packets of the peripheral. RSSI (Received Signal Strength Indicator) value of the received advertisement packet. In this example we only print the data and do nothing else. One could use this method to continue in the application flow by connecting to the device found, or make a list of possible peripherals for the user to select from etc.

So now we know of a peripheral. What to do with it next ? In some devices this is probably the only thing you would have to do, broadcast devices would function in this way. Since they send all of their data in the advertisement data we never have to connect to them. I will write an example or tutorial later involving a broadcast peripheral.

If we want to continue on, we need to connect to the device to retrieve or send data to/from the device. This is done by calling :

The variable called peripheral in the above code listing has to be set to a value of a peripheral found with the scanForPeripheralsWithServices function call above.

Calling connectPeripheral will start a connection process between the iOS device and the BLE peripheral, once this processed is finished, one of two delegate functions will be called :

If the connection process completes successfully the didConnectPeripheral delegate will be called, or else the didFailToConnectPeripheral delegate will be called.

If successfull connection is obtained, the isConnected property of the CBPeripheral returned is set to true. To disconnect you should call :

This call takes some time to complete. When it completes :

Is called and the peripheral now should return to it’s previous idle state.

The last two delegates is to read CBCentralManagers table over devices :

These delegates are called when :

are called. These two lists do together contain all the information on all known devices, both discovered and not connected and discovered and connected.

This was a short intro in using the CBCentralManager class in iOS. In Mac OS X the functions are pretty much the same. The framework is called IOBluetooth on Mac OS X.

7 Responses to A look at the CBCentralManager class of iOS

  1. Pingback: Added an introduction to the CBCentralManager class in iOS | Bluetooth Smart Log (BLE)

  2. Kyle Bennett says:

    Awesome post! However, when I retrieve a peripheral and look at the entire object, the RSSI value shows and the services array shows I’m not sure what I’m doing wrong. Any ideas?

    Thanks!

  3. diaplo says:

    Hello~
    I want to ask the method of [didDisconnectPeripheral:]
    if I turn the BLE device’s power off,will iPhone call this method?
    another question is, after I call [cancelPeripheralConnection:] method ,
    will iPhone call the [didDisconnectPeripheral:] method too?

    THX!

  4. Jelari says:

    Nice Article, Really usefull. BTW, do u have a code snippet of sending/receiving data to/from connected BLE Device?

  5. Himanshu Lanjewar says:

    I have gone through your tutorial. I want to implement CoreBluetooth framework for my bluetooth printing application. But when i m going to use CBCentralManagerDelegate delegate from CoreBluetooth framework. it say “CoreBluetooth BLE hardware is unsupported on this platform” in my device. My device has iOS 6. then why i got this error?

  6. Brent says:

    Thanks for the a nice overview, which is presented in a logical, easily digestible manner.
    You mentioned that you would write an example or tutorial later involving a broadcast peripheral, but I can’t seem to find anything like that on your site. I’m very interested in seeing such an example.

  7. snehal says:

    retrievePeripherals is not instance method that cBCM can call. Actually method is [cBCM retrievePeripherals: someArrayOfUUID]. Please help me to understand how to get information of previously connected bluetooth devices. I know some private framework , but afraid to use at it may violate apple’s approval policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">