首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法写入或读取CBPeripheral | CBMutableCharacteristic

无法写入或读取CBPeripheral | CBMutableCharacteristic
EN

Stack Overflow用户
提问于 2016-03-15 04:09:56
回答 1查看 470关注 0票数 0

我正在连接一个蓝牙硬件,但是我得不到通知,当一个值被更新时,我的代码如下:

代码语言:javascript
复制
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {

    guard peripheral == self.peripheral && error == nil else {
        return
    }

    if let characteristics = service.characteristics {
        for characteristic in characteristics {
            if characteristic.UUID == QuadrarCharacteristicUUID {
                if let peripheral = self.peripheral {
                    self.quadrarCharacteristic = CBMutableCharacteristic(type: QuadrarCharacteristicUUID,
                        properties: [.Read, .Write, .Notify],
                        value: nil,
                        permissions: [.Readable, .Writeable])
                    self.quadrarCharacteristic?.descriptors = characteristic.descriptors
                    peripheral.setNotifyValue(true, forCharacteristic: self.quadrarCharacteristic!)
                }
            }
        }
    }
}

当我写的时候:

代码语言:javascript
复制
 func writeBytes() {
           self.quadrarCharacteristic!.service.peripheral.writeValue(self.bytesToWriteArray[self.senderCounter], 
            forCharacteristic: self.quadrarCharacteristic!, type: CBCharacteristicWriteType.WithResponse)
    }

我知道,即使是写作也行不通因为

代码语言:javascript
复制
 func peripheral(peripheral: CBPeripheral, didWriteValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)

没有被称为..。但是如果我只使用

代码语言:javascript
复制
var quadrarCharacteristic: CBCharacteristic?

试着写它,作为一个非CBMutableCharacteristic,它可以工作,但我不能让通知工作…

EN

回答 1

Stack Overflow用户

发布于 2016-03-15 07:12:06

当您充当中心时,您不需要创建新的CBMutableCharacteristic,只需使用didDiscoverCharacteristicsForService中提供给您的CBCharacteristic对象-

代码语言:javascript
复制
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {

    guard peripheral == self.peripheral && error == nil else {
        return
    }

    if let characteristics = service.characteristics {
        for characteristic in characteristics {
            if characteristic.UUID == QuadrarCharacteristicUUID {
                self.quadrarCharacteristic=charateristic
                peripheral.setNotifyValue(true, forCharacteristic: self.quadrarCharacteristic!)
                }
            }
        }
    }
}

此外,您还需要确认您的特征是否支持通知。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35997111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档