如何在Android上获取蓝牙MAC地址

时间:2014-09-03 20:32:23

标签: android bluetooth

我使用以下代码在Android上获取蓝牙的MAC地址:

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());

另外,我向Manifest添加了权限:

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

但是,我的应用程序意外停止了。我的代码出了什么问题?

使用Android 2.3.6(三星Galaxy mini)

更新:我在Class扩展AsyncTask内使用了此代码。

栈跟踪

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

2 个答案:

答案 0 :(得分:3)

由于Android 6之后的限制,无法通过上述解决方案获得蓝牙MAC。我遇到了这个并且运行良好。

 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;
}

答案 1 :(得分:1)

This answer建议您在活动BluetoothAdapter.getDefaultAdapter();中致电onCreate,以便对其进行初始化,然后进行任何后续调用。

但是,要与AsynTask一起使用,您必须使用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());
    }
});