Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notification from Bluetooth LE devices in iOS app

I am working on an iOS Bluetooth LE application. The functionality which I am able to follow correctly and successfully are as follows :

  1. Discovering peripherals.
  2. Connecting to peripheral.
  3. Getting services and characteristics.
  4. Able to read data from the characteristics while clicking on read button.
  5. Able to write data.

Here I am facing a problem, I need to read incoming data only when the BLE device transmit it to app. I'm explicitly reading the characteristics on button click. My BLE device is continuously transmitting some data in particular intervals, but I am not able to get it.

I have set the setNotify as well on characteristics, not success on it as well.

[peripheral setNotifyValue:YES forCharacteristic:characteristic];

How can my app be notified by the BLE device event (sending by it to app)? Please help me here or suggest me something.

Thanks in advance.

like image 516
Lalit Paliwal Avatar asked Aug 08 '12 05:08

Lalit Paliwal


2 Answers

On the peripheral side, it is necessary to set the properties of the characteristic to enable notifications. You do this with the CBCharacteristicPropertyNotify property. For example, the following is how you might create the characteristic:

CBMutableCharacteristic *alertLevelCharacteristic = 
[[CBMutableCharacteristic alloc] initWithType:alertLevelCharacteristicUUID
                                   properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify
                                        value: nil permissions:CBAttributePermissionsReadable];
like image 28
JeffB6688 Avatar answered Oct 02 '22 13:10

JeffB6688


What you are doing should work. All data that arrives from the Peripheral will come to the didUpdateValueForCharacteristic callback method. Look for it there.

So whether you explicitly call the readCharacteristic method or just set up your peripheral to notify you when it has data available (like an alarm, or a heart beat), you will still receive the data the same place.

Note: When you do send the setNotifyValue message you should receive a callback on the didUpdateNotificationStateForCharacteristic method with no error. If not, I suggest you look at your Peripheral firmware and make sure that characteristic is not read/write only.

like image 173
Anders Grunnet-Jepsen Avatar answered Oct 02 '22 13:10

Anders Grunnet-Jepsen