Here is the code I'm currently working with:
import {Page} from 'ionic-angular';
import {BLE} from 'ionic-native';
@Page({
templateUrl: 'build/pages/list/list.html'
})
export class ListPage {
devices: Array<{name:string, id: string}>;
constructor() {
this.devices=[];
}
startScan (){
this.devices = [];
BLE.scan([],5).subscribe(
(device)=>{
if(device.name){
this.devices.push({name:device.name,id:device.id});
}
},
(err) => {
console.log(JSON.stringify(err));
}
);
}
connectToDevice(device){
BLE.connect(device.id).subscribe(success=>{
console.log(JSON.stringify(success));
});
}
}
When I call the startScan function and try to add the returned device to an array, this.devices seems to be inaccessible. I attempted saving it using 'self=this', but that didn't work either. Can someone help me figure out what I'm missing?
UPDATE: Adding
var self = this;
at the beginning of startScan() and then utilizing it in the .subscribe callback solves the issue!