Alvaro Cortizo
Alvaro Cortizo

Reputation: 11

Bluetooth (BLE) device not appears in Ionic Capacitor App

I have an Ionic Capacitor application with the @capacitor-community/bluetooth-le plugin. The operating system detects the beacon, but my app does not. The issue only occurs on devices with Android 13.

This is my ionic info output

Ionic:

   Ionic CLI                     : 7.1.5 (/usr/local/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 7.5.5
   @angular-devkit/build-angular : 17.0.1
   @angular-devkit/schematics    : 17.0.1
   @angular/cli                  : 17.0.1
   @ionic/angular-toolkit        : 9.0.0

Capacitor:

   Capacitor CLI      : 5.5.1
   @capacitor/android : 5.5.1
   @capacitor/core    : 5.5.1
   @capacitor/ios     : not installed

Utility:

   cordova-res (update available: 0.15.4) : 0.15.3
   native-run (update available: 2.0.0)   : 1.7.4

System:

   NodeJS : v20.9.0 (/usr/local/bin/node)
   npm    : 10.1.0

This is my Java version java version "1.8.0_361" Java(TM) SE Runtime Environment (build 1.8.0_361-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.361-b09, mixed mode)

This is my app.component.ts file

import { Component, OnInit } from '@angular/core';
import { Capacitor } from '@capacitor/core';
import { BleClient } from '@capacitor-community/bluetooth-le';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent implements OnInit {
  isLocationEnabled: any;
  isBLEEnabled: any;
  deviceId: any;
  ble: boolean = false;
  devices: any[] = [];
  response: any;

  constructor(
  ) {}

  async ngOnInit() {
  try {
    if (Capacitor.getPlatform() == 'android'){
      this.isLocationEnabled = await BleClient.isLocationEnabled();
    } else {
      this.isLocationEnabled = true;
    }
    await BleClient.initialize();
    this.isBLEEnabled = await BleClient.isEnabled();
    if (this.isBLEEnabled && this.isLocationEnabled) {
      this.ble = true;
      this.startScanning();
    }
    } catch (error) {
      console.error(error)
    }
  }

  startScanning() {
      BleClient.requestLEScan({ allowDuplicates: false }, (res1) => {
        console.log('Device found', res1)  
      });
  }
}

Upvotes: 1

Views: 1632

Answers (1)

Michael Kotzjan
Michael Kotzjan

Reputation: 2673

I just had a look at the bluetooth-le capacitor plugins GitHub page and found something interesting that might explain your problem:

The installation guide contains a section regarding Bluetooth Permissions for Android 12 and above that mentions the androidNeverForLocation flag with this hint:

Note: If you include neverForLocation in your android:usesPermissionFlags, some BLE beacons are filtered from the scan results.

This is also explained in the Android Guide.

My guess is that you did set the flag and therefore the beacon can not be found.

Upvotes: 0

Related Questions