[英]Writing is not permitted error when attempting to subscribe to CBCharacteristic
I'm developing an App that communicates with a Bluetooth LE device and so I'm using CoreBluetooth to do this. 我正在开发一个与Bluetooth LE设备进行通信的应用程序,因此我正在使用CoreBluetooth来做到这一点。
The peripheral I'm using exposes 1 service which has two characteristics a serial port FIFO characteristic which supports indication/notification/write/write no response; 我正在使用的外围设备公开了一项服务,该服务具有两个特征,一个串行FIFO特征,该特征支持指示/通知/写/写无响应; and a serial port credit characteristic which supports write.
以及支持写操作的串行端口信用特性。
From my reading I gather that the characteristic that I need to subscribe to is the FIFO one but when I call [_connectedPeripheral setNotifyValue:YES forCharacteristic:characteristic];
从我的阅读中,我了解到我需要订阅的特征是FIFO,但是当我调用
[_connectedPeripheral setNotifyValue:YES forCharacteristic:characteristic];
I get a Writing is not permitted
error. 我收到
Writing is not permitted
错误。
I checked the characteristic's properties and it has the Read
, WriteWithoutResponse
, Notify
, and Indicate
properties. 我检查了特征的属性,它具有
Read
, WriteWithoutResponse
, Notify
和Indicate
属性。
This is the first time I've used CoreBluetooth and I'm a bit of a bluetooth noob so it's probably something obvious I've overlooked but any help would be much appreciated. 这是我第一次使用CoreBluetooth,而且我有点像蓝牙菜鸟,所以我可能忽略了这很明显的事情,但是任何帮助将不胜感激。
EDIT: Here's the code: 编辑:这是代码:
#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);
}
}
For future reference if anyone comes across this problem, it was to do with the device's credit characteristic. 供将来参考,如果有人遇到此问题,则与设备的信用特征有关。
The service which I was interested in exposed two characteristics a credit characteristic and a FIFO characteristic. 我感兴趣的服务暴露了两个特性: 信用特性和FIFO特性。 (I understand that a credit characteristic is standard issue.) The problem was that I had to write the maximum amount of data that i wanted to receive to the credit characteristic before i could then receive and write data to the FIFO characteristic.
(我知道信用特征是标准问题。)问题是,我必须先将要接收的最大数据量写入信用特征,然后才能接收数据并将其写入FIFO特征。
Ie: 即:
unsigned char buf[1] = {/*relevant data here...*/};
NSData *data = [NSData dataWithBytes:buf length:1];
[peripheral writeValue:data forCharacteristic:creditCharacteristic type:CBCharacteristicWriteWithoutResponse];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.