2017-01-21 60 views
0

因此,目前我一直在iOS应用程序和覆盆子pi之间的界面上工作,pi通过蓝牙从应用程序接收信息。现在我有我的应用程序工作,连接到pi,并发送数据。Raspberrypi从蓝牙接收数据

我唯一的问题是,我不知道如何从pi中读取数据。我正在使用python尝试读取数据,并且不知道从哪里开始。蓝牙运行在哪个端口(RPi3)?我将如何连接到该端口以接收输入?

对不起,这个模糊的问题,但我似乎无法找到类似的帮助。

非常感谢!

回答

1

首先,你必须知道你使用了什么样的characteristic properties

类型1:CBCharacteristicPropertyNotify

这样,你必须设置为通知特色的服务。

例如:

- (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]; 
     } 
    } 

} 

类型2:CBCharacteristicPropertyRead或其他

这样,你必须阅读的特征值后发送数据成功。

- (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]; 
    } 
} 

后,您可以接收数据:

- (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); 
} 

您可能会发现一些帮助,这演示:https://github.com/arrfu/SmartBluetooth-ios-objective-c

相关问题