Write commands from react native application

Posted on
  • Hi everyone!
    I have a puck.js which I would like to use to control a robotic car, made up of 4 servomotors. In order to control this, I'd want to send the commands to the puck via BLE having a react native application as interface.
    I manged to identify the characteristics I can write to, but I don't know hoe to code the puck so that it reads these characteristics and acts upon what is communicated through them.
    Does anyone have any idea how to help me with this task? Or knows of any effective tutorials I can look into...
    I am quite new to the javascript-bluetooth world so any help is deeply appreciated.
    Silvia

  • Tue 2020.02.11

    Well hello @user109470 and welcome to Espruino!

    See the examples here, and ask away . . .

    http://www.espruino.com/BLE+Advertising

    http://www.espruino.com/Tutorials

    forum post Trying to understand BLE advertising


    'I am quite new to the javascript-bluetooth world'

    Would links to Javascript only sites aid here?

  • You might find this handy: http://www.espruino.com/About+Bluetooth+­LE#services-and-characteristics

    It's the NRF.setServices function that you'll need on Puck.js, and there is some example code in http://www.espruino.com/Reference#l_NRF_­setServices

    For instance:

    NRF.setServices({
      0xBCDE : {
        0xABCD : {
          writable : true,
          onWrite : function(evt) {
            digitalWrite([LED3,LED2,LED1], evt.data[0]);
          }
        }
      }
    });
    

    Will let you change the state of the 3 LEDs.

    Your other option is to write raw JS code to the 'UART' characteristic on Puck.js - it's a bit less efficient but that way you don't have to have any specific code on the Puck...

  • Thank you so much!
    I managed to read from an espruino board with the react app I developed, now the part I am missing is writing. The javascript links might be helpful, thank you!
    Once I define the characteristic I want to write to, with the code provided below, it recieves the sent data in evt, correct? and I can store that data in an array I can then access to control the car.
    If that is correct, you have been super helpful and I guess I will move to figuring out how to write in the characteristic from the app.

  • Wed 2020.02.12

    My favorite I like to introduce individuals to:

    https://www.w3schools.com/js/

    as each concept has a 'try it yourself' window, where basic edits may be entered.

    Mozilla repository:

    https://developer.mozilla.org/en-US/docs­/Web/JavaScript
    https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Reference/Functions

    International Standards body:

    https://www.w3.org/standards/webdesign/s­cript


    From post #4

    'with the code provided below'

    Was this a reference to the snippet in post #3 ? (e.g. that snippet appeared below the 'reply to edit' window when editting the reply, [even though post #3 occurred before your edit] rather than code that was intended to be uploaded?)

    I believe it is the setServices() function you are after:

    http://www.espruino.com/Reference#NRF
    http://www.espruino.com/Reference#t_l_NR­F_setServices

  • Hi Robin, I think @user109470 has got the hang of the JS side of things.

    it recieves the sent data in evt, correct?

    Yes, evt.data is in ArrayBuffer, so you can do new Uint8Array(evt.data) to get an array you can easily access from it.

  • Thank you all for the help! I tried uploading the example code to the puck, but when I connect to it from the react native app and printing out all characteristics, I can't seem to find the one I supposedly created using NRF.setServices().
    Am I doing something completely wrong that you can see? When using the same RN library reading from a MDBT42Q it works well.

    Espruino Code:

    console.log(NRF.getAddress());
    console.log(NRF.getBattery());
    
    NRF.setServices({
      0xBCDE : {
        0xABCD : {
          value: 'hi',
          writable : true,   // optional, default is false
          notify : true,   // optional, default is false
          indicate : true,   // optional, default is false
          description: "My Characteristic",  // optional, default is null,
          onWrite : function(evt) {
            console.log(evt.data);
          }
        }
      }
    });
    

    RN code:

    componentDidMount() {
            const bleManager = new BleManager();
    
            bleManager.startDeviceScan(null, null, async (err, device) => {
                if (err) console.log(err)
                else {
                    if (device.localName === DEVICE_NAME) {
                        bleManager.stopDeviceScan()
    
                        const deviceId = device.id;
                        console.log(`Trying to connect to device with MAC: ${deviceId}`);
    
                        try {
                            const device = await bleManager.connectToDevice(deviceId).the­n(d => d.discoverAllServicesAndCharacteristics(­));
                            const svcs = await device.services();
                            console.log('try')
                            for (let ii = 0; ii < svcs.length; ++ii) {
                                const currentService = svcs[ii];
                                let characteristics = await currentService.characteristics();
                                for (let jj = 0; jj < characteristics.length; ++jj) {
                                    const currentCharacteristic = characteristics[jj];
                                    console.table(currentCharacteristic);
                                    if (currentCharacteristic.uuid === CHARACTERISTIC_UUID {
    
    
                                  
                                        console.table(currentCharacteristic);
                                        const cc = await currentCharacteristic.writeWithoutRespon­se(
                                            btoa('1111111')
                                        )
                                        
                                        console.table(currentCharacteristic);
    
                                    }
    
    
                                }
                            }
                        }
                        catch (e) {
                            console.log(e)
                        }
                    }
                }
            })
        }
    
    
    
  • You could try manually disabling bluetooth on the device and re-enabling it. I know iOS (and newer Android?) actually cache the Bluetooth services for devices so if you connected to the device previously then the phone might be giving you old info on services?

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Write commands from react native application

Posted by Avatar for user109470 @user109470

Actions