android - IONIC 电容器 BLE 插件扫描不起作用

标签 android ionic-framework bluetooth-lowenergy capacitor ionic5

我正在学习 ionic 5,想要创建一个简单的应用程序,显示附近所有支持蓝牙的手机的列表。我的问题是,当我使用 BLE 插件的调用扫描时,我的回调方法永远不会被调用。我在连接的手机(Samsung 9)上进行了测试,还通过生成 app-debug.apk 并安装在手机上进行了测试。

这是我的项目的详细信息。我正在将 Capacitor 用于 native 应用程序。

SDK: enter image description here

tab-1.ts

import { Component, NgZone } from '@angular/core';
import { BLE } from '@ionic-native/ble/ngx';
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
text = 'hello';
devices: any[] = [];
constructor(private ble: BLE,
            private ngZone: NgZone,
            public alertController: AlertController) { }
 scan() {
  this.text = 'Loading...';
  console.log('going to this.scan.....');
  this.devices = [];
  this.showAlert('starting scan.....');


  this.ble.scan([], 60).subscribe(devices1=>{
    this.showDeviceList(devices1);
    this.text = devices1;
    this.showAlert('scan finished success');
  },error=> this.showAlert('scan fini with error'), 
  ()=>this.showAlert('scan void finish'));
}

async showDeviceList(devices) {
  const alert = await this.alertController.create({
    header: 'Alert',
    subHeader: 'Subtitle',
    message: 'Going to start scan',
    buttons: ['OK']
  });
  await alert.present();
  console.log('devices are ', devices);
  this.ngZone.run(() => {
    this.devices.push(...devices);
    this.text = 'finished';
  });
}

async showAlert(msg){
  const alert = await this.alertController.create({
    header: 'Alert',
    subHeader: 'Subtitle',
    message: msg,
    buttons: ['OK']
  });
  await alert.present();
}
}

ionic 信息:

enter image description here

Package.json:

{
  "name": "COVID-TRACKING",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~8.2.14",
    "@angular/core": "~8.2.14",
    "@angular/forms": "~8.2.14",
    "@angular/platform-browser": "~8.2.14",
    "@angular/platform-browser-dynamic": "~8.2.14",
    "@angular/router": "~8.2.14",
    "@capacitor/android": "^2.0.1",
    "@capacitor/core": "2.0.1",
    "@ionic-native/ble": "^5.23.0",
    "@ionic-native/core": "^5.0.7",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic/angular": "^5.0.0",
    "cordova-plugin-ble-central": "^1.2.4",
    "core-js": "^2.5.4",
    "rxjs": "~6.5.1",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.20",
    "@angular/cli": "~8.3.23",
    "@angular/compiler": "~8.2.14",
    "@angular/compiler-cli": "~8.2.14",
    "@angular/language-service": "~8.2.14",
    "@capacitor/cli": "2.0.1",
    "@ionic/angular-toolkit": "^2.1.1",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  },
  "description": "An Ionic project"
}

安装插件的命令:

npm install cordova-plugin-ble-central
npm install @ionic-native/ble
ionic cap sync

最佳答案

此问题仅适用于 Android 10。您需要 Android 10 中的后台位置权限才能使蓝牙正常工作。您需要将后台位置权限添加到 AndroidManifest.xml,并且还需要将后台地理定位插件添加到您的应用程序,即使您不打算使用它。在你的 app.component 中做这样的事情

const config: BackgroundGeolocationConfig = {
    desiredAccuracy: 10,
    stationaryRadius: 20,
    distanceFilter: 30,
    debug: true, //  enable this hear sounds for background-geolocation life-cycle.
    stopOnTerminate: false, // enable this to clear background location settings when the app terminates
  };

  this.bgLoc.stop();

完成上述操作后,这将自动请求后台位置权限,并允许蓝牙在 Android 10 上工作。另请确保将粗略位置权限添加到 androidmanifest.xml

关于android - IONIC 电容器 BLE 插件扫描不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61180374/

相关文章:

c++ - 从非托管 DLL 调用 UWP api

java - 按分隔符数量拆分字符串

Android:ListActivity 的正确线程

java - 我的 Android Studio 项目中出现什么运行时错误?

ionic-framework - 如何在所有页面上显示 Ionic Tabs?

android - 类未找到异常 : unable to find class "android.support.v4.util.ArrayMap"

android - 在 BLE Android 中写入后无法获得读取特性

android - 使用 CursorAdapter 维护 ListView 中的复选框状态

ios - 在 iOS 上使用 Cordova 日历插件列出日历事件

ionic-framework - 在 Ionic 4 中更改 ionic 输入下划线颜色