16,748
社区成员
发帖
与我相关
我的任务
分享
public static String getBtAddressByReflection() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Field field = null;
try {
field = BluetoothAdapter.class.getDeclaredField("mService");
field.setAccessible(true);
Object bluetoothManagerService = field.get(bluetoothAdapter);
if (bluetoothManagerService == null) {
return null;
}
Method method = bluetoothManagerService.getClass().getMethod("getAddress");
if(method != null) {
Object obj = method.invoke(bluetoothManagerService);
if(obj != null) {
return obj.toString();
}
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}