【发布时间】:2015-03-13 10:47:21
【问题描述】:
我正在开发一个与蓝牙 LE 设备通信的应用程序,因此我正在使用 CoreBluetooth 来执行此操作。
我正在使用的外围设备公开了 1 个服务,该服务具有两个特性:一个支持指示/通知/写入/写入无响应的串行端口 FIFO 特性;和一个支持写的串口信用特性。
从我的阅读中,我了解到我需要订阅的特性是 FIFO 特性,但是当我调用 [_connectedPeripheral setNotifyValue:YES forCharacteristic:characteristic]; 时,我收到了 Writing is not permitted 错误。
我检查了特征的属性,它具有Read、WriteWithoutResponse、Notify 和Indicate 属性。
这是我第一次使用 CoreBluetooth,而且我有点蓝牙菜鸟,所以这可能很明显我忽略了,但任何帮助将不胜感激。
编辑:这是代码:
#define SDL440S_SERVICE @"2456E1B9-26E2-8F83-E744-F34F01E9D701"
#define SDL440S_CHARACTERISTIC @"2456E1B9-26E2-8F83-E744-F34F01E9D703"
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect %@", peripheral.name);
// _connectedPeripheral is a property...
if (_connectedPeripheral != peripheral) {
_connectedPeripheral = peripheral;
}
_connectedPeripheral.delegate = self;
[_connectedPeripheral discoverServices:@[[CBUUID UUIDWithString:SDL440S_SERVICE]]];
}
#pragma mark - <CBPeripheralManager>
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (peripheral == _connectedPeripheral) {
for (CBService *service in _connectedPeripheral.services) {
if ([service.UUID.UUIDString isEqualToString:SDL440S_SERVICE]) {
[_connectedPeripheral discoverCharacteristics:@[[CBUUID UUIDWithString:SDL440S_CHARACTERISTIC]] forService:service];
}
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID.UUIDString isEqualToString:SDL440S_CHARACTERISTIC]) {
// Subsribe to characteristic
[_connectedPeripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
// Getting values
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error) {
NSLog(@"Error changing note state for char: %@.\nError: %@", characteristic, error.localizedDescription);
}
}
【问题讨论】:
-
你是如何获得
characteristic的?来自didDiscoverCharacteristics委托方法? -
没错。我发现了我感兴趣的外围设备,然后发现了我感兴趣的服务,然后服务的特征使用对特征的引用来订阅通知。
-
我正在使用由制造组件here的公司定义的协议@
-
哪个委托方法给你错误信息?。写入错误对于 setNotify 操作没有意义。
-
这是
didUpdateNotificationStateForCharacteristic:error:代表CBPeripheralDelegate:- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if (error) { NSLog(@"Error changing note state for char: %@.\nError: %@", characteristic, error.localizedDescription); } }
标签: ios objective-c bluetooth bluetooth-lowenergy core-bluetooth