Reputation: 413
So, currently I've been working on an interface between an iOS app and a raspberry pi where the pi receives information from the app through bluetooth. Right now I have my app working, connecting to the pi, and sending data.
The only problem I'm having is that I have no idea on how to read in that data from the pi. I'm using python to attempt to read in the data, and just have no idea on where to start. What port does bluetooth run on (RPi3)? How would I connect to that port to receive the input?
Sorry for such a vague question, but I can't seem to find anything similar to help.
Thank you so much!
Upvotes: 0
Views: 924
Reputation: 182
First,you have to know what kind of characteristic properties
you used.
type 1:CBCharacteristicPropertyNotify
In this way , you must set Notify for the service of characteristics.
for example :
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error{
if (error) {
NSlog(@"error:%@",error.localizedDescription);
return ;
}
for (CBCharacteristic *characteristic in service.characteristics) {
if (characteristic.properties & CBCharacteristicPropertyNotify) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
type 2:CBCharacteristicPropertyRead
or other
In this way , you must read the characteristic value after send data succeed.
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error{
if (error) {
NSlog(@"error:%@",error.localizedDescription);
return ;
}
if (!(characteristic.properties & CBCharacteristicPropertyNotify)) {
[peripheral readValueForCharacteristic:characteristic];
}
}
after that ,you can receive data :
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error{
if (error) {
NSlog(@"error:%@",error.localizedDescription);
return ;
}
NSlog(@"characteristic value = %@",characteristic.value);
uint8_t *data = (uint8_t *)[characteristic.value bytes];
NSMutableString *temStr = [[NSMutableString alloc] init];
for (int i = 0; i < characteristic.value.length; i++) {
[temStr appendFormat:@"%02x ",data[i]];
}
NSlog(@"receive value:%@",temStr);
}
You may find something help with this demo:https://github.com/arrfu/SmartBluetooth-ios-objective-c
Upvotes: 1