I'm currently facing an issue with my Ionic 2 project and I'm hoping someone here can provide some assistance. The problem lies within a provider that I have created which contains a function for BLE. I have successfully included this provider in one of my pages and can call the function without any issues. However, I am now trying to access a public variable within the provider. This variable represents the connection status (true or false) and I need to retrieve it from my page to determine if I am connected to BLE or not.
Below is the code snippet:
BleProvider.ts
@Injectable()
export class BleProvider {
public isConnected:boolean;
constructor(public http: Http) {
console.log('Hello BleProvider Provider');
}
connectToDevice(device){
this.isConnected = true;
}
}
My page Login.ts
import { BleProvider } from '../../providers/ble/ble'
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
@NgModule({
providers: [BleProvider],
})
export class LoginPage {
constructor(public ble: BleProvider){
ble.connectToDevice();
}
function testIsConnected(){
console.log("isConnected value: " + this.ble.isConnected);
}
}
Upon testing, the console log returns "isConnected value: undefined". Even when I set the isConnected variable in the provider constructor (e.g. this.isConnected = false), calling the connectToDevice function still returns false instead of true. Can anyone shed light on what might be causing this and how to resolve it?