Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Bluetooth MAC Address on Ionic 3 returns null

I am currently developing an Ionic 3 application where I need to get the phone's Bluetooth MAC address (or something that would identify the device inside a Bluetooth network) on an Android device and I have been trying to obtain the MAC address (or UUID etc.) using the Uid native plugin for Ionic, along with the native plugin for AndroidPermissions. I am currently using the method suggested at the plugins homepage https://ionicframework.com/docs/native/uid/

This is the code for getting the MAC address of the device:

async getMAC() {
   const { hasPermission } = await this.androidPermissions.checkPermission(
     this.androidPermissions.PERMISSION.READ_PHONE_STATE
   );

   if (!hasPermission) {
     const result = await this.androidPermissions.requestPermission(
       this.androidPermissions.PERMISSION.READ_PHONE_STATE
     );

     if (!result.hasPermission) {
       throw new Error('Permissions required');
     }

     return;
   }

    return this.uidNative.MAC;
  }

I have also added the READ_PHONE_STATEpermission to my AndroidManifest.xml and setup the according providers for Ionic. My phone requests for permission normally.

My problem

After requesting for permission the above Promise is resolved successfully but it returns null and I cannot figure out why this happens

  1. Is there any workaround for this? Did anyone had similar problems?
  2. Is there another way to get the MAC address of the android device and passing it to the Ionic Application?

Thanks in advance

like image 866
bolzano Avatar asked Feb 14 '18 11:02

bolzano


1 Answers

Solution:

Write a cordova plugin to fetch the MAC Address using the Phone Settings

String mac = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address")

Then invoke this call through Ionic 3.

A fast way to do: Alter the https://ionicframework.com/docs/native/device/ Ionic Native Plugin.

FYI have a look at this altered Device.java gist we did with a friend of mine: https://gist.github.com/giannisdaras/04126edcae32d597cf54e7b2cae680ac

like image 139
bolzano Avatar answered Sep 25 '22 01:09

bolzano