首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >离子BLE写入到其他设备

离子BLE写入到其他设备
EN

Stack Overflow用户
提问于 2020-03-18 00:59:01
回答 1查看 289关注 0票数 0

我的应用程序当前已连接到我正在尝试使用的设备。

日志:我们尝试发送的消息是"Hello“

代码语言:javascript
运行
复制
⚡️  [log] - WE ARE CURRENTLY CONNECTED
To Native Cordova ->  BLE writeWithoutResponse BLE46928892 ["options": 
[027B18C5-15E6-86C1-ADDC-B2475099CDA0, 6E400001-B5A3-F393-E0A9- 
E50E24DCCA9E, 6E400002-B5A3-F393-E0A9-E50E24DCCA9E, {
    0 = 72;
    1 = 69;
    2 = 76;
    3 = 76;
    4 = 79;
}]]
2020-03-17 16:16:19.223013-0400 App[5985:1842762] writeWithoutResponse
2020-03-17 16:16:19.223160-0400 App[5985:1842762] getData
2020-03-17 16:16:19.223308-0400 App[5985:1842762] Looking for 
6E400002-B5A3-F393-E0A9-E50E24DCCA9E with properties 4

我遇到的问题是BLE中的写入功能。

代码语言:javascript
运行
复制
(method) BLE.write(deviceId: string, serviceUUID: string, characteristicUUID: string, value: 
ArrayBuffer): Promise<any>

.TS

代码语言:javascript
运行
复制
import { BLE } from '@ionic-native/ble/ngx';

devices:any[] = [];
id;

constructor(
public plt: Platform,
private route: ActivatedRoute,
private ble: BLE,
private ngZone: NgZone,
private cd: ChangeDetectorRef,
) { }


connect(count:number){

this.ble.connect(this.devices[count].id).subscribe
(peripheralData => {
      console.log(peripheralData);

    },
    peripheralData => {
      console.log('disconnected');
    });;

    this.id = this.devices[count].id;

}

write(){
    if (this.ble.isConnected){
      console.log("WE ARE CURRENTLY CONNECTED");
    var string = "HELLO"

    var array = new Uint8Array(string.length);
    for (var x = 0, l = string.length; x < l; x++)
      {array[x] = string.charCodeAt(x);}

    this.ble.writeWithoutResponse(this.id, "6E400001-B5A3-F393-E0A9- 
    E50E24DCCA9E", 
    "6E400002-B5A3-F393-E0A9-E50E24DCCA9E", array);
  }
  else{
    console.log("WE ARE NOT CONNECTED");
  }

}

编辑:我将其转换为两个函数,这样我就不必再担心promise错误了。日志显示我们正在调用该函数,并且我们已连接到BLE设备。尽管如此,消息实际上并没有发送出去。

EN

回答 1

Stack Overflow用户

发布于 2020-03-18 05:07:48

它不是很漂亮,但它起作用了

.TS

代码语言:javascript
运行
复制
import { BLE } from '@ionic-native/ble/ngx';


export class AddDeviceComponent implements OnInit {

routeParams;
SCAN_RESULT_COUNT = 20;
scanResult;
id;



constructor(
public plt: Platform,
private route: ActivatedRoute,
private ble: BLE,
private ngZone: NgZone,
private cd: ChangeDetectorRef,
) { }


connect(count:number){

this.ble.connect(this.devices[count].id).subscribe
(peripheralData => {
  console.log(peripheralData);

},
peripheralData => {
  console.log('disconnected');
});;

this.id = this.devices[count].id;

}



  write(){
if (this.ble.isConnected)
{
  console.log("WE ARE CURRENTLY CONNECTED");
var string = "HELLO"

var array = new Uint8Array(string.length);
for (var x = 0, l = string.length; x < l; x++) {
  array[x] = string.charCodeAt(x);
}


// this is what needed to be done
this.ble.writeWithoutResponse(this.id, "6E400001-B5A3-F393-E0A9- 
E50E24DCCA9E".toLowerCase(), 
 "6E400002-B5A3-F393-E0A9-E50E24DCCA9E".toLowerCase(), array.buffer);



}
else{
 console.log("WE ARE NOT CONNECTED HELP");
}

}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60726926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档