我有一个带有蓝牙双重类型的蓝牙设备,虽然从设备读取数据没有问题,但是当我使用CBCharacteristicWriteWithResponse或CBCharacteristicWriteWithoutResponse写入值时,它不会更改设备上的值。 对于android,无论是否具有响应特征类型,它的工作状况都很好。
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
我已设置为YES。 我没有发送超过20个字节的数据。 还是同样的问题。
if ([command isKindOfClass:[NSData class]]) {
NSData *cmdData = (NSData *)command;
command = [[NSString alloc] initWithData:cmdData encoding:NSASCIIStringEncoding];
}
command = [command stringByAppendingString:@"\r\n"];
while ([command length] != 0) {
NSString* str = command;
if ([command length] > 20) {
str = [command substringToIndex:20];
}
DebugLog(@"SENT:%@",str);
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
[self.discoveredPeripheral writeValue:data forCharacteristic:self.selectedCharacteristic type:CBCharacteristicWriteWithResponse];
command = [command substringFromIndex:[str length]];
}
我正在使用此方法进行调试。
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error) {
DebugLog(@"didWriteValueForCharacteristic ERROR: %@",error);
}
else {
DebugLog(@"didWriteValueForCharacteristic: %@",characteristic.UUID.UUIDString);
}
}
在编写特征时遇到此错误。
error NSError * domain: @"CBATTErrorDomain" - code: 3
0x14db57d0 _userInfo __NSDictionaryI * 1 key/value pair
0x14d66550 [0] (null)
@"NSLocalizedDescription" : @"Writing is not permitted." key
__NSCFConstantString * @"NSLocalizedDescription" 0x376b3cf4 value
__NSCFConstantString * @"Writing is not permitted." 0x37d109e4