9
votes

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

1
What happens when you call it the second time?Tarun Lalwani
The docs explicitly state that you'll have to restart the app after permissions are granted. Does your code work after restarting the app?Phonolog
This, sadly, does not work. I just solved the issue today and I will post the reply soon.bolzano

1 Answers

3
votes

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