BLE Tutorial 1

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

Load or make a new project as described in my first article.

Then you should add a class named : “CBCMCtrl”. This is done in the following way in Xcode :

Xcode main window for application

Xcode main window for application

Select the project on the left side panel and right click :

Project context menu

Project context menu

Select “New File …” and continue :

File type selection

File type selection

You should now select objective C class, and select next

Class name and subclass selection

Class name and subclass selection

 

Enter class name into the “Class” field : CBCMCtrl

The “Subclass” field selects if this class should inherit (be a subclass) of any particular class, you should choosw NSObject here which is the generic object-oriented class in iOS.

Select Next.

Select creation location

Select creation location

Select location for your new files, and click “Create”

Class creation complete

Class creation complete

 

Your new class is now ready and added to your project.

 

We will also have to connect two buttons and one textfield to our application. This is how its done :

 

In the project navigator select the MainStoryboard_iPhone.storyboard file, the following screen will be shown :

Screenshot of Empty Storyboard for an iPhone Application

Empty Storyboard for iPhone App

The empty storyboard for the iPhone application is shown, we will need two buttons, so we’ll have to add them.

Down in your right corner you have the object navigator. It shows you the possible elements you can add to your storyboard :

iPhone_Storyboard_Object_Navigator_Button

Object Navigator Round Rectangular Button viewed

 

We are going to use the Round Rect button, and the Text field :

iPhone_Storyboard_Object_Navigator_Text_Field

Object Navigator Text Field Object

 

Building an interface is pretty straight forward, add two buttons and one text field, and modify sizes so it looks like this :

iPhone_Storyboard_Text_Field_Added

Buttons and Text Fields Added

 

When changing the text in a button, it can be done by selecting the object and turning on the property navigator (the window above the object navigator and selecting the little slider icon) :

Property_Navigator_shown

Property Navigator Shown

You can see the Title field, change this to change the title of your buttons, then select the text view and remove the standard text.

 

Now that we have made the buttons and the textview, we will need a method of registering when the button is pressed, or changing the text in the text field, this is done by actions and outlets in iOS :

  • Actions is an event that is runned when something happens to an object in the storyboard (e.g. press of a UI button).
  • Outlet is a way to get a pointer to an object in the storyboard so that you can change its properties (e.g. changing title of a button or changing text in a text view).

Connecting these is easy :

  1. Turn on the assistant view (Select View -> Assistant Editor -> Show Assistant Editor on the menu
  2. Press and hold the Ctrl key
  3. Select object in storyboard.
  4. Drag mouse cursor over to the source file where you want the action or outlet to go
  5. Select settings (e.g. Action or Outlet) and name it and select connect.

 

iPhone_Storyboard_Adding_Button_Outlet

Adding button outlet

 

iPhone_Storyboard_Adding_Scan_Button_Action

Adding scan button action

 

You should now be able to enter the following source into your files :

Source listings :

BTSAppDelegate.h :

BTSAppDelegate.m :

BTSViewController.h :

BTSViewController.m

CBCMCtrl.h

CBCMCtrl.m

All files for this howto including XCode project, is in this zip file : BLE-Tutorial_1

6 Responses to BLE Tutorial 1

  1. Stewart says:

    Hey there,

    I’ve been really impressed by these tutorials and the way in which they are so well laid out. I too am looking at the various potential that BLE can bring to iOS developers. Specifically how the tech can be used on a leash basis with background activity triggers. Not unlike the simple application that Find my Car Smart uses… Have you had any experience yet of proximity based applications on iOS? And, would you be interested in working on something together to show how these technologies can be leveraged by App developers?

    • ole says:

      Hi Stewart !

      Thanks for your kind words :)

      I’ve done some test work on proximity applications, and I’ve seen the Find my car smart app. It should be pretty straight forward to develop these kinds of applications in iOS.

      In my BLE Tutorial 2 I’ve added the read RSSI call which gives the programmer the possiblity to use it for proximity, as well as using the trigger for device disconnect as a trigger for sensing out of range, and then start connecting again, and when peripheral is connected you sense that it’s in range.

      One thing I haven’t investigated is how iOS acts if an application running in the background starts a connection or disconnection sequence.

      It would be nice to do a demo application of something like this :) I’ll have to see when I could get the time to collaborate on a demo project, it would be cool to do !

      Ole A.

      • Stewart says:

        Hi Ole,

        I have a theory on how to do this, so I’ll have a play at some point with your 2nd tutorial and if it all works, I’ll let you know.

        If my theory is correct (seems to be totally undocumented though), it is absurdly simple. But I’ll obviously check it out on a device too as I have some programable BLE chips. If I am correct, I’ll drop you an email with the details.

        Cheers,

        Stewart.

  2. Richard says:

    Great Tutorial !

    I did copy and paste your code and tryed to use it with my iPhone4S and the cc2540 Keyfob.
    The Rssi Value Display dont work :-( . Do I use a wrong hex file on the Keyfob (/Ti../Accessories/HexFiles/cc2540..DemoSlave.hex)?

    By the way, I am investigating a projekt same as yours. My idea is to mesure the distance by Rssi or round trip time. But somehow will there be no success in that BLE Distance Problem. Goal is to display the distance on the iphones screen (opt. with alert/event at explicid value).

  3. Francisco Cabeza says:

    You are using this in viewDidLoad for your ONLY viewController:

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@”Initializing BLE”);
    [dbgText setText:[NSString stringWithFormat:@"%@Initializing BLE\r\n",dbgText.text]];
    self.CBC = [CBCMCtrl alloc];
    if (self.CBC) {
    self.CBC.cBCM = [[CBCentralManager alloc] initWithDelegate:self.CBC queue:nil];
    self.CBC.delegate = self;
    }
    }

    But what if I have a second view controller? For example:
    First View Controller manages the Bluetooth Device Info,
    Second View Controller manages the Bluetooth Device Battery Info.

    What do I put on viewDidLoad of the Second View Controller?

    I tried to put the same as in the First View Controller, but it’s not working.

    Thanks for your help.

    Regards.

    Francisco Cabeza

    • Hi Francisco !

      When working with BT and multiple viewcontrollers in iOS, there are more than one way to do this.

      One way is to set the delegate in the viewDidAppear function of each viewcontroller, this function is called after a view has been displayed to the user.

      This has the disadvantage that your last viewcontroller will no longer receive updates when another viewcontroller takes over.

      Another way is to make a class that handles all your bluetooth activity, and making a protocol from this which handles the communication with CoreBluetooth and runs callbacks on all your viewcontrollers.

      Ole A.

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