Please refer two articles, for better understanding of BLE and IONIC connectivity and how to implement ionic 4 Bluetooth low energy devices communication.
Standard BLE service is FFE0 and BLE characteristic with WriteWithoutResponse is FFE1
After connecting to BLE devices; we need to do following tasks to start reading data
So we need two more functions one to convert array buffer into plain character string and another function to capture valuechange
- Ionic-4 Bluetooth Low Energy Devices - List BLE Devices
- Ionic 4 bluetooth low energy - sample code - Connect to BLE Devices
In this article we shall communicate with BLE (Bluetooth Low energy) device with IONIC4.
To start BLE device we need to send some Command in my case it is ST (start + enter).
Note: BLE takes one character at a time and also in HEX
Hex code for S - 53
Hex code for T - 54
Hex code for LF (Line feed) - 0A
Also BLE device has their own Services and Characteristics, in previous article we had listed all the services associated with selected device. ref following code in previous article in details page (details.page.html)
<ion-list>
<ion-item ngfor="let service of peripheral.services">
{{service}}
</ion-item>
</ion-list>
Standard BLE service is FFE0 and BLE characteristic with WriteWithoutResponse is FFE1
After connecting to BLE devices; we need to do following tasks to start reading data
- WriteWithoutResponse (Service FFE0 and Characteristic FFE1)
- Then Subscribe to start getting value change notification for Service FFE0 and Characteristic FFE1
Check following two snippets one for WriteWithoutResponse and another for StartGettingNotification
BleWrite() {
// Subscribe for notifications when the resitance changes
var inputdata = new Uint8Array(3);
inputdata[0] = 0x53; // S
inputdata[1] = 0x54; // T
inputdata[2] = 0x0a; // LF
this.ble
.writeWithoutResponse(
this.peripheral.id,
BLE_SERVICE,
BLE_CHARACTERISTIC,
inputdata.buffer
)
.then(
data => {
debugger;
console.log(data);
this.subscribe();
},
err => {
console.log(err);
}
);
}
And for reading values from BLE device
subscribe() {
this.ble
.startNotification(this.peripheral.id,
BLE_SERVICE, BLE_CHARACTERISTIC)
.subscribe(
data => {
console.log(data);
this.onValueChange(data);
},
() =>
this.showAlert(
"Unexpected Error",
"Failed to subscribe for changes, please try to re-connect."
)
);
}
In above function we are capturing data and passing it to onValueChange function. You can handle your data in onValueChange function. Data which we send and receive from BLE devices is always an ArrayBuffer. In above functions we have passed ST + LineFeed as array buffer input.buffer
So we need two more functions one to convert array buffer into plain character string and another function to capture valuechange
onValueChange(buffer: ArrayBuffer) {
this.ngZone.run(() => {
try {
if (this.dataFromDevice == undefined)
this.dataFromDevice = this.bytesToString(buffer).replace(/\s+/g, " ");
else this.dataFromDevice += '
' + this.bytesToString(buffer).replace(/\s+/g, " ");
//Simply assign data to variable dataFromDevice and string concat
} catch (e) {
console.log(e);
}
});
}
bytesToString(buffer) {
return String.fromCharCode.apply(null, new Uint8Array(buffer));
}
This is how we read data from BLE devices using IONIC4.
Ionic4 Ble example.