如何在 Ionic 4 中检查蓝牙状态
How to check bluetooth state in Ionic 4
我是 Ionic 4 的新手,我正在尝试使用 @ionic-native/diagnostic 检查蓝牙状态,这是我的代码
app.module.ts
@NgModule({
declarations: [
MyApp,
HomePage,
CheckRegInfo
],
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
}),
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
CheckRegInfo
],
providers: [
StatusBar,
SplashScreen,
Diagnostic,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
校验函数:
import { Component } from '@angular/core';
import {AlertController, NavController} from 'ionic-angular';
import {Diagnostic} from "@ionic-native/diagnostic/ngx";
@Component({
selector: 'page-check',
templateUrl: 'check.html'
})
export class CheckRegInfo {
logowhitesrc:string = "assets/imgs/logowhite.png";
constructor(public navCtrl: NavController, private alertController:AlertController, private diagnostic: Diagnostic) {
}
checkBluetoothState(){
let titleStr: string;
this.diagnostic.getBluetoothState()
.then((state) => {
if (state == this.diagnostic.bluetoothState.POWERED_ON){
titleStr = "Okay"
} else {
titleStr = "Not Okay"
}
}).catch(e => console.error(e));
let addAlert = this.alertController.create({
title:"Bluetooth state",
message:titleStr
});
addAlert.present();
}
}
但是当我在设备上尝试这个时,没有任何反应?我尝试使用 ionic serve -> 它会出现错误 Object(...) is not a function
尝试以下操作:
declare let cordova: any;
@Component({
selector: 'page-check',
templateUrl: 'check.html'
})
export class CheckRegInfo {
logowhitesrc : string = "assets/imgs/logowhite.png";
constructor(public navCtrl : NavController,
private alertController : AlertController,
private diagnostic : Diagnostic) {}
checkBluetoothState(){
let titleStr: string;
cordova.plugins.diagnostic.getBluetoothState().then((state) => {
if (state == cordova.plugins.diagnostic.bluetoothState.POWERED_ON){
titleStr = "Okay";
showAlert(titleStr);
} else {
titleStr = "Not Okay";
showAlert(titleStr);
}
}).catch(e => console.error(e));
}
showAlert(title){
let addAlert = this.alertController.create({
title :"Bluetooth state",
message:titleStr
});
addAlert.present();
}
根据文档,您需要使用 cordova.plugins.diagnostic
:
来调用它
The core plugin module is exposed via the global cordova.plugins.diagnostic
object and it aliases all functions and properties of the other optional modules.
另外,由于then()
用于异步操作,所以需要调用.then()
内部的方法showAlert(titleStr)
。
我是 Ionic 4 的新手,我正在尝试使用 @ionic-native/diagnostic 检查蓝牙状态,这是我的代码 app.module.ts
@NgModule({
declarations: [
MyApp,
HomePage,
CheckRegInfo
],
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
}),
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
CheckRegInfo
],
providers: [
StatusBar,
SplashScreen,
Diagnostic,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
校验函数:
import { Component } from '@angular/core';
import {AlertController, NavController} from 'ionic-angular';
import {Diagnostic} from "@ionic-native/diagnostic/ngx";
@Component({
selector: 'page-check',
templateUrl: 'check.html'
})
export class CheckRegInfo {
logowhitesrc:string = "assets/imgs/logowhite.png";
constructor(public navCtrl: NavController, private alertController:AlertController, private diagnostic: Diagnostic) {
}
checkBluetoothState(){
let titleStr: string;
this.diagnostic.getBluetoothState()
.then((state) => {
if (state == this.diagnostic.bluetoothState.POWERED_ON){
titleStr = "Okay"
} else {
titleStr = "Not Okay"
}
}).catch(e => console.error(e));
let addAlert = this.alertController.create({
title:"Bluetooth state",
message:titleStr
});
addAlert.present();
}
}
但是当我在设备上尝试这个时,没有任何反应?我尝试使用 ionic serve -> 它会出现错误 Object(...) is not a function
尝试以下操作:
declare let cordova: any;
@Component({
selector: 'page-check',
templateUrl: 'check.html'
})
export class CheckRegInfo {
logowhitesrc : string = "assets/imgs/logowhite.png";
constructor(public navCtrl : NavController,
private alertController : AlertController,
private diagnostic : Diagnostic) {}
checkBluetoothState(){
let titleStr: string;
cordova.plugins.diagnostic.getBluetoothState().then((state) => {
if (state == cordova.plugins.diagnostic.bluetoothState.POWERED_ON){
titleStr = "Okay";
showAlert(titleStr);
} else {
titleStr = "Not Okay";
showAlert(titleStr);
}
}).catch(e => console.error(e));
}
showAlert(title){
let addAlert = this.alertController.create({
title :"Bluetooth state",
message:titleStr
});
addAlert.present();
}
根据文档,您需要使用 cordova.plugins.diagnostic
:
The core plugin module is exposed via the global
cordova.plugins.diagnostic
object and it aliases all functions and properties of the other optional modules.
另外,由于then()
用于异步操作,所以需要调用.then()
内部的方法showAlert(titleStr)
。