如何在Ionic 4中检查蓝牙状态

时间:2019-04-12 07:15:35

标签: ionic-framework ionic4

我是Ionic 4的新手,我正在尝试使用@ ionic-native / diagnostic检查蓝牙状态,这是我的代码 app.module.ts

public void replaceImageById(String id, String placeholderImageName, File newImage) throws Exception {
    Relationship rel = document.getMainDocumentPart().getRelationshipsPart().getRelationshipByID(id);

    BinaryPartAbstractImage imagePart;
    if(FilenameUtils.getExtension(placeholderImageName).toLowerCase() == ContentTypes.EXTENSION_BMP) {
        imagePart = new ImageBmpPart(new PartName("/word/media/" + placeholderImageName));
    }
    else if([ContentTypes.EXTENSION_JPG_1, ContentTypes.EXTENSION_JPG_2].contains(FilenameUtils.getExtension(placeholderImageName).toLowerCase())) {
        imagePart = new ImageJpegPart(new PartName("/word/media/" + placeholderImageName));
    }
    else if(FilenameUtils.getExtension(placeholderImageName).toLowerCase() == ContentTypes.EXTENSION_PNG) {
        imagePart = new ImagePngPart(new PartName("/word/media/" + placeholderImageName));
    }

    InputStream stream = new FileInputStream(newImage);
    imagePart.setBinaryData(stream);

    if(FilenameUtils.getExtension(newImage.getName()).toLowerCase() == ContentTypes.EXTENSION_BMP) {
        imagePart.setContentType(new ContentType(ContentTypes.IMAGE_BMP));
    }
    else if([ContentTypes.EXTENSION_JPG_1, ContentTypes.EXTENSION_JPG_2].contains(FilenameUtils.getExtension(newImage.getName()).toLowerCase())) {
        imagePart.setContentType(new ContentType(ContentTypes.IMAGE_JPEG));
    }
    else if(FilenameUtils.getExtension(newImage.getName()).toLowerCase() == ContentTypes.EXTENSION_PNG) {
        imagePart.setContentType(new ContentType(ContentTypes.IMAGE_PNG));
    }

    imagePart.setRelationshipType(Namespaces.IMAGE);

    final String embedId = rel.getId();

    rel = document.getMainDocumentPart().addTargetPart(imagePart);
    rel.setId(embedId);
}

检查功能:

@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 {}

但是当我在设备上尝试此操作时,没有响应?我尝试使用离子发球机->出现错误Object(...)不是函数

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

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()用于异步操作,因此您需要在showAlert(titleStr)内部调用方法.then()