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_STATE
permission 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
Thanks in advance
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With