gpt4 book ai didi

ios - CoreBluetooth用字节数组写入数据

转载 作者:行者123 更新时间:2023-12-01 22:29:56 25 4
gpt4 key购买 nike

我目前正在尝试使用corebluetooth从Central向Peripheral发送7bytes的数据,我的代码如下:

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {

connectingPeripheral = peripheral
for characteristic in service.characteristics as [CBCharacteristic]!{

if characteristic.UUID.UUIDString == "2AF2"{
connectingCharacteristicWrite = characteristic
}

@IBAction func Button(sender: UIButton) {
let value: [UInt8] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
let data = NSData(bytes: value, length: 7)
peripheral.setNotifyValue(true, forCharacteristic: characteristic)
connectingPeripheral.writeValue(data, forCharacteristic: connectingCharacteristicWrite, type: CBCharacteristicWriteType.WithoutResponse)
}

但是我只能发送一个字节。我尝试下面的代码行:
let value: [UInt8] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
let data = NSData(bytes: value, length: 7)
connectingPeripheral.writeValue(data, forCharacteristic: connectingCharacteristicWrite, type: CBCharacteristicWriteType.WithResponse)

但仍然无法发送数据。有人知道为什么吗?

最佳答案

这是Swift 5的稍微修改的解决方案

let value: [UInt8] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
connectingPeripheral.writeValue(Data(value), for: connectingCharacteristicWrite, type: .withResponse)

关于ios - CoreBluetooth用字节数组写入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35480804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com