ionic-framework - 无法发现 Ionic BLE 外围设备
问题描述
我的包括:
import { BluetoothLE } from '@ionic-native/bluetooth-le/ngx';
我在 android 设备上激活服务的代码是可发现的:
this.ble.initialize({request:true, statusReceiver: true, restoreKey: 'appname'}).subscribe((res:any)=>{
// init as peripheral
this.ble.initializePeripheral({request:true})
.subscribe(async (res:any)=>{
switch(res.status){
case 'enabled':
console.log("enabled BLE");
await this.ble.stopAdvertising(); // stop advertising even before this, the device cannot be seen
await this.ble.removeAllServices(); // remove all previous services
await this.AddService(); // add my new service
const info = await this.ble.getAdapterInfo();
console.log('INFO: ',JSON.stringify(info)); // get info about device (here it returns that i'm not discoverable)
await this.StartAdvertising(info.name); // start advertising again => return 'Too many advertisers' and therefore stops
break;
default:
console.log("got other status: ",res.status);
}
},
(err:any)=>{
this.status = "Problem";
console.log("BLE ERROR: ",JSON.stringify(err));
});
});
其他功能:
async AddService(){
return this.ble.addService({
service:'7459',
characteristics:[{
uuid:'7459',
permissions: {read: true, write: true },
properties : {
read: true,
writeWithoutResponse: true,
write: true,
notify: true,
indicate: true,
}
}]
}).then(res=>{
// always succeds -> service is added, but cannot be adverised
console.log("BLE service added: ",JSON.stringify(res));
})
.catch(err=>{
console.log("BLE service error: ", JSON.stringify(err));
});
}
async StartAdvertising(name:string='Badge'){
await this.ble.startAdvertising({
services:['7459'],
service:'7459',
name:name
}).then((res:any)=>{
console.log("Started advertising: ",JSON.stringify(res));
}).catch(err=>{
console.log("BLE advertising error: ", JSON.stringify(err));
// always returns error
});
}
因此,如评论中所述,我在尝试将服务宣传为外围设备时遇到问题。我一直在关注此处提供的示例:https ://github.com/don/cordova-plugin-bluetoothle和此处:https ://github.com/don/cordova-plugin-ble-peripheral但不能真正使该设备被任何其他设备发现。
难道我做错了什么?
解决方案
推荐阅读
- javascript - 阴影只持续一秒钟
- init - 如何在 Alpine Linux 上启动服务?
- r - 使用 rtweet 的 ts_plot() 绘制推文时时区不起作用
- java - 如何使 Spring Service 检查认证?
- arrays - 拆分数组,然后按日期对部分进行排序
- python - 正则表达式替换除模式之外的所有内容
- python - 从 pandas datetimeindex 获取每个月的累积工作日列
- c# - C# EM_CHARFROMPOS 如何将一个点投射到 Intptr?
- python - 正则表达式检查python中的密码强度?
- node.js - `isomorphic-fetch` 和 `isomorphic-unfetch` npm 包有什么区别