Reputation: 133
I am working on an Appcelerator module that utilizes Core Bluetooth. I can connect to a peripheral without issue, and subscribe to a characteristic. I have implimented all delegate functions and everything appears to be firing as expected, except that I get an 'unlikely error' when attempting to read or write. My central methods are shown below.
-(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");
}
I am thinking it may be a permissions issue, so here is the code I use to create the characteristics in my peripheral.
CBMutableCharacteristic* c = [[CBMutableCharacteristic alloc] initWithType:cuuid
properties:CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite|
CBCharacteristicPropertyNotify
value:nil
permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];
Upvotes: 1
Views: 1536
Reputation: 115051
From the CBMutableCharacteristic class reference
CBMutableCharacteristic objects represent the characteristics of a local peripheral’s service (local peripheral devices are represented by CBPeripheralManager objects).
You can't use a CBMutableCharacteristic
instance with a CBCentralManager
to communicate with a remote peripheral.
You have to use the CBCharacteristic
that is returned to the peripheral's peripheral:didDiscoverCharacteristicsForService:error:
delegate method after calling discoverCharacteristics:forService:
on the connected CBPeripheral
.
Upvotes: 2