4

what i am trying to do : using https://github.com/don/cordova-plugin-ble-central plugin creating a android app using ionic and need to get the rssi data and advertising data also continuously even in screen lock also.

what i did : right now i am getting the data after pressing the scan button and rssi data and advertising data in buffer

what i need :

  1. How to get/ scan the bluetooth devices continuously including their advertising data ?
  2. How to scan even though the screen is locked or app is in background ?

below is my code:

html code:

<ion-header>
  <ion-toolbar>
    <ion-title>
      BLE Scanner
    </ion-title>
    <ion-button (click)="Scan()" slot="end">
      <ion-icon name="Bluetooth"></ion-icon>
      <p>Scan</p>
    </ion-button>
  </ion-toolbar>
</ion-header>

<ion-content>
  <ion-list>
    <ion-card-content ion-item *ngFor="let device of devices">
      <p>{{device.name || 'Unnamed'}}</p>
      <p>{{device.id}}</p>
      <p> RSSI: {{device.rssi}}</p>
    </ion-card-content>
  </ion-list>
</ion-content>

.ts code :

import { BLE } from '@ionic-native/ble/ngx';


  devices: any[] = [];

  constructor(private ble: BLE, private ngZone: NgZone) {

  }
  Scan() {
    this.devices = [];
    this.ble.scan([],5).subscribe(
      device => this.onDeviceDiscovered(device)
    );
  }
  onDeviceDiscovered(device) {
    console.log('Discovered' + JSON.stringify(device, null, 2));
    this.ngZone.run(() => {
      this.devices.push(device)
      console.log(device)
      let  adData = new Uint8Array(device.advertising);
      console.log(adData);
    })
  }
Madpop
  • 729
  • 4
  • 24
  • 61

2 Answers2

1

How to get/ scan the bluetooth devices continuously including their advertising data ?

According to the plugin doc:

https://github.com/don/cordova-plugin-ble-central#scan

{
    "name": "TI SensorTag",
    "id": "BD922605-1B07-4D55-8D09-B66653E51BBA",
    "rssi": -79,
    "advertising": /* ArrayBuffer or map */
}

the plugin should provide you BLE device should provide advertisement either your decoding of buffer must be not right or else the device is not advertising the packets.

How to scan even though the screen is locked or app is in background ?

you can try this plugin for background mode scan

https://ionicframework.com/docs/native/background-mode

https://github.com/katzer/cordova-plugin-background-mode

Mohan Gopi
  • 7,606
  • 17
  • 66
  • 117
  • with out background mode plugin will the ble central wil emit values in background ? if so up to how much time ? – Madpop May 13 '20 at 04:13
1

How to scan even though the screen is locked or app is in background ?

As suggested by Mohan Gopi a background-mode plugin could help, but it is not the whole solution starting from Android 8.1

BLE scan is not working when screen is off on Android 8.1.0

As you can read scan with a locked screen is allowed only with filters and cordova-plugin-ble-central doesn't allow to set an empty filter (at least I have not found it).

The solution is to set an empty filter, but I am still looging for a plugin that allows this.