@ionic-native/ble/ngx startScan([serviceUUID]) never finds my peripheral

I’m using @ionic-native/ble/ngx but it won’t connect to my peripheral. I’ve double-checked the service UUID, can view it in LightBlue. When I was testing earlier, I had placed the device’s MAC in connect() and that worked. But not this. According to the docs, startScan([this.serviceUUID]) should work.

Note that I did not add anything to app.module.ts or setup.module.ts. It worked fine when using the MAC, as mentioned earlier.

Also, I’m still kinda a newbie at this.

I am using a Galaxy S6. Please assist.

setup.page.ts:

import { Component, OnInit } from '@angular/core';
import { BLE } from '@ionic-native/ble/ngx';

@Component({
  selector: 'app-setup',
  templateUrl: './setup.page.html',
  styleUrls: ['./setup.page.scss'],
})
export class SetupPage implements OnInit {
  private serviceUUID = '2fe11a47-e4b8-4522-9cff-aa729b8215c0';
  public bluetooth_enabled: boolean;
  public bluetooth_connected: boolean;

  constructor(private ble: BLE) { }

  ngOnInit() {
    this.bluetooth_enabled = false;
    this.bluetooth_connected = false;
    console.log('[DEBUG] this.bluetooth_enabled', this.bluetooth_enabled);
    console.log('[DEBUG] this.bluetooth_connected', this.bluetooth_connected);
    this.ble.enable().then(() => {
      this.ble.isEnabled().then(() => {
        this.bluetooth_enabled = true;
        console.log('[DEBUG] this.bluetooth_enabled', this.bluetooth_enabled);
        this.ble.startScan([this.serviceUUID]).subscribe(device => {
          console.log('[DEBUG] device.id', device.id);
          this.ble.autoConnect(device.id, () => {
            this.ble.stopScan();
            this.bluetooth_connected = true;
            console.log('[DEBUG] this.bluetooth_connected', this.bluetooth_connected);
          },
            // Disconnect callback
            () => { });
        });
      });
    });
  }
}

setup.page.html:

<ion-header>
  <ion-toolbar>
    <ion-title>
      Setup
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <div *ngIf="bluetooth_enabled">Bluetooth enabled</div><br />
  <div *ngIf="bluetooth_connected">Bluetooth connected</div>
</ion-content>

Console:

2020-07-05 01:29:22.991 32548-32548/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 239 - Msg: [DEBUG] this.bluetooth_enabled false
2020-07-05 01:29:22.992 32548-32548/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 240 - Msg: [DEBUG] this.bluetooth_connected false
2020-07-05 01:29:23.202 32548-32548/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 244 - Msg: [DEBUG] this.bluetooth_enabled true

Output on the phone:

Bluetooth enabled

Also tried a different method, looking at the device name, since my peripheral’s name always starts with the same string.

In testing this, this method did work at least once, so I know the device.name property is in fact being read. But now it only sees a device without a name, probably a tablet somewhere in the house.

setup.page.ts

import { Component, OnInit } from '@angular/core';
import { BLE } from '@ionic-native/ble/ngx';

@Component({
  selector: 'app-setup',
  templateUrl: './setup.page.html',
  styleUrls: ['./setup.page.scss'],
})
export class SetupPage implements OnInit {
  public bluetooth_enabled: boolean;
  public bluetooth_connected: boolean;

  constructor(private ble: BLE) { }

  ngOnInit() {
    this.bluetooth_enabled = false;
    this.bluetooth_connected = false;
    console.log('[DEBUG] this.bluetooth_enabled', this.bluetooth_enabled);
    console.log('[DEBUG] this.bluetooth_connected', this.bluetooth_connected);
    this.ble.enable().then(() => {
      this.ble.isEnabled().then(() => {
        this.bluetooth_enabled = true;
        console.log('[DEBUG] this.bluetooth_enabled', this.bluetooth_enabled);
        this.ble.scan([], 60).subscribe(device => {
          console.log('[DEBUG] device.name', JSON.stringify(device.name));
          if (device.name.startsWith('SecureCoop_')) {
            console.log('[DEBUG] device.name.startsWith(SecureCoop_)');
            this.ble.connect(device.id).subscribe(peripheral => {
              this.bluetooth_connected = true;
              console.log('[DEBUG] this.bluetooth_connected', this.bluetooth_connected);
              console.log('[DEBUG] peripheral: ', JSON.stringify(peripheral));
            });
          } else {
            console.log('[DEBUG] !device.name.startsWith(SecureCoop_)');
          }
        });
      });
    });
  }
}

setup.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>
      Setup
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <div *ngIf="bluetooth_enabled">Bluetooth enabled</div><br />
  <div *ngIf="bluetooth_connected">Bluetooth connected</div>
</ion-content>

Console:

2020-07-05 02:25:18.755 21605-21605/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 238 - Msg: [DEBUG] this.bluetooth_enabled false
2020-07-05 02:25:18.756 21605-21605/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 239 - Msg: [DEBUG] this.bluetooth_connected false
2020-07-05 02:25:18.999 21605-21605/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 243 - Msg: [DEBUG] this.bluetooth_enabled true
2020-07-05 02:25:19.215 21605-21605/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 245 - Msg: [DEBUG] device.name undefined

There it is, this second method worked again. But only once. So it’s intermittent. Hmmmm.

Console:

2020-07-05 02:39:35.599 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 238 - Msg: [DEBUG] this.bluetooth_enabled false
2020-07-05 02:39:35.600 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 239 - Msg: [DEBUG] this.bluetooth_connected false
2020-07-05 02:39:35.840 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 243 - Msg: [DEBUG] this.bluetooth_enabled true
2020-07-05 02:39:36.026 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 245 - Msg: [DEBUG] device.name "SecureCoop_D256CC9E"
2020-07-05 02:39:36.026 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 247 - Msg: [DEBUG] device.name.startsWith(SecureCoop_)
2020-07-05 02:39:36.134 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 245 - Msg: [DEBUG] device.name undefined
2020-07-05 02:39:40.961 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 250 - Msg: [DEBUG] this.bluetooth_connected true
2020-07-05 02:39:40.962 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 251 - Msg: [DEBUG] peripheral:  {"name":"SecureCoop_D256CC9E","id":"B8:27:EB:FC:66:34","advertising":{},"rssi":-61,"services":["1800","1801","2fe11a47-e4b8-4522-9cff-aa729b8215c0"],"characteristics":[{"service":"1800","characteristic":"2a00","properties":["Read"]},{"service":"1800","characteristic":"2a01","properties":["Read"]},{"service":"1801","characteristic":"2a05","properties":["Indicate"],"descriptors":[{"uuid":"2902"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c1","properties":["Read"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c2","properties":["Read"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c3","properties":["Read"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c4","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c5","properties":["Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c6","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c7","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c8","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215c9","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215ca","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215cb","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215cc","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215cd","properties":["Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215ce","properties":["Read","Notify"],"descriptors":[{"uuid":"2902"},{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215cf","properties":["Read","Notify"],"descriptors":[{"uuid":"2902"},{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215d0","properties":["Read","Write"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215d1","properties":["Read"],"descriptors":[{"uuid":"2901"}]},{"service":"2fe11a47-e4b8-4522-9cff-aa729b8215c0","characteristic":"2fe11a47-e4b8-4522-9cff-aa729b8215d2","properties":["Read","Notify"],"descriptors":[{"uuid":"2902"},{"uuid":"2901"}]}]}
2020-07-05 02:40:23.239 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 238 - Msg: [DEBUG] this.bluetooth_enabled false
2020-07-05 02:40:23.240 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 239 - Msg: [DEBUG] this.bluetooth_connected false
2020-07-05 02:40:23.401 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 243 - Msg: [DEBUG] this.bluetooth_enabled true
2020-07-05 02:40:23.600 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 245 - Msg: [DEBUG] device.name undefined
2020-07-05 02:40:46.897 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 238 - Msg: [DEBUG] this.bluetooth_enabled false
2020-07-05 02:40:46.898 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 239 - Msg: [DEBUG] this.bluetooth_connected false
2020-07-05 02:40:47.044 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 243 - Msg: [DEBUG] this.bluetooth_enabled true
2020-07-05 02:40:47.271 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 245 - Msg: [DEBUG] device.name undefined
2020-07-05 02:40:56.140 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 238 - Msg: [DEBUG] this.bluetooth_enabled false
2020-07-05 02:40:56.141 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 239 - Msg: [DEBUG] this.bluetooth_connected false
2020-07-05 02:40:56.296 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 243 - Msg: [DEBUG] this.bluetooth_enabled true
2020-07-05 02:40:56.504 24700-24700/com.ionicthemes.ionic5fullapp I/Capacitor/Console: File: http://localhost/setup-setup-module-ngfactory-es2015.js - Line 245 - Msg: [DEBUG] device.name undefined

Tried a different peripheral, a virtual peripheral on my iPhone, same result. Seems the root problem is scan() doesn’t always show every device. I’ll probably have to raise a bug report with the plugin developer.

Anyone else seeing this?

By the way, I’m using a Galaxy S6.

Current code:

import { Component, OnInit } from '@angular/core';
import { BLE } from '@ionic-native/ble/ngx';

@Component({
  selector: 'app-setup',
  templateUrl: './setup.page.html',
  styleUrls: ['./setup.page.scss'],
})
export class SetupPage implements OnInit {
  public bluetooth_enabled: boolean;
  public bluetooth_connected: boolean;

  constructor(private ble: BLE) { }

  ngOnInit() {
    this.bluetooth_enabled = false;
    this.bluetooth_connected = false;
    console.log('[DEBUG] this.bluetooth_enabled', this.bluetooth_enabled);
    console.log('[DEBUG] this.bluetooth_connected', this.bluetooth_connected);
    this.ble.enable().then(() => {
      this.ble.isEnabled().then(() => {
        this.bluetooth_enabled = true;
        console.log('[DEBUG] this.bluetooth_enabled', this.bluetooth_enabled);
        this.ble.startScan([]).subscribe(device => {
          this.ble.refreshDeviceCache(device.id, 1000);
          console.log('[DEBUG] device.name', JSON.stringify(device.name));
          if (device.name.startsWith('SecureCoop_')) {
            console.log('[DEBUG] device.name.startsWith(SecureCoop_)');
            this.ble.connect(device.id).subscribe(peripheral => {
              this.ble.isConnected(device.id).then(() => {
                this.bluetooth_connected = true;
                console.log('[DEBUG] this.bluetooth_connected', this.bluetooth_connected);
                console.log('[DEBUG] peripheral: ', JSON.stringify(peripheral));
                this.ble.stopScan();
              });
            });
          }
        });
      });
    });
  }
}

Different device (Galaxy S3 with Lineage), same exact issue.

Solved my problem. I was doing device.name.startswith() and it was bombing when device was undefined. But I didn’t see that because in Android studio, I was filtering on the string ‘debug’. So it was an ID-ten-T error.