首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取Marshmallow中的蓝牙本地mac地址

获取Marshmallow中的蓝牙本地mac地址
EN

Stack Overflow用户
提问于 2015-10-27 20:35:39
回答 9查看 43.6K关注 0票数 32

我的应用程序可以通过BluetoothAdapter.getDefaultAdapter().getAddress().获得它的设备MAC地址

现在,随着Marshmallow的出现,安卓系统正在返回02:00:00:00:00:00

我看到了一些链接(抱歉,不知道现在在哪里),上面说您需要添加附加权限。

代码语言:javascript
运行
复制
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"/> 

才能得到它。不过,这对我没有用。

是否需要额外的许可才能获得mac地址?

我不确定这是否与此相关,但舱单也包括

代码语言:javascript
运行
复制
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

那么有什么办法可以得到本地的蓝牙mac地址吗?

EN

Stack Overflow用户

发布于 2018-03-08 13:01:10

请使用以下代码获取蓝牙mac地址。如果有什么问题请告诉我。

代码语言:javascript
运行
复制
private String getBluetoothMacAddress() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    String bluetoothMacAddress = "";
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
        try {
            Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
            mServiceField.setAccessible(true);

            Object btManagerService = mServiceField.get(bluetoothAdapter);

            if (btManagerService != null) {
                bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
            }
        } catch (NoSuchFieldException e) {

        } catch (NoSuchMethodException e) {

        } catch (IllegalAccessException e) {

        } catch (InvocationTargetException e) {

        }
    } else {
        bluetoothMacAddress = bluetoothAdapter.getAddress();
    }
    return bluetoothMacAddress;
}
票数 4
EN
查看全部 9 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33377982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档