android - 如何获取我的设备蓝牙 ID。我尝试获取但它们总是返回 02:00:00:00:00:00
问题描述
您好,我尝试获取我的蓝牙设备 ID,但这不起作用。我尝试下面的代码,所以你能检查一下我犯了哪个错误。
public static String getBluetoothMacAddress(Context context) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String bluetoothMacAddress = "0";
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);
Utils.printLog("bluetoothMacAddress ", bluetoothMacAddress);
}
bluetoothMacAddress= android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
} catch (Exception e) {
Utils.printLog("bluetoothMacAddress1 ", e.getMessage());
}
if (bluetoothMacAddress == null || bluetoothMacAddress.equals("") || bluetoothMacAddress.equals("02:00:00:00:00:00")) {
bluetoothMacAddress = "0";
}
Utils.printLog("bluetoothMacAddress ", bluetoothMacAddress);
return bluetoothMacAddress;
}
解决方案
您的 mac 地址实际上是一种 id,但在网络层,因此网络可以识别您的设备。您应该始终为特定设备获取相同的 mac 地址,尽管在制造过程中存在一些例外情况,即多个设备可能具有相同的 mac(如 Arduino 控制器)。但是,这是EXCEPTIONAL,而不是一般
推荐阅读
- android - 在模拟 OpenGL ES 2.0 支持的 AVD 上更快的动画
- python-3.x - 创建一个文件,文件名是列表中的一个项目
- python - 由 Apple 的 Retina 显示屏引起的 Kivy 像素测量问题
- html - 设置 translate3d 独立于其他变换
- time - 存储任意计划数据的好方法是什么?
- git - 视觉工作室设置项目重建错误
- javascript - 使用 Firefox (Ubuntu) 拒绝 CORS 请求
- java - 在 android studios 中获得通货膨胀异常
- javascript - 自己的图像托管页面:如何始终打开一个 html 文件,但在 url 中给出了图像?
- caching - 番石榴缓存
.put(key, value) 不向我的缓存添加值;put() 方法不起作用