首页 > 解决方案 > 无法发现 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但不能真正使该设备被任何其他设备发现。

难道我做错了什么?

标签: ionic-frameworkbluetooth-lowenergycordova-plugins

解决方案


推荐阅读