Home > Mobile >  How do you get the bluetooth MAC address android8.0, please!
How do you get the bluetooth MAC address android8.0, please!

Time:09-27

A lot of search on the net, can under 8.0, but in not more than 8.0, is there anyone who knows!

CodePudding user response:

Reflection

 
Private String getBluetoothMacAddress () {
BluetoothAdapter BluetoothAdapter=BluetoothAdapter. GetDefaultAdapter ();
String bluetoothMacAddress="";
If (android. OS. Build. VERSION. SDK_INT & gt;=android. OS. Build. VERSION_CODES. M) {
Try {
The Field mServiceField=bluetoothAdapter. GetClass (). GetDeclaredField (" mService ");
MServiceField. SetAccessible (true);

The Object btManagerService=mServiceField. Get (bluetoothAdapter);

If (btManagerService!=null) {
BluetoothMacAddress=(String) btManagerService. GetClass () getMethod (" getAddress "). The invoke (btManagerService);
}
} the catch (NoSuchFieldException e) {

} the catch (NoSuchMethodException e) {

} the catch (IllegalAccessException e) {

} the catch (InvocationTargetException e) {

}
} else {
BluetoothMacAddress=bluetoothAdapter. GetAddress ();
}
Return bluetoothMacAddress;
}

CodePudding user response:

Upstairs, 6.0 can be, but get less than 8.0, throw an exception

CodePudding user response:

Seems to make uncertain, I'm up for a long time

CodePudding user response:

What process?

If it is not reflection system apis abnormalities, see the GitHubhttps://github.com/tiann/FreeReflection

CodePudding user response:

references 4 floor wang can reply:
thrown exception?

If it is not abnormal reflection system apis, see the GitHubhttps://github.com/tiann/FreeReflection


Throw InvocationTargetException this exception, should not the reflection of the things,

CodePudding user response:

Cut a figure out ah, drunk, you this is to ask questions

CodePudding user response:

I was also a lot of trouble with this thing, give you some hints,
1. In Android8.1 Settings - & gt; The System - & gt; The About - & gt; The Status, & gt; In the Bluetooth address show, don't know if your version,
2. A careful look at the code is
 private void setBtStatus () {
BluetoothAdapter bluetooth=BluetoothAdapter. GetDefaultAdapter ();
If (bluetooth!=null & amp; & MBtAddress!=null) {
String address=bluetooth. IsEnabled ()? Bluetooth. GetAddress () : null;
if (! TextUtils. IsEmpty (address)) {
//Convert the address to lowercase for consistency with the wifi MAC address.
MBtAddress. SetSummary (address. ToLowerCase ());
The e (" AHU. MAIN_ACTIVITY ", "address=" + address. ToLowerCase ());
} else {
MBtAddress. SetSummary (mUnavailable);
}
}
}


Code in, vendor, mediatek, proprietary \ packages \ apps \ MtkSettings \ SRC \ com \ android \ Settings \ deviceinfo \ Status Java

You put with this code application system/priv - app/below can be obtained, and get in the same way as system, pay attention to the/system/pri - app, not the system/app

3. Can use the Settings. Secure. Get string (context. GetContentResolver (), "bluetooth_address"); Available, but you have to change the source code,
Add this MOVED_TO_GLOBAL. Add (Settings. Global. BLUETOOTH_ADDRESS); Can also,

You can give it a try,

CodePudding user response:

What you said is the traditional bluetooth, are android and IOS also learned to hide the bluetooth address?

CodePudding user response:

The building Lord, now resolved? My side is thrown directly InvocationTargetException this exception,
  • Related