无法向 BLE 外设 (ZL-RC04A) 设备发送数据
Posted
技术标签:
【中文标题】无法向 BLE 外设 (ZL-RC04A) 设备发送数据【英文标题】:Not able to send data to BLE Peripheral (ZL-RC04A) device 【发布时间】:2017-08-09 13:55:08 【问题描述】:我正在尝试使用代码 sn-p 写入数据。
- (void)peripheral:(CBPeripheral *)peripheral1 didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
// Again, we loop through the array, just in case.
for (CBCharacteristic *characteristic in service.characteristics)
// And check if it's the right one
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]])
NSString *payloadMessage = @"3N";
NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
[_discoveredPeripheral discoverDescriptorsForCharacteristic:characteristic];
[_discoveredPeripheral writeValue:payload forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
[_discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];
但出现错误
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
作为:
Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo=NSLocalizedDescription=Writing is not permitted.
虽然同样适用于 android。
【问题讨论】:
那个characteristic
的properties
是什么?
CBCharacteristicPropertyRead = 0x02, CBCharacteristicPropertyWriteWithoutResponse = 0x04, CBCharacteristicPropertyNotify = 0x10,
所以你不能做CBCharacteristicWriteWithResponse
(不包括CBCharacteristicPropertyWrite = 0x08,
),你需要用CBCharacteristicWriteWithoutResponse
来做。
【参考方案1】:
如果您对特征使用了错误的写入类型(假设该特征毕竟是可写的),则会出现该错误。向特征写入数据有两种类型:
CBCharacteristicWriteWithResponse
:在这种情况下,您将收到外设的确认数据包。您可以将其视为 TCP 数据包。
CBCharacteristicWriteWithoutResponse
:这是一种“一劳永逸”的写作方式。您可以将其视为 UDP 数据包。
因此,请尝试使用CBCharacteristicWriteWithoutResponse
而不是CBCharacteristicWriteWithResponse
。如果这不起作用,您可能必须检查您的特征是否是可写的。
【讨论】:
我也试过 CBCharacteristicWriteWithoutResponse ,但它也不起作用。如何知道 CBCharacteristic 是否可写?但它在android中工作,这意味着我猜它必须是可写的。 当 WithResponse 不允许时,似乎正是 WithResponse 的情况:***.com/questions/45592772/… 使用CBCharacteristicWriteWithoutResponse
时遇到什么错误?
你能不能if (characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) NSLog(@"Able to write without response"); if (characteristic.properties & CBCharacteristicPropertyWriteWithResponse) NSLog(@"Able to write with response");
告诉我们你得到了什么?同样正如@Jens Meder 所建议的那样,错误是否相同而没有响应?还是发送的数据格式错误?
@JensMeder 没有收到任何错误,因为没有回调。实际上我有带 LED 的继电器,当输入为“3N”时它会启发。以上是关于无法向 BLE 外设 (ZL-RC04A) 设备发送数据的主要内容,如果未能解决你的问题,请参考以下文章