ios - CBPeripheral setNotifyValue WithResponse 不调用 didWriteValueForCharacteristic

标签 ios swift core-bluetooth bluetooth-lowenergy

我有一个 BLE 基金可以将数据写入外围服务:

func writeData(data: [Int8]) {
    let character = CBMutableCharacteristic(type: cuivienenCharacterUUID, properties: [.Write, .Read, .Notify], value: nil, permissions: [.Readable, .Writeable])
    test_peripheral.writeValue(NSData(bytes: data, length: data.count), forCharacteristic: character, type: .WithResponse)
    test_peripheral.setNotifyValue(true, forCharacteristic: character)
}

我已经初始化了 test_peripheral 并将其委托(delegate)设置为 self。但是test_peripheral.writeValue从未打过电话func peripheral(peripheral: CBPeripheral, didWriteValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) 。我连错误消息都收不到!

test_peripheral.setNotifyValue也是如此。它从不调用func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?)

我的代码有什么问题吗?

最佳答案

CB特性

test_peripheral.writeValue(NSData(bytes: data, length: data.count), forCharacteristic: character, type: .WithResponse)

不应由您自己的代码初始化。您应该在

中找到您的 CBCharacteristic
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)

然后使用它。

关于ios - CBPeripheral setNotifyValue WithResponse 不调用 didWriteValueForCharacteristic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34336949/

相关文章:

ios - 将 iOS 应用内订阅链接到 MySQL 数据库

objective-c - 将 MapView 委托(delegate)设置为 self 时 showUserLocation 发生变化

swift - 如何调整UITextField.leftView的大小?

ios - 查看 EkReminder 重复频率的最佳方式?

ios - ios 中的回调未返回进行身份验证

java - 通过 JNI 从 Java 使用 CoreBluetooth 时未调用 didDiscoverCharacteristics

ios - iOS 上的低功耗蓝牙数据传输

ios - 由于使用社交登录,Apple 拒绝了我的应用

ios - Swift 3.0 标签空白(零值?)

ios - 连接 BLE 时在后台运行的推送通知应用程序