简体   繁体   中英

Ionic BLE writing to other devices

My app is currently connected to a device that I am trying to write with.

Log: Message we are trying to send is "Hello"

⚡️  [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

The issue I'm having is the writing function in 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");
  }

}

EDIT: I turned this into two functions so I don't have to worry about the promise errors anymore. Log shows that we are calling the function and we are connected to the BLE device. Still, the message doesn't actually send.

It's not pretty, but it worked

.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");
}

}
}

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