Swift BLE外设writeValue不起作用

时间:2015-03-07 01:55:31

标签: swift core-bluetooth

我目前的代码是:

@IBAction func sendData(sender: UISwitch) {
    if advertisingSwitch.on {
        var parameter = NSInteger(45)
        let data = NSData(bytes: &parameter, length: 1)
        if let connectedPeripheral = discoveredPeripheral {
            println("========= In connected peripheral \(connectedPeripheral)")
            //println("========= Send data is \(currentSendData)")
            println("========= Characteristic is \(sendDataCharacteristic)")
            println("========= data length is \(data.bytes)")
            self.sendDataToCentral(connectedPeripheral, characteristic: sendDataCharacteristic!, data: data)
        }
    }
}

private func sendDataToCentral(peripheral: CBPeripheral, characteristic: CBCharacteristic, data: NSData) {
    println("data is \(data)")
    peripheral.writeValue(data, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)
    println("writed characteristic \(characteristic)")
}

当我检查外围设备时,它连接显示:

BPeripheral: 0x1700ee980, identifier = E2377588-84CB-87ED-570A-B51614287B3C, name = TAv22u-FDF1, state = connected

特征是从已知UUID的服务扫描中获取。我确信我得到的特性有功能"写",这是

<CBCharacteristic: 0x174086090, UUID = FFE9, properties = 0x8, value = (null), notifying = NO>
执行函数后

peripheral.writeValue(data, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)

特征中的价值没有变化。我不知道哪里出错了。

1 个答案:

答案 0 :(得分:0)

peripheral.writeValue(data, forCharacteristic: characteristic, type: 
 CBCharacteristicWriteType.WithResponse)

尝试.withResponse并检查外围设备的响应是什么。