[英]Ionic BLE writing to other devices
我的应用程序当前连接到我正在尝试使用的设备。
日志:我们试图发送的消息是“你好”
⚡️ [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 中的写入功能。
(method) BLE.write(deviceId: string, serviceUUID: string, characteristicUUID: string, value:
ArrayBuffer): Promise<any>
.TS
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");
}
}
编辑:我把它变成了两个函数,所以我不必再担心承诺错误了。 日志显示我们正在调用该函数并且我们已连接到 BLE 设备。 尽管如此,该消息实际上并未发送。
它不漂亮,但它有效
.TS
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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.