树莓派接收蓝牙数据

Raspberrypi receiving data from bluetooth

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

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

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

非常感谢!

首先你得知道你用的是什么characteristic properties

输入1:CBCharacteristicPropertyNotify

这样的话,必须要为特性服务设置Notify。

例如:

- (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