I'm writing an app using Swift on Xcode that connects to a bluetooth BLE peripheral. I've established a connection to a the device, and want to read some data from a specific characteristic (specifically FFF1 in service UUID FFF0).
I'm able to request a read of characteristics using the following code if the characteristic that I want to find info for is characteristicx
:
peripheral.readValueForCharacteristic(charactericsx)
What I want to know is this: How do I check that this read value is what I'm looking for. I want to be able to do an if statement to check my value against the discovered value for that characteristic.
Eg: If discovered value is X then do something, else if discovered value is Y then do something else.
That's not a very good explanation of what I want to do, but I hope you get the gist.
Anyone know how to go about doing this?
A characteristic object represents a characteristic of a Bluetooth® Low Energy peripheral device. If read or write are supported in the object Attributes property, you can read characteristic values using read or you can write characteristic values using write .
A service can have one or more characteristics, and each service distinguishes itself from other services by means of a unique numeric ID called a UUID, which can be either 16-bit (for officially adopted BLE Services) or 128-bit (for custom services).
write( c , data ) writes the specified data to a characteristic of a Bluetooth® Low Energy peripheral device. The Attributes property of the input characteristic object c must be "Write" and/or "WriteWithoutResponse" . write( c , data , type ) specifies whether the device expects a response back using type .
A GATT characteristic is a basic data element used to construct a GATT service, BluetoothGattService . The characteristic contains a value as well as additional information and optional GATT descriptors, BluetoothGattDescriptor .
Updated For Swift3
After you execute that method, the delegate
of your peripheral is going to asynchronously receive the peripheral(_:didUpdateValueFor:error:)
method. In that method you can query the value
of the passed characteristic
parameter. value
will be an NSData
which you can pull the bytes out of. E.g.
// MARK: - CBPeripheralDelegate
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let e = error {
print("ERROR didUpdateValue \(e)")
return
}
guard let data = characteristic.value else { return }
...
}
The value
method actually returns an Optional
around the expected Data
, so a let guard is the way to go.
Usually a characteristic will have a simple value encoded in it's up-to-20-byte Data
payload. E.g. maybe it's a simple UInt16 counter. To
To convert between these Data
glumps and meaningful numbers, have a look at the answer to round trip Swift number types to/from Data (I've included my own implementation of that below).
So for example, if you know that the characteristic of interest is some counter that is a meant to be extracted as a UInt16
, I would fill out the above example with something like:
// MARK: - CBPeripheralDelegate
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let e = error {
print("ERROR didUpdateValue \(e)")
return
}
guard let data = characteristic.value else { return }
print("counter is \(UInt16(data:data))")
}
// Data Extensions:
protocol DataConvertible {
init(data:Data)
var data:Data { get }
}
extension DataConvertible {
init(data:Data) {
guard data.count == MemoryLayout<Self>.size else {
fatalError("data size (\(data.count)) != type size (\(MemoryLayout<Self>.size))")
}
self = data.withUnsafeBytes { $0.pointee }
}
var data:Data {
var value = self
return Data(buffer: UnsafeBufferPointer(start: &value, count: 1))
}
}
extension UInt8:DataConvertible {}
extension UInt16:DataConvertible {}
extension UInt32:DataConvertible {}
extension Int32:DataConvertible {}
extension Int64:DataConvertible {}
extension Double:DataConvertible {}
extension Float:DataConvertible {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With