ionic 4 bluetooth low energy - example code - Read Data from BLE Devices

January 24, 2020 8 comments
Please refer two articles, for better understanding of BLE and IONIC connectivity and how to implement ionic 4 Bluetooth low energy devices communication.


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

  1. WriteWithoutResponse (Service FFE0 and Characteristic FFE1)
  2. 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.
Share Share Tweet Share

8 thoughts on " ionic 4 bluetooth low energy - example code - Read Data from BLE Devices"

avatar

hi, can you share the sample code? im not quite clear on the subscribe and getting the data

avatar

Hi Fuad, sorry for late reply, yes please share your code.

avatar

Hi, very good tutorial on Ionic+ BLE subject matter. Would you mind sharing your sample source code with me. mohdsuhairi@gmail.com. Thanks

avatar

hello i'm also interested in the sample source code kindly. Anyone please share mwangijustus12@gmail.com

This comment has been removed by the author. - Deleted
avatar

Hello, thanks for your sample code.
does it support for android 10, also iOS?
and how does it works with writewithoutresponse S,T,LF before startnotification.
frankly I'm working with Ble 5.2.8 and Ionic 5 for arduino ble.
send string success, but receive not, just square.
and App can't find ble(HM-10) in android 10.
looking forward to your opinion.
thanks

avatar

Have the same problem the app can't find the hm-10

LEAVE A REPLY