iOS版。 BLE。 CBCharacteristicWriteWithoutResponse - 如何让它工作?

时间:2014-01-31 01:30:14

标签: ios

这是我在这里提出的第一个问题,请不要向我收取太多费用。 我想建议如何使用CBCharacteristicWriteWithoutResponse从一台iOS设备向另一台设备发送少量字节。

我确实尝试使用writeValue发送数据:forCharacteristic:type:来自一个iOS设备(中心角色) 并在另一个iOS设备上使用peripheralManager:didReceiveWriteRequests:接收它(外围角色)。

我对类型没有问题:CBCharacteristicWriteWitResponse,但如果我用类型替换它:CBCharacteristicWriteWithoutResponse外设停止接收它。怎么了?我是否遗漏了某些东西或CBCentralManagerDelegate不支持WriteWithoutResponse?

这里有一些代码:

@interface BleCentralDemo : NSObject <CBCentralManagerDelegate> {}
@end

@implementation BleCentralDemo

-(void) writeValue:(int)sUUID characteristicUUID:(int)cUUID p:(CBPeripheral *)p data:(NSData *)data {
[p writeValue:data forCharacteristic:cUUID type:CBCharacteristicWriteWithResponse]; // sent, received
[p writeValue:data forCharacteristic:cUUID type:CBCharacteristicWriteWithOutResponse]; // sent, not received
}
@end

@interface BlePeripheralDemo : NSObject <CBPeripheralManagerDelegate> {}
@end

@implementation BlePeripheralDemo

-(void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests {
 for (CBATTRequest *request in requests){
 NSLog(@"request.value:%@", request.value); 
 [peripheral respondToRequest:request withResult:CBATTErrorSuccess];
 }
}
@end

1 个答案:

答案 0 :(得分:1)

我今天遇到了同样的问题。

你应该用CBCharacteristicPropertyWriteWithoutResponse属性设置你的特性。(代码中的cUUID)

我的特点是来自外围设备。像那样创造

[[CBMutableCharacteristic alloc] initWithType:writeableCharacteristicUUID properties:CBCharacteristicPropertyWrite | CBCharacteristicPropertyNotify | CBCharacteristicPropertyWriteWithoutResponse value :nil permissions:CBAttributePermissionsWriteable];

希望有所帮助