Swift BLE外围写值不工作
我目前的代码是:Swift BLE外围写值不工作,swift,core-bluetooth,Swift,Core Bluetooth,我目前的代码是: @IBAction func sendData(sender: UISwitch) { if advertisingSwitch.on { var parameter = NSInteger(45) let data = NSData(bytes: ¶meter, length: 1) if let connectedPeripheral = discoveredPeripheral {
@IBAction func sendData(sender: UISwitch) {
if advertisingSwitch.on {
var parameter = NSInteger(45)
let data = NSData(bytes: ¶meter, 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的服务扫描。我确信我得到的特性具有函数“write”,即
<CBCharacteristic: 0x174086090, UUID = FFE9, properties = 0x8, value = (null), notifying = NO>
特征值不变。我不知道我哪里出错了
peripheral.writeValue(data, forCharacteristic: characteristic, type:
CBCharacteristicWriteType.WithResponse)
尝试
。使用response
并检查什么是来自外围设备的响应。当您说值没有变化时,您如何验证这一点?从接收外围设备或您只是在发送中心检查CBCharacteristic
的值?在外围设备:didWriteValueForCharacteristic:error:
(cbperipheraldegate
方法)中会出现什么错误?@Paulw11我的意思是数据没有显示在接收外围设备中。我有一个外设接收器,可以更新屏幕中的数据。@MichałCiuba该函数甚至没有执行。如果您在没有响应的情况下写入,则不应调用外设:didWriteValue for特性:错误:
。是否改为尝试使用响应?如何设置sendDataCharacteristic?
peripheral.writeValue(data, forCharacteristic: characteristic, type:
CBCharacteristicWriteType.WithResponse)