我正在开发一款利用Core Bluetooth的Appcelerator模块。我可以毫无问题地连接到外设,并订阅特性。我已经谴责了所有委托函数,并且所有内容似乎都按预期触发,除了在尝试读取或写入时我得到“不太可能的错误”。我的核心方法如下所示。
-(void)readValueForCharacteristicByUUID:(NSString *)uuidstring
{
if (self.connectedService)
{
CBMutableCharacteristic* characteristic = [self characteristicFromUUIDstring:uuidstring];
if (characteristic)
{
[self.connectedPeripheral readValueForCharacteristic:characteristic];
}
} else {
NSLog(@"[INFO] No service to read");
}
}
-(void)writeValue:(NSData*)value ForCharacteristicByUUID:(NSString*)uuidstring
{
if (self.connectedService)
{
CBMutableCharacteristic* characteristic = [self characteristicFromUUIDstring:uuidstring];
if (characteristic)
{
[self.connectedPeripheral writeValue:value forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
} else {
NSLog(@"[INFO] No service to write to");
}
}
-(CBMutableCharacteristic*)characteristicFromUUIDstring:(NSString*)uuidstring
{
for (CBMutableCharacteristic* c in self.connectedService.characteristics)
{
if ([c.UUID.UUIDString isEqualToString:uuidstring])
{
return c;
}
}
NSLog(@"No Characteristic found with that uuid");
}
我认为它可能是权限问题,所以这里是我用来在我的外围设备中创建特征的代码。
CBMutableCharacteristic* c = [[CBMutableCharacteristic alloc] initWithType:cuuid
properties:CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite|
CBCharacteristicPropertyNotify
value:nil
permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];
答案 0 :(得分:2)
来自CBMutableCharacteristic class reference
CBMutableCharacteristic对象代表a的特征 本地外围设备的服务(代表本地外围设备) 通过CBPeripheralManager对象)。
您无法使用process.stdout
实例和CBMutableCharacteristic
与远程外围设备进行通信。
在连接的CBCentralManager
上调用CBCharacteristic
后,您必须使用返回到外围设备peripheral:didDiscoverCharacteristicsForService:error:
委托方法的discoverCharacteristics:forService:
。