请教使用反射获取android本机蓝牙地址的问题

morninggood 2019-05-06 02:26:14
下面这段代码使用反射方式获取蓝牙地址,哪位高手能帮助翻译成delphi的吗?

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;
}
...全文
316 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
morninggood 2019-05-06
  • 打赏
  • 举报
回复
引用 1 楼 早打大打打核战争 的回复:
use System.Bluetooth; var mac: string; //... with TBluetoothAdapter.Create(nil) do mac := Address;
谢谢你,这个获得的地址不准,我翻译到了Object obj = method.invoke(bluetoothManagerService);这句出了问题,JMethod没有invoke方法
  • 打赏
  • 举报
回复
use System.Bluetooth;

var
mac: string;
//...
with TBluetoothAdapter.Create(nil) do mac := Address;

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧