Ionic BLE : Can't store this.var after reading in BLE data


I’m able to read the BLE data just fine & store it in an array. However if I want to update my view and put the array values into my this.first_BLE_stat it simply won’t work. Nothing happens. I can do an alert and print data[0] and it is perfect. I’ve been reading for hours and still can’t figure it out - apologies i’m an ionic newbie!

    connect(id) {
        BLE.connect(id).subscribe (
          peripheralData => {
                    alert("Connect:" + JSON.stringify(peripheralData));
              , this.service_UUID, this.color_UUID).then(function (buffer){
                             alert("I'm running first");
                         //convert bytes to string   
                           var convertData = String.fromCharCode.apply(null, new Uint8Array(buffer));
                          var data = [];
                          for (var i = 0; i < convertData.length; i++) {
                            var resultNumber = convertData.charCodeAt(i);
                            data[i] = (resultNumber.toString());
                            alert("data from BLE:  " + data[i]);
                        this.first_BLE_stat= data[0];
                        alert("first BLE stat" + this.first_BLE_stat);
                        }, function(error) {
                          alert("Error Read" + JSON.stringify(error));
                error => alert("Error Connecting" + JSON.stringify(error))

In my HTML i just have a{{first_BLE_stat}}.

I’ve also tried using Ng zone & updating the variable inside but no luck :frowning:

Any help appreciated…I think I’m going insane :slight_smile:


I have found that change detector is sometimes needed with BLE, not sure why it may be a bug

import { ChangeDetectorRef } from ‘@angular/core’;
constructor(public cdf: ChangeDetectorRef)
this.cdf.detectChanges(); // run this after you change something that the view needs to see

1 Like

Hello! Thank you so much for taking the time to reply. I have tried your solution but unfortunately I am still having the same problem. Have you been working with BLE & ionic 2? Do you have any basic sample I could use to try fix this? Resources for BLE & ionic are scarce :frowning: Thanks again! -Daire

Instead of ".then(function (buffer){" use ".then((buffer) => {"

"this" is not bound to your controller when you are calling this.first_BLE_stat= data[0];

you could also try

connect(id) {
   var self = this;
   self.first_BLE_stat= data[0];
1 Like

Please do not do let self = this:. Please.

The previous poster is correct though that you shouldn’t be typing function within a function. Instead you should utilize fat arrows, so respectively doing:

buffer =>
error =>

This will effectively, behind the scenes anyway, do the same thing as the previous poster said. This will very likely fix your issue as well, as currently it’s not setting “your” Bluetooth variable data, but instead a different contexts/scope.


Thank you both for taking time out to help. I really do appreciate it! As you both figured, “this” was not bound to my controller. The fat arrows saved the day. :smile:

1 Like