0
votes

I'm sending a command to bluetooth device but got no response return back. What'm I doing wrong for creating the data packet?

Here is the example for the frame format describe by document. enter image description here

I've tried on create a data such as:

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

    let bytes : [UInt16] = [0x0000, 0x0002, 01, 00]
    let data = Data(bytes: bytes, count: bytes.count)

    self.peripheral.writeValue(data, for: characteristic, type:  CBCharacteristicWriteType.withoutResponse)

}

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor   characteristic: CBCharacteristic, error: Error?) {

        if (error != nil) {
            print("didWrite Value for characteristic:\(characteristic)")
        }

    }

}

I though I did write value to peripheral success, but after that no any respond of func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor ...) return. Anyone please help?

1
This would send only the first 4 bytes of data of your array, [0,0,2,0]. Are you sure that's correct? To me, the doc excerpt reads more like should send the 4 bytes [0,2,1,0] and you should change your definition to use UInt8. - Gereon
@Gereon Thanks suggestion. let bytes : [UInt8] = [0x0000, 0x0002, 01, 00], let data = Data(bytes) still not working. - yonlau
Try with let bytes : [UInt8] = [0x00, 0x00, 0x00, 0x02, 0x01, 0x00] - Orkhan Alikhanov
According to the table, it should be [ 0, 0, 0, 2, 1, 0 ] - Gereon
Thanks guys, it work finally. I just forget to consider their bytes order with little endian! - yonlau

1 Answers

0
votes

You are using CBCharacteristicWriteType.withoutResponse as the type when calling self.peripheral.writeValue, and according to Apples documentation, you would have no response from the peripheral to indicate whether the write was successful. https://developer.apple.com/documentation/corebluetooth/cbcharacteristicwritetype/withoutresponse

Is it possible for you to try using CBCharacteristicWriteType.withResponse instead, to see if you get a returned response?