如何转换十六进制为蓝牙LE写入特性Swift 4

时间:2017-10-04 15:11:47

标签: swift bluetooth hex bluetooth-lowenergy

在BLE中,将0x04作为数据值传递给外设特性需要什么功能?我可以传递什么int值来获取字节0x04?

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

    if let characteristics = service.characteristics {

        // I WANT TO WRITE 0x04 TO THE PERIPHERAL USING enableBytes below
        // enableByte MUST BE A SINGLE BYTE

        var enableValue:UInt8 = 4
        let enableByte = Data(bytes: &enableValue, count: MemoryLayout<UInt8>.size)
        // This returns 04
        // print("enableByte is \(enableByte as NSData)")

        // writing to Characteristics
        for characteristic in characteristics {

            if characteristic.uuid == Device.OnOffUUID {
                self.peripheral?.setNotifyValue(true, for: characteristic)

                self.peripheral?.writeValue(enableByte, for: characteristic, type: .withResponse)

                peripheral.readValue(for: characteristic) // this will call peripheral(_: didUpdateValueFor characteristic)
            }
        }
    }
}

0 个答案:

没有答案