Ionic framework 如何在Ionic 4中检查蓝牙状态
我是爱奥尼亚4的新手,我正在尝试使用@Ionic native/diagnostic检查蓝牙状态,这是我的代码 app.module.tsIonic framework 如何在Ionic 4中检查蓝牙状态,ionic-framework,ionic4,Ionic Framework,Ionic4,我是爱奥尼亚4的新手,我正在尝试使用@Ionic native/diagnostic检查蓝牙状态,这是我的代码 app.module.ts @NgModule({ declarations: [ MyApp, HomePage, CheckRegInfo ], imports: [ BrowserModule, HttpClientModule, TranslateModule.forRoot({ loader: {
@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->它将返回错误对象(…)不是函数尝试以下操作:
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调用它:
核心插件模块通过全局cordova.plugins.diagnostic
对象公开,它对其他可选模块的所有函数和属性使用别名
另外,由于then()
用于异步操作,因此需要在的内部调用方法showarter(titleStr)
尝试以下操作:
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调用它:
核心插件模块通过全局cordova.plugins.diagnostic
对象公开,它对其他可选模块的所有函数和属性使用别名
另外,由于then()
用于异步操作,因此需要在的内部调用方法showarter(titleStr)
你在哪里调用checkBluetoothState()?我通过屏幕上的按钮调用它签入你的设备,你收到警报了吗?根本没有响应你在哪里调用checkBluetoothState()?我通过屏幕上的按钮调用它签入你的设备,你收到警报了吗?根本没有响应谢谢你的回答,我尝试按照您的指示在config.xml上添加cordova插件,使用首选项指定模块,添加蓝牙权限,但仍然没有任何更改,我们是否需要使用以下内容指定cordova:声明let cordova:any;你在用哪部电话?另外,请在方法声明之后立即添加console.log(),以查看它是否正在进入该方法。。也请使用chrome://inspect/ 在设备上运行应用程序时,我使用三星note FE,它已经调用了函数checkBluetoothState,但在ionic serve中,它会出现一个错误,即未定义cordova:(此插件在ionic serve(浏览器)中不起作用),仅适用于手机我明白,但cordova的声明有任何问题吗?谢谢你的回答,我尝试按照你的指示在config.xml上添加cordova插件,优先指定模块,添加蓝牙权限,但仍然没有任何变化,我们是否需要在is:声明让科尔多瓦:有;你在用哪部电话?另外请添加console.log()方法声明后,立即查看是否正在输入方法..也请使用chrome://inspect/ 在设备上运行应用程序时,我使用三星note FE,它已经调用了函数checkBluetoothState,但在ionic serve中,它会出现一个错误,即未定义cordova:(此插件在ionic serve(浏览器)中不起作用),这只适用于手机。我明白,但是科尔多瓦的声明有什么问题吗?