alesdario
alesdario

Reputation: 1894

Sending command to Bluetooth LE Device from a iOS App

I'm building a simple IOS app for interacting with a Bluetooth LE device.

Main steps ( Connecting, discovering services, discovering characteristics and reading characteristics' values ) are done rightly.

Now i'm searching for a tutorial/example for sending command to the Bluetooth LE device i'm connected on.

I'm searching in the Apple Developer Guide but i'm not finding anything.

Upvotes: 1

Views: 4835

Answers (3)

Stephen MbientLab
Stephen MbientLab

Reputation: 36

You should check out Apple's TemperatureSensor example, specifically where it calls [servicePeripheral writeValue:data ...

Upvotes: 2

Robert Haworth
Robert Haworth

Reputation: 448

When interacting with a CBCharacteristic I am going to assume it is a read/write characteristic (you can confirm this by looking at the value for properties on your characteristic).

The two main methods when interacting with a characteristic are:

func writeValue(_ data: NSData!, forCharacteristic characteristic: CBCharacteristic!, type type: CBCharacteristicWriteType)
func readValueForCharacteristic(_ characteristic: CBCharacteristic!)

Both of these methods are found on your CBPeripheral. Once you have called one of these functions you can utilize the CBPeripheralDelegate to confirm each of these actions in these delegate methods:

optional func peripheral(_ peripheral: CBPeripheral!, didWriteValueForCharacteristic characteristic: CBCharacteristic!, error error: NSError!)
optional func peripheral(_ peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error error: NSError!)

These are the places you will look to confirm your read and write's success. (inside of your read you can retrieve the value that was read from the BLE Device off the value property on CBCharacteristic.

Keep in mind the interactions you have (what you can read and write) are entirely dependent on the BLE device you are interrogating/interacting with. Essentially you must know what to read, what to write, and how.

Upvotes: 1

Avt
Avt

Reputation: 17043

You can send a data with writeValue:forCharacteristic:type: method of CBPeripheral. What characteristic+data match the required commands must be described in device specification.

Upvotes: 0

Related Questions