简体   繁体   中英

How to get Bluetooth MAC Address on Android

I have used the following code to get MAC Address of Bluetooth on Android:

BluetoothAdapter ba;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    ba = (BluetoothAdapter) getSystemService(Context.BLUETOOTH_SERVICE);
} else {
    ba = BluetoothAdapter.getDefaultAdapter();
}
Log.i("TEST", ba.getAddress());

Also, I added the permission to the Manifest:

<uses-permission android:name="android.permission.BLUETOOTH"/>

But, I my app has stopped unexpectedly. What is wrong with my code?

using Android 2.3.6 (Samsung Galaxy mini)

Update: I have used this code inside a Class extended AsyncTask .

StackTrace :

09-04 01:09:43.671: E/AndroidRuntime(12372): FATAL EXCEPTION: AsyncTask #1
09-04 01:09:43.671: E/AndroidRuntime(12372): java.lang.RuntimeException: An error occured while executing doInBackground()
09-04 01:09:43.671: E/AndroidRuntime(12372):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at java.lang.Thread.run(Thread.java:1019)
09-04 01:09:43.671: E/AndroidRuntime(12372): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-04 01:09:43.671: E/AndroidRuntime(12372):    at android.os.Handler.<init>(Handler.java:121)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at android.bluetooth.BluetoothAdapter$1.<init>(BluetoothAdapter.java:1117)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at android.bluetooth.BluetoothAdapter.<init>(BluetoothAdapter.java:1117)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at android.bluetooth.BluetoothAdapter.getDefaultAdapter(BluetoothAdapter.java:437)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at org.radyabi.app.StepActivity$CollectDeviceCodes.doInBackground(StepActivity.java:119)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at org.radyabi.app.StepActivity$CollectDeviceCodes.doInBackground(StepActivity.java:1)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-04 01:09:43.671: E/AndroidRuntime(12372):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
09-04 01:09:43.671: E/AndroidRuntime(12372):    ... 4 more

It is not possible to get Bluetooth MAC with the solution above due to the restrictions after Android 6. I came across with this and works perfectly.

 private String getBluetoothMac(final Context context) {

    String result = null;
    if (context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH)
            == PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Hardware ID are restricted in Android 6+
            // https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id
            // Getting bluetooth mac via reflection for devices with Android 6+
            result = android.provider.Settings.Secure.getString(context.getContentResolver(),
                    "bluetooth_address");
        } else {
            BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
            result = bta != null ? bta.getAddress() : "";
        }
    }
    return result;
}

This answer suggests that you call BluetoothAdapter.getDefaultAdapter(); in your activity's onCreate so it is initialized and any subsequent calls will go fine.

However, for using with AsynTask , you have to use runOnUiThread :

runOnUiThread(new Runnable() {
    public void run() {
        BluetoothAdapter ba;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            ba = (BluetoothAdapter) getSystemService(Context.BLUETOOTH_SERVICE);
        } else {
            ba = BluetoothAdapter.getDefaultAdapter();
        }
        Log.i("TEST", ba.getAddress());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM