ios – 订阅来自CBC特性的通知不起作用

ios – 订阅来自CBC特性的通知不起作用,第1张

概述首先要做的事:运行OSX 10.10.4,iOS 4, Xcode 6.3.2,iPhone 6, Swift 短篇小说:我在这里有一个蓝牙LE设备,我希望在特征值发生变化时收到通知,例如:通过用户输入.尝试订阅它不会成功,而是产生错误错误域= CBATTErrorDomain代码= 10“找不到该属性.” 长话:所以,我有一个BluetoothManager类,在我的$CBCentralMana 首先要做的事:运行OSX 10.10.4,iOS 4,Xcode 6.3.2,iPhone 6,Swift

短篇小说:我在这里有一个蓝牙LE设备,我希望在特征值发生变化时收到通知,例如:通过用户输入.尝试订阅它不会成功,而是产生错误错误域= CBATTErrorDomain代码= 10“找不到该属性.”

长话:所以,我有一个BluetoothManager类,在我的$CBCentralManager.state是.PoweredOn时,我开始扫描@R_301_3263@.这很容易,我甚至是一个好公民,专门为有我想要的服务的人扫描

centralManager.scanFor@R_301_3263@WithServices([ServiceUUID],options: nil)

希望这会成功,我实现了以下委托方法:

func centralManager(central: CBCentralManager!,dIDdiscoverPeripheral peripheral: CBPeripheral!,advertisementData: [NSObject : AnyObject]!,RSSI: NSNumber!) {    if *this is a kNown device* {        connecttoperipheral(peripheral)        return    }    [...] // varIoUs stuff to make something a kNown device,this works}

所以继续前进,我们得到:

func connecttoperipheral(peripheral: CBPeripheral) {    println("connecting to \(peripheral.IDentifIEr)")    [...] // saving the peripheral in an array along the way so it is being retained    centralManager.connectPeripheral(peripheral,options: nil)}

Yupp,这成功了,所以我得到确认并开始发现服务:

func centralManager(central: CBCentralManager!,dIDConnectPeripheral peripheral: CBPeripheral!) {            println("Connected \(peripheral.name)")    peripheral.delegate = self    println("connected to \(peripheral)")    peripheral.discoverServices([BluetoothConstants.MY_SERVICE_UUID])}

这也有效,因为该委托方法也被调用:

func peripheral(peripheral: CBPeripheral!,dIDdiscoverServices error: NSError!) {    if peripheral.services != nil {        for service in peripheral.services {            println("discovered service \(service)")            let serviceObject = service as! CBService            [...] // discover the Characteristic to send controls to,this works            peripheral.discovercharacteristics([BluetoothConstants.MY_CHaraCTERISTIC_NOTIFICATION_UUID],forService: serviceObject)           [...] // Some unneccessary stuff about command caches        }    }}

你知道什么:特征被发现了!

func peripheral(peripheral: CBPeripheral!,dIDdiscovercharacteristicsForService service: CBService!,error: NSError!) {    for characteristic in service.characteristics {        let castCharacteristic = characteristic as! CBCharacteristic        characteristics.append(castCharacteristic) // Retaining the characteristic in an Array as well,not sure if I need to do this        println("discovered characteristic \(castCharacteristic)")        if *this is the control characteristic* {            println("control")        } else if castCharacteristic.UUID.UUIDString == BluetoothConstants.MY_CHaraCTERISTIC_NOTIFICATION_UUID.UUIDString {            println("notification")            peripheral.setNotifyValue(true,forCharacteristic: castCharacteristic)        } else {            println(castCharacteristic.UUID.UUIDString) // Just in case        }        println("following propertIEs:")        // Just to see what we are dealing with        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.broadcast) != nil {            println("broadcast")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.Read) != nil {            println("read")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.WriteWithoutResponse) != nil {            println("write without response")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.Write) != nil {            println("write")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.Notify) != nil {            println("notify")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.Indicate) != nil {            println("indicate")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.AuthenticatedSigneDWrites) != nil {            println("authenticated signed writes ")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.ExtendedPropertIEs) != nil {            println("indicate")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.NotifyEncryptionrequired) != nil {            println("notify encryption required")        }        if (castCharacteristic.propertIEs & CBCharacteristicPropertIEs.IndicateEncryptionrequired) != nil {            println("indicate encryption required")        }        peripheral.discoverDescriptorsForCharacteristic(castCharacteristic) // Do I need this?    }}

现在控制台输出到这里看起来像这样:

connected to <CBPeripheral: 0x1740fc780,IDentifIEr = $FOO,name = $Somename,state = connected>discovered service <CBService: 0x170272c80,isPrimary = YES,UUID = $bar>[...]discovered characteristic <CBCharacteristic: 0x17009f220,UUID = $barbar propertIEs = 0xA,value = (null),notifying = NO>controlfollowing propertIEs:readwrite[...]discovered characteristic <CBCharacteristic: 0x17409d0b0,UUID = $BAZBAZ,propertIEs = 0x1A,notifying = NO>notificationfollowing propertIEs:readwritenotify[...]discovered DescriptorsForCharacteristic[]updateNotification: false

嘿!它说updateNotification是假的.它来自哪里?为什么,这是我对setNotify的回调…:

func peripheral(peripheral: CBPeripheral!,dIDUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic!,error: NSError!) {    println("updateNotification: \(characteristic.isnotifying)")}

是什么赋予了?我告诉它要通知!为什么不通知?让我们在println的行中设置一个断点并检查错误对象:

(lldb) po errorError Domain=CBATTErrorDomain Code=10 "The attribute Could not be found." UserInfo=0x17026eac0 {NSLocalizedDescription=The attribute Could not be found.}

好的,所以这让我没有想法.我无法找到有关该错误代码的相关线索.自从我尝试为之前发现的特征设置通知以来,我无法理解描述本身,因此它必须存在,对吧?此外,在AndroID上似乎可以订阅通知,所以我想我可以排除设备的问题……或者我可以吗?有关这方面的任何线索都非常感谢!

解决方法 对我来说问题是我使用另一个AndroID设备作为外围设备并且需要实现配置描述符.看这里:
https://stackoverflow.com/a/25508053/599743 总结

以上是内存溢出为你收集整理的ios – 订阅来自CBC特性的通知不起作用全部内容,希望文章能够帮你解决ios – 订阅来自CBC特性的通知不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100385.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存