cordova - 获取蓝牙设备列表并使用 IONIC cordova bluetoothle 插件进行连接
问题描述
我想获取附近蓝牙设备的列表并与之建立连接,为此我正在使用 IONIC 科尔多瓦蓝牙插件。但它没有按预期工作,我收到一条错误消息,上面写着“扫描蓝牙低功耗设备时出错”。
这是我的代码:
import { Component, OnInit, NgZone } from '@angular/core';
import { BluetoothLE } from '@ionic-native/bluetooth-le/ngx';
import { Platform, ToastController } from '@ionic/angular';
@Component({
selector: 'app-bluetooth-connect',
templateUrl: './bluetooth-connect.page.html',
styleUrls: ['./bluetooth-connect.page.scss'],
})
export class BluetoothConnectPage implements OnInit {
statusMessage: string;
constructor(
public bluetoothle: BluetoothLE,
public plt: Platform,
private toastController: ToastController,
private ngZone: NgZone,
) {
this.plt.ready().then((readySource) => {
console.log('Platform ready from', readySource);
this.bluetoothle.initialize().subscribe(ble => {
console.log('ble', ble.status); // logs 'enabled'
this.bluetoothle.enable();
});
});
}
adapterInfo() {
this.bluetoothle.getAdapterInfo().then((success) => {
console.log('adapterInfo: ' + success);
this.setStatus(success.name);
});
}
startScan() {
const params = {
services: [
'180D',
'180F'
],
allowDuplicates: true,
scanMode: this.bluetoothle.SCAN_MODE_LOW_LATENCY,
matchMode: this.bluetoothle.MATCH_MODE_AGGRESSIVE,
matchNum: this.bluetoothle.MATCH_NUM_MAX_ADVERTISEMENT,
callbackType: this.bluetoothle.CALLBACK_TYPE_ALL_MATCHES,
};
this.bluetoothle.startScan(params).subscribe((success) => {
console.log('startScan: ' + JSON.stringify(success));
this.setStatus(success.address);
}, (error) => {
console.log('error: ' + error);
this.scanError(error);
});
}
handleError(error) {
console.log('handle error executed');
}
stopScan() {
this.bluetoothle.stopScan().then((resp) => {
console.log('stopScan: ' + resp);
this.setStatus(resp.status);
});
}
retrieveConnected() {
const params = {
services: [
'180D',
'180F'
]
};
this.bluetoothle.retrieveConnected(params).then((resp) => {
console.log('retrieveConnected: ' + resp);
this.setStatus('retrieveConnected');
});
}
// If location permission is denied, you'll end up here
async scanError(error: string) {
this.setStatus('Error ' + error);
const toast = await this.toastController.create({
message: 'Error scanning for Bluetooth low energy devices',
position: 'middle',
duration: 5000
});
toast.present();
}
setStatus(message: string) {
console.log('message: ' + message);
this.ngZone.run(() => {
this.statusMessage = message;
});
}
ngOnInit() {
}
}
这是我的模板代码:
<ion-header>
<ion-toolbar>
<ion-title>bluetooth-connect</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-button (click)="adapterInfo()">AdapterInfo</ion-button>
<ion-button (click)="startScan()">StartScan</ion-button>
<ion-button (click)="stopScan()">StopScan</ion-button>
<ion-button (click)="retrieveConnected()">RetrieveConnected</ion-button>
</ion-content>
<ion-footer>
<ion-toolbar>
<p>{{ statusMessage }}</p>
</ion-toolbar>
</ion-footer>
请帮忙。
解决方案
推荐阅读
- python - pandas 数据框的某些标头显示为属性,但在 VSCode 中的“调试”>“变量”中没有全部显示
- php - 成功登录亚马逊账户授权后,亚马逊支付出现“AccessDenied”错误
- typescript - 带有 typescript 的 React 路由器 v5 上所需的 url 参数,可以是未定义的
- javascript - 如何在Vue中为v-for创建的每个按钮组设置accesskey?
- azure - Azure IoT Hub - 检查端口 5671 是否打开
- php - 如何从 laravel 控制器传递嵌套的 foreach 以在刀片上查看
- apache-kafka - 是否可以启用或禁用特定的 JMX 指标以使用 Jconsole 监控 Kafka 集群
- reactjs - 学习 Redux + react-redux 的一些问题
- php - 我想在 laravel 查询中进行 mysqli 查询。时间计数
- tomcat - 代理 IIS 服务器到 Tomcat 应用程序