'Sending data over ble with React Native

I have a react-native application that needs to send a data array across ble and write to a characteristic on a peripheral device.

I have the project set up and functioning but was wondering about the type of data assembly I'm using and whether it's going to create problems.

I'm currently just using a blank Array to save the data that is required before sending the array.

My question is whether this method will run into memory problems etc or something I'm not aware of.

    //function to pack bytes into 32bit int
    const toBytesInt32 = (num) => {
        let arr = new Uint8Array([
            (num & 0xff000000) >> 24,
            (num & 0x00ff0000) >> 16,
            (num & 0x0000ff00) >> 8,
            (num & 0x000000ff)
        ]);
        console.log('arr:',arr);
        return arr;
    };

//This is inside a function that's called via button tap
BleManager.retrieveServices(device.id).then((deviceInfo) => {
    console.log('Device name is:', device.name)
    const serviceUUID = '**************************';
    const charUUID = '**************************';

//create array and buffer for dispense characteristic structure
    const command = [];
    console.log('creating command array...');

    command[0] = 1;
    command[1] = 0;

//Get time/lat/long wifi position
    GetLocation.getCurrentPosition({enableHighAccuracy: false, timeout: 15000}).then(location => {
        let tmp = toBytesInt32(location.time/1000);
        command[2] = tmp[0];
        command[3] = tmp[1];
        command[4] = tmp[2];
        command[5] = tmp[3];
        console.log('time tmp:', tmp)

        tmp= toBytesInt32(location.latitude*10000000.0)
        command[6] = tmp[0];
        command[7] = tmp[1];
        command[8] = tmp[2];
        command[9] = tmp[3];

        tmp= toBytesInt32(location.longitude*10000000.0)
        command[10] = tmp[0];
        command[11] = tmp[1];
        command[12] = tmp[2];
        command[13] = tmp[3];

        console.log('assembled command payload:', command);
        
        BleManager.write(device.id, serviceUUID, charUUID, command).then((results) => {
        console.log('dispense payload write:', command);
        console.log('dispense payload write results:', results);
    
        })
...///catch error handling

I previously tried to create a TypeArray and Dataview on 14 bytes of memory then assembling the payload,

const payload = new ArrayBuffer(14);
const command = new Uint8Array(payload);

but i was encountering many errors involving the below

"Malformed JS fields"

or

"com.facebook.react.bridge.ReadableNativeMap cannot be cast to com.facebook.react.bridge.ReadableNativeArray"

thanks in advance for any assistance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source