Vik*_*yal 8 iphone bluetooth ios bluetooth-lowenergy swift
我试图在特征值改变时从蓝牙设备获得通知.为此,我需要启用客户端特性配置(CCC)描述符的通知.我已经使用setNotifyValue(enabled: Bool, forCharacteristic characteristic: CBCharacteristic)了特性但没有获得值更改的更新.
我试图启用CCC的指示,writeValue(data: NSData, forDescriptor descriptor: CBDescriptor)但我的应用程序崩溃了这个API并显示错误为
无法使用此方法编写客户端特征配置描述符!
任何帮助!!
提供更多代码可能有助于提高答案的准确性;但是,我们假设您已经能够发现所有特征值。通常,您只需要在实现中根据每个客户端特征配置(CCC)描述符迭代所有特征并设置/写入值CBPeripheralDelegate。
下面附上一个例子:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (error) {
NSLog(@"Error discovering characteristics: %@", error);
return;
}
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBManager accelDataUUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
} else if ([characteristic.UUID isEqual:[CBManager accelConfigUUID]]) {
[peripheral writeValue:[CBManager switchData:YES]
forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
}
//... if you have more to iterate
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1301 次 |
| 最近记录: |