0

im new in ionic, right now i'm trying to send a "code" in string. which the string consist of a few packets data in HEX. eg. V1-FC03-2ED1-FE01

V1 is the sequence, then after the first - is the first packet "FC03" the code successfully send to my arduino using serial monitor on pc. arduino serial monitor

now i want to send it trough BLE using ionic. i follow the example on github on doing the BLE. it works if send 1 or 0.

here is the function in ionic codes that going to send to arduino after button pressed

onPowerSwitchChange(event) {
console.log('onPowerSwitchChange');
let value = this.power ? 1 : 0;
let buffer = new Uint8Array([value]).buffer;
console.log('Power Switch Property ' + this.power);
this.ble.write(this.peripheral.id, LIGHTBULB_SERVICE, SWITCH_CHARACTERISTIC, buffer).then(
  () => this.setStatus('Light is ' + (this.power ? 'on' : 'off')),
  e => this.showAlert('Unexpected Error', 'Error updating power switch')
);
}

here i tried to change

 let value = this.power ? 1 : 0;

to

 let value = "V1-FC03-2ED1-FE01";

but when compile, got error

Argument of type 'string[]' is not assignable to parameter of type 'ArrayBuffer'. Property 'byteLength' is
        missing in type 'string[]'.
  L68:  let value = "V1-FC03-2ED1-FE01";
  L69:  let buffer = new Uint8Array([value]).buffer;
  L70:  console.log('Power Switch Property ' + this.power);

hopefully someone can help me on this problem

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • Possible duplicate of [Converting between strings and ArrayBuffers](https://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers) – gre_gor Aug 27 '18 at 18:11

1 Answers1

0

Not sure if this will help you as I'm not a programmer by trade... but I wanted to send a "2" as string and it turned out that I had to do it like this:

  var data = new Uint8Array(1);
  data[0] = 50;

With 50 being the Uint8Array equivalent of 2

Currently I'm trying to read from the BLE in the opposite direction to no effect so please keep us posted as to your progress.

frednikgohar
  • 340
  • 1
  • 3
  • 14