2017-01-21 29 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