Communicating using Core Bluetooth - ios

I am new to Objective c and I want to send simple strings from an iPhone to an arduino an vice versa with Bluetooth. I have read apples information about Core Bluetooth, but I am having trouble understanding it. As I said, all I need the iPhone to do is: 1) connect to the BLE device of the arduino. 2) Send instructions (in the form of strings) 3)get a response from the arduino when the instructions have been carried out, so that the next set of instructions can be sent. I would be very happy if somebody is able to help me with this project
Thanks

You should follow the wwdc presentation on core bluetooth.
You should probably start here

I suggest operate your device with YMSCoreBluetooth.
The basic step to operation bluetooth device is:
Search all nearby device by blue tooth scan, you will find all nearby device.
Stop your scan. Connect one of the devices found in response.
Discover all services on the device.
Discover all character in service found by service discover action.
Write your content into character found in 4.
You can read character value from device.
YMSCoreBluetooth let you write your logic in one code block instead of separate delegate.

Related

How do I pair and/or bond to BLE on iOS using Swift code and an HM-10 so data sent is encrypted?

Background
I've built a simple circuit which includes an Arduino Nano and a HM-10 BLE serial module.
You can see that circuit at the following Electrical Engineering SE question: https://electronics.stackexchange.com/questions/412525/when-i-write-data-to-a-ble-characteristic-ffe1-to-send-data-over-bluetooth-is
I also have an Xcode project written in Swift that successfully sends bytes to the FFE1 characteristic.
It all works great and I can see the data coming from my iPad via Arduino Serial monitor.
However, from the research I did (see previous link) I believe I've determined that the data I send using Core Bluetooth writeValue() is not encrypted.
What I'm Trying To Do
Now that I've successfully sent and received bytes I would like to cause my iPad to bond with the BLE device (HM-10) so I can insure the data I send is encrypted via the provided BLE channel.
What I've Tried
Read through the Core Bluetooth docs at Apple, but I cannot find how to pair or bond to the device so that the data sent over the channel will then be encrypted.
Read every SO question I could find, but can't find any iOS code that
shows me how to bond and send data securely.
I did find some very basic info that to get the devices to bond I
need to try to write to a characteristic of the BLE device that has
the property CBCharacteristicPropertyNotifyEncryptionRequired but the
documentation on that at Apple is a ridiculous one-liner.
I wrote code that iterated through the characteristics of the HM-10
and read the properties and I didn't seem to find that property. But
the docs on the HM-10 seem to indicate that it does support a
authentication and encryption and I know that is a part of BLE.
Questions
Can you point me to Swift code that gives me an example of what I will do to bond and send encrypted data? Is there some property I need to set on CBPeriperhal or some param I need to send into writeValue()?
Am I misunderstanding? Does the HM-10 not support sending encrypted
data?
After posting my question I searched for pairing / bonding again.
I was taken to a link to a datasheet for the HM-10 which I've read numerous times. This time however, I searched for the word "bond" inside the document.
When I did that I read the following chart in the document:
This started me thinking and guessing.
I believe the issue is that the HM-10 is sent out as TYPE 0 so that hobbyist users can get it set up without pairing.
This is quite different from the HC-05 modules (classic Bluetooth) that I've used numerous times in other projects and which require pairing.
Possible Solution
I believe all I need to do is set the AT_TYPE to 2 and then my iOS (iPad) device will then allow me to pair using the PIN.
I'm guessing that it will then force every user to use the PIN to pair their device and then data will only be able to be written (using writeValue) via the secure BLE channel.
Extra Information
This is interesting to me also because when I use the default Bluetooth iOS scanning the OS never sees the HM-10-- the name doesn't appear under Bluetooth section. I am guessing that is because the HM-10 is not broadcasting that it can pair via pin and iOS ignores it.
That was always interesting to me because even though iOS couldn't see the HM-10, I could connect to it and send data to it via my Swift code. Wow, this may all be coming together finally.
I will test this tonight and post back to confirm if setting the TYPE solves it.
One More Thing
None of the extensive reading that I've done on the HM-10 (see Martyn Curry's fantastic, detailed articles) have ever mentioned this option and I've not seen it discussed anywhere.
I also see that some sites indicate that AT+TYPE is for setting whether the device is a Master/Slave (Central/Periperhal) which I don't believe is correct documentation for that item. This has all made it additionally difficult to determine how to get this working.
Update Note
I was reading yet another datasheet for HM-10 and this one shows that the default setting for AT+TYPE is in fact 0 (PIN code not needed), shown at the bottom of the chart. Not sure why the other datasheet didn't have this same info since it looks like the same chart.
Update 2 - This is the answer. I powered up the HM-10 and ran the Arduino code that allows me to send AT commands and sent AT+TYPE3<ENTER> and then sent AT+RESET to insure the HM-10 would restart itself.
After that, there is nothing else you need to do. I used my iOS app running on my iPad to send data to the FFE1 characteristic. When I attempted to do so, iOS popped up the default BLE pair/bond dialog and asked or the PIN (default is 000000 on these HM-10 components).
Note One thing I found was that one of my HM-10 is faulty. It allows data to be written even if I cancel the pairing or fail pairing (by sending incorrect PIN). That manufacturer's component is no longer available so I'm assuming that it is in error. The other HM-10 I have works exactly as expected. If you don't pair or fail to pair then the app fails to write the data. I will mark this as answer as soon as I can.

How to ask for authentication before connect to devices using bluetooth?

I want to make demo application, which contains following features.
search and show device list in my device Bluetooth range.
connect(pair) selected devices.
then share string data.
For doing that I am using core Bluetooth framework(Central and peripheral classes). using that I am able to search devices, connect devices and transfer string data also.
but I am not able to authenticate devices before pairing(when I call connect method it is directly shown me connected status). how is it handled in ios?
also please suggest me I am on the right path or I need to use another library to implement this functionality.
For anyone else beating their head against a wall, I have the solution to this issue. It's as simple as setting the AUTHENTICATION required flag for a characteristic on your BLE device. Once that flag is set, you need to attempt a READ from that service / characteristic using the iOS device. Once the read is requested, the BLE device will send back a failure message which then prompts iOS to display the PAIRING / BONDING popup. After that, you're golden.
Here is an example of a Cypress BLE device permission group. The settings are similar for most BLE embedded devices out there.
yes it is possible to ask pairing request from BLE device to ios.
I followed below steps to make ios app to ask for pairing.
edited my ble app to return error code 'RBLE_ATT_ERR_INSUFF_AUTHEN' using gatt write response function.
attempted a write from ios app then i got a prompt to enter pairing key.
used the randomly generated pairing key by BLE device to pair.
Paulw11's comment also helped me to solve that problem,
Regards
Bharath GK

Custom BLE Advertisement on iOS

I'm hoping to create a BLE advertisement on iOS where I can control the advertisement on a byte level.
One use case would be to mimic an iBeacon advertisement. What is the iBeacon Bluetooth Profile (I want to advertise while my app is in the background which is why I'm not using CoreLocation)
I do not see a way to do this with the Core Bluetooth API. Is this correct? Are there alternatives using private API's or jailbreaking?
As the others already pointed out, there is no API on iOS that allows you to do this.
You can advertise as an iBeacon, but only when your app is in the foreground. When in background, the advertisement is removed and therefore cannot be discovered anymore (except for other iOS devices which explicitly scan for that service UUID). Also see the documentation here: Core Bluetooth Background Processing
If you would share your use case and what you want to achieve, maybe there are other ways to realise it.
My experience with iOS is that if it is something is not exposed in the API, there is no way around, except jailbreaking. For Bluetooth low energy the API is at GAP/GATT level, and very little at the lower levels (if anything) is exposed. Advertising is a LL (link layer) feature.
To illustrate how restricted the access is: When scanning for BLE devices you will not have access to the advertiser's MAC address iOS. In Android you have it.
I do not see a way to do this with the Core Bluetooth API. Is this
correct?
Since you have to set Manufacture Specific Data in order to achieve this, if nothing has changed you will experience exactly the same issue that I did. Explained here:
The advertisement key 'Manufacturer Data' is not allowed in CoreBluetooth
It is not possible.

Controlling a device with CoreBluetooth

I was looking at a couple CoreBluetooth tutorials for iOS. Based on the tutorials, it looks like one device is transmitting data and the other device is receiving. Like in this example: http://www.raywenderlich.com/52080/introduction-core-bluetooth-building-heart-rate-monitor the heart rate monitor is transmitting and the iphone is receiving. Is there a way to just connect to device to control it?
For example, I have a soundbar that is Bluetooth and can I write an app to do the same thing the remote it comes with does (volume up, volume down, input source change, power off)?
Adafruit proposes a lot of tutorials / material to learn how to use BLE (Bluetooth low energy)
tutorial about their BLE module (that could run with an Arduino): https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout
tutorial about their iOS app (to connect to the previous module): https://learn.adafruit.com/bluefruit-le-connect-for-ios
iOS app source code to connect to BLE devices or play with previous module: https://github.com/adafruit/Bluefruit_LE_Connect
If you need one way transmission with the maximun data rate you need to read this article:
http://www.tbideas.com/blog/2013/04/Optimizing-Bluetooth-Low-Energy-Performance/
it talks about CBCharacteristicWriteWithoutResponse
Stackoverflow thread about the same one way communication subject:
iOS. BLE. CBCharacteristicWriteWithoutResponse - How to make it work?
Hope this helps!

How to communicate with OBD II using bluetooth or wifi using iphone

I like to create a simple application for iOS, which read data from car through ODB II Wifi/bluetooth device and display in iPhone screen. But i don't know where to start. Please somebody help me to achieve below results.
I have both Bluetooth and wifi dongle.
Step 1 : Pair the bluetooth or Wifi dongle and iPhone.
Step 2 : Read the details through dongles
Please refer any tutorial or sample code which i can understand easily. I want to understand the these process in-depth and want to code by myself. So please help.
Thanks in advance.
As David mentioned, on iOS you can use WiFi OBD interface only. Standard Bluetooth (not BTLE) will not work. Bluetooth devices should be approved by Apple to be able to transfer data to/from iOS. AFAIK there is no such a OBD's.
Probably your OBD dongle is based on ELM327 chip. Good starting point is this doc. Read it carefully. This is everything what you need. Interesting things starts at page 7.
On iOS you should open TCP connection with your OBD IP address(for example using NSInputStream, NSOutputStream), then configure OBD using AT commands. Rest is described in section "Talking to the Vehicle" (page 30) in ELM link. Enjoy reading :)
If you need more help- just ask.

Resources