2

In order to realize a project of connected objects. I need to implement a Bluetooth connection between the various devices.

Here, the goal is to create an application in React Native and then send data from this application to my Raspberry. This Raspberry has a connected HC-08 module that takes care of Bluetooth communication.

Now, I would like to use react-native-ble-plx library to send data through Bluetooth. I'm able to connect my Android to the module. But I don't understand how to send data ...

Here's my code :

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

I would like to have a send method that will send data whenever I want to. But I don't really understand how it works :/

Could someone help me or even give me an example ? I would be really appreciated.

Best regards.

Hurobaki
  • 3,728
  • 6
  • 24
  • 41

1 Answers1

4

I had success implementing the following:

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

Where I use 12ab, insert the UUID of your BLE service. Similarly, where I use 34cd, insert the UUID of your BLE characteristic. Lastly, include a base64 encoding of whatever message you're trying to send where I have aGVsbG8gbWlzcyB0YXBweQ==.

Hope this helps.

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
  • Could you provide a brief description of what is different about your code that makes it work? Including a brief description in your answer describing what you did with the code goes a long way in helping others understand your answer. – vielkind Aug 15 '18 at 18:38
  • I provided some edits above in my original comment. If they don't make sense, let me know, and I will try to explain further. – deasydoesit Aug 15 '18 at 18:41
  • can you share sample code to implementation of this – Raj Hirani Mar 31 '19 at 04:16
  • Does the UUID of the BLE characteristic come from the device manufacturer or from a react-native-ble-plx method? – socca1157 Sep 02 '19 at 02:05
  • @deasydoesit how do i get UUID of BLE service and UUID of the BLE characteristic ?? – chirag prajapati Sep 09 '19 at 07:22
  • I need to send some data to the corresponding BLE device. How to implement it?? – FortuneCookie Nov 21 '19 at 04:12
  • thank you @deasydoesit the example works fine, however can you show us an example for a big file ? i created an issue but no response yet: https://stackoverflow.com/questions/61386613/large-file-sending-by-using-react-native-ble-plx – raddaoui Apr 23 '20 at 12:40
  • @deasydoesit I am getting the same base 64 input value as the response when I write the value. But it should return an int binary int value. What could go wrong? My implementation is the same as the above answer. – Dhanuka Perera Sep 14 '21 at 00:50