简体   繁体   English

使用Ionic Native BLE向BLE外设发出写入值

[英]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. 我目前正在用Ionic编写一个小型应用程序,用我的应用程序控制FLUX蓝牙灯泡。 I am using ionic-native/ble and so far everything but writing a value is working. 我正在使用离子本机/ ble,到目前为止,除了编写值之外,其他所有方法都有效。

Scan, and connection both work fine. 扫描和连接都可以正常工作。 However when attempting to write the off value for the bulb nothing happens. 但是,当尝试写入灯泡的off值时,没有任何反应。

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. 我在android上使用了snoop功能,发现值CC2433可以关闭灯泡,我也使用nRF Connect应用程序对此进行了测试,当将该值写入灯泡时,灯会关闭。 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). 如您所见,我正在调用ble插件write而没有响应,这是因为不需要响应(我也尝试仅使用write函数)。 I pass the device id, service, and characteristic which are all correct, then pass the array buffer that is returned by off function. 我传递了所有正确的设备ID,服务和特性,然后传递了off函数返回的数组缓冲区。 Inside console there is no write error and gives me a result of OK(200). 在控制台内部没有写入错误,并给我一个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. 尽管该命令不执行任何操作,但是即使就像我说的那样,如果我为相同的服务和特性将相同的值传递给nRF Connect写命令,它也可以正常工作。

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. CC2433看起来像十六进制而不是字符串。 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. 特征ffe9的属性将确定您使用ble.write还是ble.writeWithoutResponse。 The JSON returned when you connect to the peripheral has these details. 当您连接到外围设备时返回的JSON具有这些详细信息。 The nRF Connect app will also show characteristic properties. nRF Connect应用程序还将显示特征属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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