简体   繁体   中英

Issue writing value to BLE peripheral with Ionic Native BLE

I am currently writing a small app with Ionic to control a FLUX bluetooth bulb with my app. I am using ionic-native/ble and so far everything but writing a value is working.

Scan, and connection both work fine. However when attempting to write the off value for the bulb nothing happens.

I have used the snoop functionality on android and discovered that value CC2433 is what turns the bulb off, I have also tested this using nRF Connect application and when writing that value to the bulb the light turns off. Please find code below to let me know if I am doing something dumb. Thank you !

this.ble.writeWithoutResponse(
 "3C:A3:08:A8:1E:C3", 
 "ffe5", 
 "ffe9", 
 this.off())
        .then(result => {
          console.log(result);
        }).catch(error => {
          alert(JSON.stringify(error));
        });

off() {
 let string = "CC2433";
    let array = new Uint8Array(string.length);
    for (let i = 0, l = string.length; i < l; i ++) {
      array[i] = string.charCodeAt(i);
    }
    console.log(array.buffer);
    return array.buffer;
}

As you can see I am calling the ble plugin write without response, seeing as there is no need for a response(I have tried using just the write function as well). I pass the device id, service, and characteristic which are all correct, then pass the array buffer that is returned by off function. Inside console there is no write error and gives me a result of OK(200). Although the command does nothing, even though like I said if I pass the same value into nRF Connect write command for same service and characteristic it works perfect.

Any thoughts would be much appreciated! Thank you!

I expect that you're not getting and error because the data is being written correctly, but the bulb just doesn't know how to interpret the command. CC2433 looks like hex rather than a string. Try this:

let array = new Uint8Array([0xCC, 0x24, 0x33]);

this.ble.writeWithoutResponse(
  "3C:A3:08:A8:1E:C3", 
  "ffe5", 
  "ffe9", 
  array.buffer)
        .then(result => {
          console.log(result);
        }).catch(error => {
          alert(JSON.stringify(error));
        });

The properties of characteristic ffe9 will determine if you use use ble.write or ble.writeWithoutResponse. The JSON returned when you connect to the peripheral has these details. The nRF Connect app will also show characteristic properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM