BLE Tutorial 2

If you don’t want to go through all the steps, there is a zip file with all files including the xcode project BLE-Tutorial 2

Tutorial 2 covers connecting to peripheral and writing,reading and enabling a notification from a characteristic on that peripheral.

The tutorial includes the source code which will be used for my next article about the CBPeripheral class.

This code is based on the code from Tutorial 1.

So, lets get started :

The first thing we will need to do is add a new class, this is done in the same way as Tutorial 1.

The new class should be named : CBCPCtrl

This class will be our new class that interfaces the CBPeripheral class in CoreBluetooth.

 

 
BTSAppDelegate.h :

BTSAppDelegate.m :

BTSViewController.h :

BTSViewController.m :

CBCMCtrl.h :

CBCMCtrl.m :

CBCPCtrl.h :

CBCPCtrl.m :

I will soon write my article on CBPeripheral. In the mean time feel free to download the entire project here BLE-Tutorial 2

PS The new project file includes support for iPad 3 GUI :)

Ole A.

Ole

22 Responses to BLE Tutorial 2

  1. Pingback: Added iOS BLE Tutorial 2 | Bluetooth Smart Log (BLE)

  2. Stewart says:

    Hi,

    I’ve had a ‘very’ quick play… A BLE dongle in default factory mode will drop connection at its end when you enter background as the session is stopped from an actual connection, rather than awareness of a device. So it won’t automatically pick it up again. And, the important bit, that I hinted at yesterday is you need to place an entry in UIBackgroundModes of ‘bluetooth-central’ and that’s it… If I commented out your background code and placed the entry in the plist, it keeps everything alive when entering background or locked. Oh, the BLE was in broadcast mode.

  3. konninee says:

    Hi,
    thanks for the Link, Between do u have any sample code from CBCentralManager to initiate secure connection. There is no document in CBCentralManager regarding authentication/authorization. Kindly help .

  4. oshri says:

    Great post! well detailed and contract

    I was wondering if it is possible to use the mentioned below method in order to find (and get the RSSI) of other iOS and Android devices OR will it find only non-mobile-phone Bluetooth devices (like headset, hear monitor…)?

    “centralManager:didDiscoverPeripheral:advertisementData:RSSI:”

  5. Lowell Bahner says:

    Thank you so much for the great tutorials on ble.

    I used your code on an iPhone4S (IOS6), and it finds the Bluegiga DKBLE112 Development board with the default Thermometer code and identifies its services and characteristics.

    I have some very basic questions that I have not solved: In your code you use ‘ [self.CBC.cBCM scanForPeripheralsWithServices:nil options:nil];’ to identify any local peripherals. When I modify this code to ‘[self.CBC.cBCM scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"1809"]] options:nil];’ the 1809 thermometer service is not found. The full UUID does not work either: ‘[self.CBC.cBCM scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"24B92166-780A-9B75-5233-B46680DE4A46"]] options:nil];’. Do you have any advice on how to specify which services are located? The same question can then be asked for choosing a specific characteristic so that a value for a sensor can be displayed on the iPhone. The TI and Apple example codes seem to use the 4-digit UUID’s but I have not been successful in using them.

    Thanks in advance.

    Log:
    2012-09-25 19:15:36.991 BLE-Tutorial_2[2196:907] Initializing BLE
    2012-09-25 19:15:37.012 BLE-Tutorial_2[2196:907] applicationDidBecomeActive
    2012-09-25 19:15:37.052 BLE-Tutorial_2[2196:907] CoreBluetooth BLE hardware is powered on and ready
    2012-09-25 19:15:43.797 BLE-Tutorial_2[2196:907] Button pressed, start scanning …
    2012-09-25 19:15:43.841 BLE-Tutorial_2[2196:907] Did discover peripheral. peripheral: <CBConcretePeripheral: 0x1e055340 UUID = 24B92166-780A-9B75-5233-B46680DE4A46, Name = “DKBLE112 thermomete”, IsConnected = NO> rssi: -37, UUID: 24B92166-780A-9B75-5233-B46680DE4A46 advertisementData: {
    kCBAdvDataLocalName = “DKBLE112 thermometer”;
    }
    2012-09-25 19:15:46.520 BLE-Tutorial_2[2196:907] Connection successfull to peripheral: <CBConcretePeripheral: 0x1e055340 UUID = 24B92166-780A-9B75-5233-B46680DE4A46, Name = “DKBLE112 thermomete”, IsConnected = YES> with UUID: 24B92166-780A-9B75-5233-B46680DE4A46
    2012-09-25 19:15:49.270 BLE-Tutorial_2[2196:907] Starting Service Scan !
    2012-09-25 19:15:49.276 BLE-Tutorial_2[2196:907] Service discovery on peripheral : <CBConcretePeripheral: 0x1e055340 UUID = 24B92166-780A-9B75-5233-B46680DE4A46, Name = “DKBLE112 thermomete”, IsConnected = YES> UUID: 24B92166-780A-9B75-5233-B46680DE4A46 OK !
    2012-09-25 19:15:49.282 BLE-Tutorial_2[2196:907] [0] – Service : UUID: Generic Access Profile
    2012-09-25 19:15:49.291 BLE-Tutorial_2[2196:907] [1] – Service : UUID: Unknown ()
    2012-09-25 19:15:49.300 BLE-Tutorial_2[2196:907] [2] – Service : UUID: Unknown ()
    2012-09-25 19:15:49.309 BLE-Tutorial_2[2196:907] [3] – Service : UUID: Unknown ()
    2012-09-25 19:15:49.328 BLE-Tutorial_2[2196:907] Service discovery on peripheral : <CBConcretePeripheral: 0x1e055340 UUID = 24B92166-780A-9B75-5233-B46680DE4A46, Name = “DKBLE112 thermomete”, IsConnected = YES> UUID: 24B92166-780A-9B75-5233-B46680DE4A46 OK !
    2012-09-25 19:15:49.338 BLE-Tutorial_2[2196:907] [0] – Characteristic : UUID: Device Name
    2012-09-25 19:15:49.347 BLE-Tutorial_2[2196:907] [1] – Characteristic : UUID: Appearence
    2012-09-25 19:15:49.356 BLE-Tutorial_2[2196:907] Service discovery on peripheral : <CBConcretePeripheral: 0x1e055340 UUID = 24B92166-780A-9B75-5233-B46680DE4A46, Name = “DKBLE112 thermomete”, IsConnected = YES> UUID: 24B92166-780A-9B75-5233-B46680DE4A46 OK !
    2012-09-25 19:15:49.365 BLE-Tutorial_2[2196:907] [0] – Characteristic : UUID: Unknown ()
    2012-09-25 19:15:49.375 BLE-Tutorial_2[2196:907] [1] – Characteristic : UUID: Unknown ()
    2012-09-25 19:15:49.386 BLE-Tutorial_2[2196:907] [2] – Characteristic : UUID: Unknown ()
    2012-09-25 19:15:49.396 BLE-Tutorial_2[2196:907] Service discovery on peripheral : <CBConcretePeripheral: 0x1e055340 UUID = 24B92166-780A-9B75-5233-B46680DE4A46, Name = “DKBLE112 thermomete”, IsConnected = YES> UUID: 24B92166-780A-9B75-5233-B46680DE4A46 OK !
    2012-09-25 19:15:49.407 BLE-Tutorial_2[2196:907] [0] – Characteristic : UUID: Unknown ()
    2012-09-25 19:15:49.417 BLE-Tutorial_2[2196:907] Service discovery on peripheral : <CBConcretePeripheral: 0x1e055340 UUID = 24B92166-780A-9B75-5233-B46680DE4A46, Name = “DKBLE112 thermomete”, IsConnected = YES> UUID: 24B92166-780A-9B75-5233-B46680DE4A46 OK !
    2012-09-25 19:15:49.429 BLE-Tutorial_2[2196:907] [0] – Characteristic : UUID: Unknown ()
    2012-09-25 19:15:51.530 BLE-Tutorial_2[2196:907] peripheralDidUpdateRSSI
    2012-09-25 19:15:51.532 BLE-Tutorial_2[2196:907] RSSI : -40
    2012-09-25 19:15:51.554 BLE-Tutorial_2[2196:907] RSSI updated : -40

  6. Lowell Bahner says:

    Thanks again for your great BLE tutorials!

    I found a solution to my previous question in the http://developer.apple.com/library/ios/#samplecode/TemperatureSensor/Introduction/Intro.html and http://developer.apple.com/library/mac/#samplecode/HealthThermometer/Listings/ReadMe_txt.html that I had not previously examined. The TemperatureSensor example would recognize the DKBLE112 Development Board when the ‘scanForPeripheralsWithServices:nil’ was used to locate any peripheral. The HealthThermometer example included code for parsing out the thermometer characteristic value data so that the temperature data correctly displays on the iPhone 4S.

  7. Oshri says:

    Is anyone can help me with the mentioned above question?

  8. Sunho Park says:

    My BTLE device provides a characteristic with the indication property set (i.e. CBCharacteristic.properties & CBCharacteristicPropertyIndicate == CBCharacteristicPropertyIndicate).
    My iOS app code issues CBPeripheral::setNotifyValue:YES …. for this characteristic. According to the CoreBluetooth documentation, the didUpdateValueForCharacteristic method is supposed to get called for both notifications and indications. It works for notifications, but not for indications — the method is never called.

    My Questions is as follows:

    I do not know how to enable indication.
    So how can I enable the indication, not notification ?
    And by the Spec, in case of indication
    how can client (iOS app) send confirmation to server (bluetooth device) ?

    Thanks in advance.

    • zhouhao says:

      Actually my sample code below your message is also trying to enable the indication. My indication is in a descriptor of a characteristics.

      • ole says:

        Hi !

        When you are enabling notification (indication) you must use the setNotifyValue routine in CBPeripheral like this :

        [peripheral setNotifyValue:enable forCharacteristic:characteristic];

        Hope this helps :)

        Ole A.

  9. zhouhao says:

    My problem is that my application crashes when I call writeValue:forDescriptor inside:

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

    if (!error) {
    for ( CBDescriptor* descriptor in characteristic.descriptors )
    {
    if ( [[descriptor.UUID representativeString] isEqualToString:@”2902″ ] )
    {
    const unsigned char raw_data[] = {0×0002};
    NSData *myData = [NSData dataWithBytes: raw_data length: 2];
    [peripheral writeValue:myData forDescriptor:descriptor];
    }
    }
    }
    }

    The error message is:

    *** Assertion failure in -[CBConcretePeripheral writeValue:forDescriptor:], /SourceCache/CoreBluetooth/CoreBluetooth-59/CBConcretePeripheral.m:360
    2012-11-27 11:11:47.789 BLETest[12524:907] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Cannot write Client Characteristic Configuration descriptors using this method!’

    I really have no idea it’s because I misused this method or something else because the Apple’s document is too simple. Do you have any sample code call writeValue:forDescriptor?

  10. andy_chen says:

    when i readValueForCharacteristic then didUpdateValueForCharacteristic show a error:
    “Error updating value for characteristic Unknown () error: The operation couldn’t be completed. (CBErrorDomain error 0.)”
    could you help me?

  11. preeti says:

    I have run your code..it does nothing,..Printing only Static Content.Can you explain..Do this code really search Peripherals? if i try to connect particular peripheral..bt it won’t connect.

  12. Allan Evans says:

    Great intro to BLE. One comment: You use the viewDidLoad method of BTSViewController to initialize CBCMCtrl and CBCPCtrl. It is considered a best practice to create designated initializers in your own custom classes. Here is a sample for CBCPCtrl:

    - (id)initWithDelegate:(id)CBCPCtrlDelegate
    {
    self = [super init];
    if (self)
    {
    self.delegate = CBCPCtrlDelegate;
    }
    return self;
    }

    Initialize this in BTSViewController like so:

    self.CBP = [[CBCPCtrl alloc] initWithDelegate:self];

    Makes things a little neater in your view controller code and helps enforce encapsulation of your custom classes. Implementation for CBCMCtrl is left as an exercise for the reader. :)

  13. Alison says:

    Wonderful tutorial!

    I’m using Xcode 4.6.1 and iOS 6.1,when I run this project, somehow when the “didConnectPeripheral:” is called and performed , the “didDisconnectPeripheral:” will be invoked immediately, thus I always fail at making a successful connection.

    Would you give me some suggestions on how to fix this issue?

    Thanks!

  14. Martin says:

    Could you explain the part writeCharacteristic, please?
    Thanks

  15. Steveshin says:

    I am a student studying ios app.
    An error occurs while an update to ios7.
    So seeking help less articles.
    Use the BLE.
    I’m studying to see BLE Tutorial 2 above.
    However, the error will be compiled in ios7.

    That information,

    …. Propert in CBPeripheral ㅛ UUID not found …. ”

    NSLog(@”Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ “, peripheral, RSSI, peripheral.UUID, advertisementData);
    What will happen to modify this section because I’m not sure starter.

    I would appreciate taught.

    Thank you for…

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="">