Read characteristic from RN4020 with CoreBluetooth

Home / ios / Read characteristic from RN4020 with CoreBluetooth

Question:
I’ve been working on a device using an rn4020 bluetooth module. We need to change the output and read the input from the device. I’ve written a code using CoreBluetooth and working on iOS to do that. I was able (after some struggling) to set the device in command mode and change the output.

Now what I want is reading the input (basically high and low level). To set the output I’m using the following code:-(void)OutputOn{
NSData *data = [@"|O,08,08" dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *mData = [NSMutableData dataWithData:data];
unsigned char f8[1] = "r";
[mData appendBytes:f8 length:1];
[self.discoveredPeripheral writeValue:mData forCharacteristic:self.MLDPDataCharacteristic type:CBCharacteristicWriteWithResponse];
}

-(void)OutputOff{
NSData *data = [@"|O,08,00" dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *mData = [NSMutableData dataWithData:data];
unsigned char f8[1] = "r";
[mData appendBytes:f8 length:1];
[self.discoveredPeripheral writeValue:mData forCharacteristic:self.MLDPDataCharacteristic type:CBCharacteristicWriteWithResponse];
}
Like I said this is working. Now for reading the input I’ve written this following code
-(void)ReadPIO2{
NSData *data = [@"|I,02" dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *mData = [NSMutableData dataWithData:data];
unsigned char f8[1] = "r";
[mData appendBytes:f8 length:1];
[self.discoveredPeripheral writeValue:mData forCharacteristic:self.MLDPDataCharacteristic type:CBCharacteristicWriteWithResponse];
}
Once I’ve received the callback "didWriteValueForCharacteristic" I’m using the following function
[peripheral readValueForCharacteristic:characteristic];
Before doing that I ensure that the characteristic is readable (which is the case). In the callback function "didUpdateValueForCharacteristic" I’ve always get the following error:

Update value callback Error Error Domain=CBATTErrorDomain Code=2 "Reading is not permitted." UserInfo={NSLocalizedDescription=Reading is not permitted.}

To sum up it seems that I’m not able to update the characteristic value because the read function always produce an error.

If I use the SmartData app from Microchip, I’m able to read the output with the command I’ve mentioned earlier (see attached picture).

Does someone have an idea on how to read the characteristic value?

Thanks

The result with SmartData app


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *