1

Using cordova BLE central with Ionic, I'm trying to write (withoutResponse).

According to the doc the data format is an ArrayBuffer.

From packet analysis I know that the written value should be 02FD 1600 0000 0000 002E 1481 1F8F 7500 0000 004D

My question is how to create the required ArrayBuffer using JavaScript ?

I've tried with

Buffer.from("02FD 1600 0000 0000 002E 1481 1F8F 7500 0000 004D").buffer

But when I check the exchanged packet, the written value is

3032 4644 2031 3630 3020 3030 3030 2030 3030 3020 3030 3245 2031 3438 3120 3146 3846 2037 3530 3020 3030 3030 2030 3034 44

0

1 Answer 1

1

https://nodejs.org/api/buffer.html#buffer_buf_buffer

This ArrayBuffer is not guaranteed to correspond exactly to the original Buffer. See the notes on buf.byteOffset for details.

Buffer's data in the underlying ArrayBuffer begins at .byteOffset

You are also using the wrong format for the hex string that Buffer.from reads, and you also need to tell it to use 'hex' encoding (default is 'utf-8'). It does not understand spaces (nor the concept of 16-bit values, which would be the job of TypedArrays and the .readInt16BE and other utility functions).

buf=Buffer.from("02FD160000000000002E14811F8F75000000004D",'hex')
data=buf.buffer.slice(buf.byteOffset,buf.byteOffset+buf.length)

I don't know where you got that data, but it looks like 16-bit values. You will have to make sure of the endian-ness of the data, to make sure the bytes don't need to be swapped.

Get ArrayBuffer by using TypedArray. This works in browser:

    data = new Uint16Array("02FD 1600 0000 0000 002E 1481 1F8F 7500 0000 004D"
     .split(' ').map(x=>parseInt(x,16)))
    
    console.log([...data].map(x=>x.toString(16).padStart(4,'0')).join(''))
    
    buf = data.buffer; //ArrayBuffer

Sign up to request clarification or add additional context in comments.

2 Comments

Hi user120242, if I had instead to create a buffer instead of ArrayBuffer, I would do it via Buffer.from("02FD160000000000002E14811F8F75000000004D",'hex') ?
read here: nodejs.org/api/buffer.html but yes, that's one way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.