rusel1989 / react-native-bluetooth-serial

Port of https://github.com/don/BluetoothSerial for react native
476 stars 291 forks source link

How to send Hex Values via Bluetooth #71

Open vladinko0 opened 6 years ago

vladinko0 commented 6 years ago

In the BluetoothSerialExample I see:

 write (message) {
    BluetoothSerial.write(message)
    .then((res) => {
      Toast.showShortBottom('Successfuly wrote to device')
      this.setState({ connected: true })
    })
    .catch((err) => Toast.showShortBottom(err.message))
  }

But I need to send the Enquiry ENQ (0x05). And if I store into the "message" value 0x05, slave device gives me no response, because it probably wants hex value 0x05, not string value.

In the ..\react-native-bluetooth-serial-master\index.js I see:

BluetoothSerial.write = (data) => {
  if (typeof data === 'string') {
    data = new Buffer(data)
  }
  return BluetoothSerial.writeToDevice(data.toString('base64'))
}  

Should I replace data.toString('base64') with data('base64') to pass hex value, or should I in ..\node_modules\react-native-bluetooth-serial\android\src\main\java\com\rusel\RCTBluetoothSerial\RCTBluetoothSerialModule.java:

 public void writeToDevice(String message, Promise promise) {
        if (D) Log.d(TAG, "Write " + message);
        byte[] data = Base64.decode(message, Base64.DEFAULT);
        mBluetoothService.write(data);
        promise.resolve(true);
    }

replace String message with Int message in order to send hex value 0x05?

Please help me. I feel so lost.

mahdieh-dev commented 6 years ago

I also need to pass hex value with this package. Can anyone help please?

PiotrDabek commented 6 years ago

Have you managed to solve this problem? I'm facing similar one right now. Any help would be appreciated

JackTam1993 commented 6 years ago

Here is my solution:

let buf = new Buffer("AA22EC00000000000102AA5500023C4E000001007000000000002300000000000000D0", "hex");
BluetoothSerial.write(buf)

Hope that still helpful.

chamaloriz commented 5 years ago

I found how to :p spent 2 Days

basically the array wasn't showing as a uint8array but as a typedArray Buffer.from() fixed it

let encoder = new EscPosEncoder();

let result = encoder
  .initialize()
  .text("HelloWorld")
  .newline()
  .qrcode('HelloWorld')
  .encode();

const { Buffer } = require('buffer');

BluetoothSerial.write(Buffer.from(result.buffer))