How to Pair And/Or Bond to Ble on iOS Using Swift Code and an Hm-10 So Data Sent Is Encrypted

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

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:

HM-10 AT+TYPE bond

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.

set module bond mode

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.

Convert BLE 4.0 data into usable information using Swift (iOS)

Finally after three days of struggling I figured this out. Answer had nothing to do with the swift code. Instead my baud rate on the arduino was too slow. Increasing this solved everything.

Apple iOS Swift5 BLE - How to send 1 byte to a characteristic (write function expects Data type, but one byte is UInt8 type)

The issue there is that your Int8 is an optional. Thats why you are getting an extra byte. All you need is to unwrap your optional before converting it to Data.

Why does CBCentralManager report Peer removed pairing information with 1st phone after 2nd phone is paired with device?

Does BLE 4.0 Allow multiple devices to be paired & bonded?

Sure. But that doesn't mean that the device does. It needs to allocate memory for it, and smaller devices often don't.

Do you know why the Peripheral would remove the pairing info? (again, this only occurs if I pair the device with a new iphone / ipad).

It doesn't remove the pairing info. It's telling you the device has removed the pairing info. Pairing info includes a shared secret, so if one side throws away their secret, there's no point in you keeping yours. You have to create a new one.

Is there a way to send something via iOS app that would tell it to renew the pairing information?

Unfortunately no. Apple doesn't give any access to the pairing experience.

There isn't any standardized way to ask devices this kind of information. Some devices support a single pairing Some support two. Some support "a few." But it's really up to the device.

It is very possible that you will get a better overall experience with a Bluetooth 4.1 or 4.2 device, because they've added better security protocols. With iOS 13, the phone also supports cross-transport key derivation, which possibly will help here (if the device also supports it). But I don't think you can improve things with a Bluetooth 4.0 device.

Data value Overflow

Based on this SO post: round trip Swift number types to/from Data (pointed to by Wei Jay)...

Try this:

func readCharacteristic(data: Data) -> Int {
return data.withUnsafeBytes { $0.pointee }
}

Note: that post provides various ways to convert the Data to different data types - Int, Float, Double, etc - so this could be used if you know you'll only be reading an Int value. Otherwise, just build on this + the other post.



Related Topics



Leave a reply



Submit