Simple IOS App for BLE Modules

by chocolateDrizzle in Circuits > Wireless

2251 Views, 3 Favorites, 0 Comments

Simple IOS App for BLE Modules

bluetoothImage.PNG

This Instructable walks through how you can create an iOS app with very basic functionality. This Instructable will not go through the entire process of making an iOS BLE app. It will only give a high level overview of some important elements within the app. From there hopefully you can download the project and play with the code yourself to learn more about the implementation.

I am communicating with an RN4871 BLE module in my project. Specifically the RN4871 Click Board which is made by MikroElektronika. These Click Boards can be found on the Mikro website as well as other distributor websites such as DigiKey, Mouser, etc. Admittedly, these Click Boards are more expensive than other modules that you can find, however I like to use them in my projects because out of the box they are ready to transmit and receive data without needing any configuration. I've had way too many times where I've bought a cheap $5 module online and had to read the entire datasheet to figure out how to configure it. For me that's about 2-4 hours of work trying to configure the module before I can even send data! These click boards seem to work right out of the box with no headaches so they get a thumbs up from me!

Although this iOS app is made to communicate with the RN4871 and RN4870, this same code can be used for other BLE modules as well (with some code modification of course).

Feel free to use the code however you like! I'm not a professional app developer so forgive me if anything in it makes you cringe :)

The Added BLE Permissions

info.png

One important feature is the added permission to use BLE within the app.

The source code of this app has an added key in the info.plist file. The Privacy - Bluetooth Peripheral Usage Description key needs to be added in order to utilize BLE. Without adding this Bluetooth key, Xcode will give you an error when you try to run the app.

The Bluetooth.swift File

This is arguably the most important file in this project. Within this Bluetooth.swift file, a global object of type BluetoothClass is created. This global object is initialized by the BluetoothHomeViewController when it appears.

The object holds both a centralManager variable and a peripheral variable. Once these variables are defined, they are used throughout the rest of the app. Through implementing our own class, we avoid needing to initialize multiple instances of the centralManager and peripheral, therefore we can use the same object no matter how many viewControllers or files are added. Additionally we don't have to worry about passing a single object to multiple files and viewControllers. That can get messy!

This file contains everything that is used to discover, connect and talk to a peripheral.

It also contains the serviceUUID that we will scan for along with the rxUUID (receive), and txUUID (transmit). If you would like to use a different module with this app, all that you should have to do is change these values to match the UUIDs of the new module that you are using.

The ViewControllers

This app is extremely simple. There are only two ViewControllers: one to send data back and forth, and one to scan for peripherals.

BluetoothHomeViewController important things to note:

  • We create notifications for when our Bluetooth object finds a peripheral and when our Bluetooth object receives a message.
  • We subscribe to the received message notification.
    • This basically generates an interrupt, when in this viewController, anytime we receive something. We then display what we received in the text field.

ScannerViewController important things to note:

  • We subscribe to the found peripheral notification.
    • This creates an interrupt, when in this viewController, any time a new peripheral is found that matches our serviceUUID so that we can reload the table displaying the available peripherals.

That's Pretty Much It!

Of course there are other things going on within the app. However I only described the things that might not be very clear within the implementation. Hopefully the rest of the code that I did not talk about is self explanatory.

Again this code should be able to be used with other BLE modules outside of the RN4871. You should simply need to modify the UUIDs inside of the Bluetooth.swift file.

Please download the project and play with the code yourself to learn exactly how everything is implemented. The code is very simple so that you can add and modify it to fit your own application.

Happy Coding!

-ChocolateDrizzle