Arduino Tutorial: Integrating Bluetooth LE and iOS

Hi Mr. Owen. Can i use Adafruit Bluefruit LE Shield instead of BLE Shield v2.0.0 ??Any differences between them ? I want to make this little project but with another bluetooth shield. Thank you !

As long as your device is supported by the Arduino library, and it is compatible with iOS - and I bet it is based on forum posts I see around - you should be fine. Additionally it is supported by the Adafruit iOS appā€¦ See the learning page on Adafruit.

Generally speaking:

  • Communication between iOS and ā€˜anyā€™ BLE device can be achieved using CoreBluetooth if you know the GATT structure of the device. As long as the device conforms to BLE standards, then it doesnā€™t matter which device you use as long as it has the features to match your project needs.

  • Communication between a BLE device and an attached microcontrollers (ie PIC, Arduino,ā€¦) is possible if you know the communication protocol used by the BLE device (assuming you are writing the microcontroller code). And if the BLE device allows you to re-flash with your custom code/script/firmware, then life gets even better because then you can determine the protocol between the BLE and the microcontroller.

I prefer using BLE modules that allow for custom firmware in a higher level language like BGScript or even C. IMO, this is much more desirable then sending line commands via a terminal program to customize the BLE device. Iā€™m not ā€˜knockingā€™ these products on the market and have used them on projects, but I prefer having more flexibility/control of what happens on the BLE module. In some projects, this additional flexibility can eliminate the extra microcontroller in the design and perform the I/O operations I need with a standalone BLE module.

Owen

Hi @owenb,

Thanks for the tutorial, I was able to get the transmit working. I was wondering, however, about implementing receiving using your specific code. If my research is correct, I need to add the didUpdateValueForCharacteristic function, however when I implement it as such:

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    NSLog(@"TEST");
}

just to see if it ever enters the function, I never see it entering, even though I am connected to a bluetooth device which I know is transmitting data. Is there something I need to enable elsewhere in the code in order to listen for incoming data? Or even more functions elsewhere that need to be added?

Thank you!

Hi kp,
Yes, the didUpdateValueforCharacteristic method is called when new values are received by iOS, if the following is true:
a) The characteristic has been discovered (inside didDiscoverCharacteristicsForService).

b) You requested to be notified of a value change (assuming the characteristic allows notifications in the GATT configuration of the Bluetooth device). You register for notifications by calling [peripheral setNotifyValue:YES forCharacteristic: characteristic];. A good place to call this is inside didUpdateValueforCharacteristic method when the characteristic is discovered.

c) And if the Bluetooth module did indeed change the value for the characteristic.

This is the recommended way of checking for new values (ie being notified when they change). Another way is to read the current value by calling [peripheral readValueForCharacteristic: characteristic];. I typically only call this when the characteristic is first discovered inside didDiscoverCharacteristicsForService, right after I set to receive notification of value changes.

Owen

Hi Mr. Owen. I have an arduino uno and a hm-10 bluetooth module. I want to make this project but with this module. My question is how can i find the service uuid for rx, tx and baudrate ? I donā€™t know how i can find them. Please tell me where i can find the uuids or how i can make the connection between iphone and my bluetooth module. I saw that you have the uuuids from an xml file. Thank you !!

Hi mihai,

There are a couple ways to get the UUIDs of a device.

  1. The datasheet for the device should give this info.

  2. In the tutorial code, go to BTDiscovery inside func startScanning. Set the scanForPeripherals services parameter to nil (line 28). This causes iOS to give scan results for all advertising BLE devices in the area. Connect to the HM-10 that will now be discovered inside didDiscover. Youā€™ll also need to set discoverServices parameter to nil, inside BTService (line 33). The tutorial code will then discover all the services on the HM-10 device. At this point, just set a breakpoint on line 50 of BTService and take note of the discovered service UUIDs. Once the services are discovered, then you can grab up the characteristic UUIDs.

  3. Download an app like BLExplr and scan while the HM-10 is advertising. Connect and grab the UUIDs.

Once you have the UUIDs, use then in the app to limit the Services and Characteristics searched for by iOS.

Btw, I found some info online that indicates the following UUIDs for the HM-10. I donā€™t know if they are valid, but wanted to post it anyway:

  • Service: 0000ffe0-0000-1000-8000-00805f9b34fb
  • Characteristic for TX & RX: 0000ffe1-0000-1000-8000-00805f9b34fb

Owen

Thank you Mr. Owen. I will try as you said. But i have some questions. I saw on internet that the service uuid is something like 00ffe0 and in this post are different. Itā€™s ok if there are different ? Are you sure that it will work with hm-10 ? I mean that you used in this project the bluegigga shield. So itā€™s not a problem if i will use the hm-10 instead of ble shield v2.0.0 right ? I am a little bit scared because itā€™s the first time when i worked with arduino and iOS. So while i get the uuid i can use the functionalities from postā€™s app right ? Thank you again for your help and i want u to know that i have a great respect for your work.

Hi mihai,
Yes, technically you can use any BLE module with the tutorial, but depending on the moduleā€™s BLE GATT setup there may need changes to the Core Bluetooth Swift code to match it.

It appears that HM-10 has GATT similar to the Black Widow with the exception that HM-10 uses only 1 Characteristic for bi-directional TX/RX. The Black Widow separates them into 2 Characteristics.

Owen

Hi there!

Iā€™ve just come across this while trying to design a device that can control something on the phone from the bluetooth device, as opposed to the other way around in this guide. For example, a button click that would cause my app to do something. Would that work the same way as it does in this example or is it only for the opposite function?

Best,
Sydney

Hi sydneybeal,
Definitely BLE communication is bi-directional.

If the setup was similar to this tutorial, youā€™d do the following:

  1. Write bytes to Arduinoā€™s BLE UART.
  2. Core Bluetoothā€™s peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) will be called with the data.
  3. Determine which characteristic had value changed by verifying the characteristic.uuid.
  4. Grab the data from characteristic.valueā€¦ and use it!

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]