使用react-native-ble-plx包发送数据

时间:2018-05-02 15:06:23

标签: javascript android react-native bluetooth-lowenergy

为了实现连接对象的项目。我需要在各种设备之间实现蓝牙连接。

这里的目标是在React Native中创建一个应用程序,然后将数据从此应用程序发送到我的Raspberry。这个Raspberry有一个连接的HC-08模块,负责蓝牙通信。

现在,我想使用react-native-ble-plx库通过蓝牙发送数据。我能够将我的Android连接到模块。但我不明白如何发送数据...

这是我的代码:

constructor() {
        super()
        this.manager = new BleManager()
    }
    componentWillMount() {
        console.log("mounted")
        const subscription = this.manager.onStateChange((state) => {
            if (state === 'PoweredOn') {
                this.scanAndConnect();
                subscription.remove();
            }
        }, true);
    }

    scanAndConnect() {
        this.manager.startDeviceScan(null, null, (error, device) => {
            if (error) {
                // Handle error (scanning will be stopped automatically)
                return
            }

            console.log(device.name)

            // Check if it is a device you are looking for based on advertisement data
            // or other criteria.
            if (device.name === 'SH-HC-08') {
                // Stop scanning as it's not necessary if you are scanning for one device.
                this.manager.stopDeviceScan();
                console.log(`Found ${device.name}`)
                this.setState({
                    device: device
                })
                // Proceed with connection.
                device.connect()
                    .then((device) => {
                        console.log(device)
                        return device.discoverAllServicesAndCharacteristics()
                    })
                    .then((device) => {
                        console.log(device)
                    })
                    .then((result) => {
                        // Do work on device with services and characteristics
                        //console.log(this.manager.characteristicsForService("00001800-0000-1000-8000-00805f9b34fb"))
                        console.log(result)
                        console.log("connected")
                    })
                    .catch((error) => {
                        // Handle errors
                        console.log(error)
                    });
            }
        });
    }

    send() {
        this.manager.writeCharacteristicWithResponseForDevice("58:7A:62:4F:EF:6D",
            this.device.serviceUUIDs[0],
            this.manager.characteristicsForDevice(this.device.id),
            "ok")
            .catch((error) => {
                console.log('error in writing data');
                console.log(error);
            })
    }

我想有一个send方法,可以随时发送数据。但我真的不明白它的工作原理:/

有人可以帮助我甚至给我一个例子吗?我真的很感激。

最好的问候。

1 个答案:

答案 0 :(得分:0)

我成功实现了以下内容:

scanAndConnect() {
    this.manager.startDeviceScan(null, null, (error, device) => {
      this.info("Scanning...");
      console.log(device);

      if (error) {
        this.error(error.message);
        return
      }

      if (device.name ==='MyDevice') {
        this.info("Connecting to Tappy");
        this.manager.stopDeviceScan();

        device.connect()
          .then((device) => {
            this.info("Discovering services and characteristics");
            return device.discoverAllServicesAndCharacteristics()
          })
          .then((device) => {
            this.info(device.id);
            device.writeCharacteristicWithResponseForService('12ab', '34cd', 'aGVsbG8gbWlzcyB0YXBweQ==')
              .then((characteristic) => {
                this.info(characteristic.value);
                return 
              })
          })
          .catch((error) => {
            this.error(error.message)
          })
       }
   });

在我使用12ab的地方,插入您的BLE服务的UUID。同样,在我使用34cd的地方,插入您的BLE特性的UUID。最后,包括您要发送到aGVsbG8gbWlzcyB0YXBweQ==的任何消息的base64编码。

希望这会有所帮助。