【问题标题】:Write error to ble characteristics in iOS在 iOS 中将错误写入 ble 特征
【发布时间】:2018-04-23 21:57:04
【问题描述】:

我尝试写入 ble 特征。但返回错误是“不允许写入”。

这是为了发现特征。 // 当您发现指定服务的特征时调用。

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if ([service.UUID isEqual:[CBUUID UUIDWithString:DEVICE_SERVICE_UUID]])  {
        for (CBCharacteristic *aChar in service.characteristics)
        {
            if ([aChar.UUID isEqual:dev2appcharacteristicsUUID]) {
                [devicePeripheral setNotifyValue:YES forCharacteristic:aChar];
                NSLog(@"Found R-Motion device with characteristics: %@\n", aChar);
                dev2appCharacteristic = aChar;
            }
            if ([aChar.UUID isEqual:app2devcharacteristicsUUID]) {
                //[devicePeripheral setNotifyValue:NO forCharacteristic:aChar];
                NSLog(@"Found R-Motion device with characteristics: %@\n", aChar);
                app2devCharacteristic = aChar;
            }
        }
    }
}

我可以正确发现。并尝试写入 app2devCharacteristic。 它有_properties unsigned long long 4_isNotifying bool false

我尝试将字节数组写为

- (IBAction)RequestResult:(id)sender {
    unsigned char bytes[] = {0x00, 0xCC, 0xFF};//{0xFF, 0xCC, 0x00};//
    NSData *transactData = [NSData dataWithBytes:bytes length:3];
    [devicePeripheral writeValue:transactData forCharacteristic:app2devCharacteristic type:CBCharacteristicWriteWithResponse];

}

我已经返回didWriteValueForCharacteristic,但出现错误"Writing is not permitted"

如果我改成无响应为

- (IBAction)RequestResult:(id)sender {
    unsigned char bytes[] = {0x00, 0xCC, 0xFF};//{0xFF, 0xCC, 0x00};//
    NSData *transactData = [NSData dataWithBytes:bytes length:3];
    [devicePeripheral writeValue:transactData forCharacteristic:app2devCharacteristic type:CBCharacteristicWriteWithoutResponse];

} 

没有返回错误。但是我的外围设备没有收到任何东西。

【问题讨论】:

  • 您说_properties unsigned long long 4 代表app2devCharacteristic。好吧,CBCharacteristicPropertyWriteWithoutResponse = 0x04, 所以你显然不能在writeValue:forCharacteristic:type:type 中做CBCharacteristicWriteWithResponse。剩下的(没有写),你怎么知道的? transactData 有效吗?你的设备真的会这样吗?

标签: ios iphone bluetooth-lowenergy core-bluetooth ios-bluetooth


【解决方案1】:

外围设备为每个特征设置权限。读写权限是分开的。不允许写入只是意味着无论加密级别如何,都不允许您对其进行写入。

如果您使用 write without response,则不会根据规范传递错误消息。

编辑:嗯,假设 _properties 参考蓝牙核心规范第 3 卷第 G 部分第 3.3.1.1 节 a 4 中的特性属性表,表示应允许无响应写入,但不允许有响应写入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多