尝试订阅CBC特性时,不允许写入错误

时间:2015-03-13 10:47:21

标签: ios objective-c bluetooth bluetooth-lowenergy core-bluetooth

我正在开发一个与蓝牙LE设备通信的应用程序,因此我使用CoreBluetooth来执行此操作。

我使用的外设有1个服务,它有两个特性,一个串口FIFO特性,支持指示/通知/写/无响应;以及支持写入的串口信用特征。

从我的阅读中我得知,我需要订阅的特性是FIFO,但是当我拨打[_connectedPeripheral setNotifyValue:YES forCharacteristic:characteristic];时,我收到Writing is not permitted错误。

我检查了特征的属性,它具有ReadWriteWithoutResponseNotifyIndicate属性。

这是我第一次使用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);
    }
}

1 个答案:

答案 0 :(得分:0)

如果有人遇到此问题,以便日后参考,那就是设备的 信用 特征。

我感兴趣的服务有两个特征: 信用 特征和 FIFO 特征。 (我知道信用特征是标准问题。)问题是我必须在我接收和写入数据到FIFO特性之前,将我想要接收的最大数据量写入信用特征。

即:

unsigned char buf[1] = {/*relevant data here...*/};
NSData *data = [NSData dataWithBytes:buf length:1];
[peripheral writeValue:data forCharacteristic:creditCharacteristic type:CBCharacteristicWriteWithoutResponse]; 
相关问题