I want to perform some action after Bluetooth connection is done and vice versa.
Handled scenarios for connection and added success and failure handler also, and changing a flag to True and False in those handler functions.
I printed that value using console.log, it changes in a component file but does not reflect in HTML.
I tried using ngZone, but it's not working.
Success and failure handle code are as follows:
BluetoothService
import { Injectable } from "@angular/core";
import { BLE } from '@ionic-native/ble';
@Injectable()
export class BlueToothService {
constructor(private ble: BLE){
}
public connect = (deviceId, onConnect, onFailure) => {
this.ble.isConnected(deviceId)
.then(response => {
onConnect(response);
},
error => {
this.ble.connect(deviceId)
.subscribe(response => {
onConnect(response);
},
error => {
onFailure(error);
});
});
} }
Component File
import {Component, NgZone} from '@angular/core';
import {Events, IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {BlueToothService} from '../../../providers/bluetooth/bluetooth.service';
@IonicPage()
@Component({
selector: 'test-data',
templateUrl: 'test-data.html',
})
export class AddTestKitDataPage {
public isBluetoothConnected: boolean = false;
public deviceId: any;
public connectToBLE() {
this.blueToothService.connect(this.deviceId, onConnectionSuccess, onConnectionFailure); //Assume device id is already present
}
private onConnectionSuccess = (reason) => {
this.zone.run(() => {
this.isBluetoothConnected = true;
});
};
private onConnectionFailure = (reason) => {
this.zone.run(() => {
this.isBluetoothConnected = false;
});
} }
HTML
<ion-content>
<div text-center *ngIf="!isBluetoothConnected">
Bluetooth Connection failure
</div>
<div text-center *ngIf="isBluetoothConnected">
Bluetooth Connection success
</div>
<button ion-button full class="primaryBlockButton" (click)="connectToBLE()">Click</button>
</ion-content>
onConnectionSuccess
called? – Kure