如何解决离子 BLE 外设无法被发现
我的包括:
import { BluetoothLE } from '@ionic-native/bluetooth-le/ngx';
我在安卓设备上激活服务的代码:
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,但无法真正使任何其他设备发现该设备。
我做错了吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。