randdusing / cordova-plugin-bluetoothle

Bluetooth Low Energy Phonegap Plugin
803 stars 353 forks source link

Advertising and Discovery #470

Open joewoodhouse opened 6 years ago

joewoodhouse commented 6 years ago

Hi all,

I'm trying to integrate this plugin into a cordova/ionic application with the aim of registering it as a peripheral. The code seems to run and I startAdvertising successfully, however I'm not able to detect my device over bluetooth from any other device. Could someone advise me what I'm doing wrong? Perhaps my expectations are wrong and I have misunderstood some portion of BLE.

Code here:

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { HomePage } from '../pages/home/home';
declare var window: any;

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = HomePage;

  advertisingParams = {
    services: ['1234'],
    service: '1234',
    name: "MatrixV2T",
    mode: "lowLatency",
    connectable: true,
    timeout: 500,
    powerLevel: "high",
    manufacturerId: 0x004C,
    manufacturerSpecificData: "Rand"
  }

  serviceParams = {
    service: "1234",
    characteristics: [
      {
        uuid: "ABCD",
        permissions: {
          read: true,
          write: true,
          //readEncryptionRequired: true,
          //writeEncryptionRequired: true,
        },
        properties: {
          read: true,
          writeWithoutResponse: true,
          write: true,
          notify: true,
          indicate: true,
          //authenticatedSignedWrites: true,
          //notifyEncryptionRequired: true,
          //indicateEncryptionRequired: true,
        }
      }
    ]
  };

  private async bleInit() {
    return new Promise((resolve, reject) => window.bluetoothle.initialize(resolve, reject));
  }

  private async bleInitPeripheral() {
    return new Promise((resolve, reject) => window.bluetoothle.initializePeripheral(resolve, reject, {
      request: true,
      restoreKey: "bluetoothleplugin"
    }));
  }

  private async bleStartAdvertising() {
    return new Promise((resolve, reject) => window.bluetoothle.startAdvertising(resolve, reject, this.advertisingParams));
  }

  private async bleAddService() {
    return new Promise((resolve, reject) => window.bluetoothle.addService(resolve, reject, this.serviceParams));
  }

  private async getAdapterInfo() {
    return new Promise((resolve, reject) => window.bluetoothle.getAdapterInfo(resolve, reject));
  }

  async init() {
    console.log(await this.bleInit());
    console.log(await this.bleInitPeripheral());
    console.log(await this.bleAddService());
    console.log(await this.bleStartAdvertising());
    console.log(await this.getAdapterInfo());
  }

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
      this.init();
    });
  }
}

Output:

Object
status:"enabled"

Object
status:"enabled"

Object
service:"00001234-0000-1000-8000-00805f9b34fb"
status:"serviceAdded"

Object
isConnectable:true
mode:2
status:"advertisingStarted"
timeout:500
txPowerLevel:2

Object
address:"02:00:00:00:00:00"
isDiscoverable:false
isEnabled:true
isInitialized:true
isScanning:false
name:"Nexus 6P"

I notice that "isDiscoverable" is always false.

Any help warmly appreciated!

joewoodhouse commented 6 years ago

Anyone? :(

andres-vasquez commented 6 years ago

Try with this advertising params: mode: "balanced", connectable: true, timeout: 20000, powerLevel: "high", includeDeviceName: true