I am building an iOS Swift app that connects to a BLE device (Redbear Labs Duo).
What works?
- I am able to initiate a scan for devices
- Connect to the device
- Collect services and characteristics of the BLE device - all good
Where is the problem?
- The BLE device (Redbear Labs Duo) also has a Wifi controller onboard and is capable of scanning for available networks. The documentation states that to scan for Wifi one must
- connect to the primary service with UUID 3EC61400-89CD-49C3-A0D9-7A85669E901E
- find the command characteristic with UUID 3EC61401-89CD-49C3-A0D9-7A85669E901E
- send a 2 byte command [0x20, 0xA0] to the command characteristic
- also set a 1 byte status indicator 0xB1 to the scan characteristic with UUID 3EC61402-89CD-49C3-A0D9-7A85669E901E
My code to do the above steps is as below..
func scanWifi() {
print("[DEBUG] - Scanning for Wifi")
let command:[UInt8] = [0x02, 0xA0]
let commandData = NSData(bytes: command, length: command.count)
BLE.sharedInstance.write(toCharacteristic: BLE.sharedInstance.RBL_CHAR_CMD_UUID, data: commandData, withType: .withResponse)
let state:[UInt8] = [0xB1]
let stateData = NSData(bytes: state, length: state.count)
BLE.sharedInstance.write(toCharacteristic: BLE.sharedInstance.RBL_CHAR_SCN_UUID, data: stateData, withType: .withResponse)
BLE.sharedInstance.read(fromCharacteristic: BLE.sharedInstance.RBL_CHAR_SCN_UUID)
}
Everything works... but... after writing the above data to the peripheral I was expecting the below method to get called - it never did.. what am I doing wrong?
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if error != nil {
print("[ERROR] Error updating value. \(error!.localizedDescription)")
return
}
if characteristic.uuid.uuidString == RBL_CHAR_CMD_UUID {
self.delegate?.bleDidReceiveData(data: characteristic.value as NSData?)
}
}
Update:
I set a bunch of debug statements and got the following output - from the below it is apparent that
- I am able to identify and connect to the right device and characteristics
- I am able to set the notification value appropriately
[DEBUG] Connecting to peripheral: 547BC3C9-4823-431C-B888-A8F3E8C699F5
[DEBUG] Connected to peripheral 547BC3C9-4823-431C-B888-A8F3E8C699F5
[DEBUG] Did connect to peripheral
[DEBUG] Found service: 3EC61400-89CD-49C3-A0D9-7A85669E901E for peripheral: 547BC3C9-4823-431C-B888-A8F3E8C699F5
[DEBUG] Found characteristic: 3EC61401-89CD-49C3-A0D9-7A85669E901E for peripheral: 547BC3C9-4823-431C-B888-A8F3E8C699F5
[DEBUG] Found characteristic: 3EC61402-89CD-49C3-A0D9-7A85669E901E for peripheral: 547BC3C9-4823-431C-B888-A8F3E8C699F5
("3EC61402-89CD-49C3-A0D9-7A85669E901E", )
("3EC61401-89CD-49C3-A0D9-7A85669E901E", )
[DEBUG] didUpdateNotification state for characteristic CBCharacteristic: 0x1702a6c60, UUID = 3EC61401-89CD-49C3-A0D9-7A85669E901E, properties = 0x14, value = (null), notifying = YES on peripheral: CBPeripheral: 0x1740fba80, identifier = 547BC3C9-4823-431C-B888-A8F3E8C699F5, name = Duo-ZKBY, state = connected
[DEBUG] didUpdateNotification state for characteristic: CBCharacteristic: 0x1742a3c60, UUID = 3EC61402-89CD-49C3-A0D9-7A85669E901E, properties = 0x10, value = (null), notifying = YES on peripheral: CBPeripheral: 0x1740fba80, identifier = 547BC3C9-4823-431C-B888-A8F3E8C699F5, name = Duo-ZKBY, state = connected
notify
, notread
so you will need to callsetNotifyValue
on the peripheral. – Paulw11setNotifyValue
- did some more research and my problem is similar to the one here - stackoverflow.com/questions/31275013/… - however the difference is that I tested the device using the iOS app provided by the manufacturer and it does indeed all work. What mistake am I making that doesnt give me a response from the device – ChicagoSkydelegate
? – Paulw11didConnect peripheral
method. Also please see additional debug inputs that i have included above – ChicagoSkywithoutResponse
. I was writingwithResponse
. Also, the Scan characteristic is only meant for notification and nothing else. – ChicagoSky