Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用字节数组写入数据_Ios_Arrays_Swift_Byte - Fatal编程技术网

Ios 使用字节数组写入数据

Ios 使用字节数组写入数据,ios,arrays,swift,byte,Ios,Arrays,Swift,Byte,我目前正在尝试使用corebluetooth将7字节的数据从中心发送到外围设备,我的代码如下: func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) { connectingPeripheral = peripheral for characteristic in service.characteristics as

我目前正在尝试使用corebluetooth将7字节的数据从中心发送到外围设备,我的代码如下:

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)
但仍然无法发送数据。有人知道为什么吗?

试试这个:

let value: [UInt8] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
let data = NSData(bytes: value, length: 7)

下面是对Swift 5稍加修改的解决方案

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

嗨,我试过你的代码,但还是一样,我不能从另一方面得到任何价值。你能更新你的问题以更有效地概述你的实际问题吗?在其当前形式中,您只是问如何定义UINT8数组。您好,是的,我已经更新了我的问题。但对于数组,我仍然无法发送数据,7字节的数据。Mark的答案应该是有效的。您是否尝试过用WithoutResponse选项替换您的书面通话?我在使用WithResponse选项写入不太稳定的设备时遇到问题。您好,您只需将选项从WithResponse更改为WithoutResponse即可工作?您能否添加与连接到外围设备并发现其服务和特征相关的所有代码?此外,您从何处获取ConnectionCharacteristicWrite?您好,是的,我已经编辑了我的问题你的特征是什么?它有写权限吗?