【问题标题】:Swift BLE peripheral writeValue is not workingSwift BLE 外设 writeValue 不起作用
【发布时间】:2015-03-07 01:55:31
【问题描述】:

我当前的代码是:

@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)

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

【问题讨论】:

  • 当您说值没有改变时,您如何验证这一点?从接收外围设备还是您只是在发送中心检查CBCharacteristic 的值?
  • 您在peripheral:didWriteValueForCharacteristic:error:CBPeripheralDelegate 方法)中遇到什么错误?
  • @Paulw11 我的意思是我的数据没有显示在接收外围设备中。我有一个可以更新屏幕数据的外围接收器。
  • @MichałCiuba 这个函数甚至没有被执行。
  • 如果你写withoutResponseperipheral:didWriteValueForCharacteristic:error:不应该被调用。试试withResponsesendDataCharacteristic是怎么设置的?

标签: swift core-bluetooth


【解决方案1】:
peripheral.writeValue(data, forCharacteristic: characteristic, type: 
 CBCharacteristicWriteType.WithResponse)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2017-04-23
    • 2022-06-25
    • 1970-01-01
    • 2020-01-19
    • 2017-02-08
    • 2017-06-24
    相关资源
    最近更新 更多