繁体   English   中英

Raspberrypi从蓝牙接收数据

[英]Raspberrypi receiving data from bluetooth

因此,目前我正在研究iOS应用程序与树莓派之间的接口,其中pi通过蓝牙从应用程序接收信息。 现在,我可以运行我的应用程序,连接到pi并发送数据。

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

很抱歉提出这样一个模糊的问题,但我似乎找不到任何类似的帮助。

非常感谢!

首先,您必须知道所使用的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM