如何在Android中创建不安全RFCOMM插座?插座、不安全、如何在、Android

2023-09-12 22:46:24 作者:捏碎你的虛偽

我在看RFCOMM插座连接了不安全的方式。我能在下面找到

I am looking at way of connecting over rfcomm socket insecurely. I was able to find the way mentioned below

Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);

这暂时是做什么我想要的。即使在here说,我们需要使用createInsecureRfcommSocketToServiceRecord不安全的连接。但目前还没有这样的方法。我发现的唯一方法是使用反射,如上图所示。而且即使在传递中createInsecureRfcommSocket和不createInsecureRfcommSocketToServiceRecord的方法。我只是想知道如何可靠的就是这种方式。如果我提到createInsecureRfcommSocketToServiceRecord的方法连接从未发生过。

This for the time being is doing what I want. Even the documentation over here says that we need to use createInsecureRfcommSocketToServiceRecord for insecure connections. But there is no such method. The only way I found out was using reflection as shown above. And even in that the method that is passed in createInsecureRfcommSocket and not createInsecureRfcommSocketToServiceRecord. I just wanted to know how reliable is this way. If I mention createInsecureRfcommSocketToServiceRecord in method the connection never happens.

推荐答案

createInsecureRfcommSocketToServiceRecord()被列入开始与Android API级别10,这样的文档会鼓励你使用它,因为该文档始终遵循API的最新版本。如果你的目标的API低于10(又名2.3.3或姜饼),则该方法不公开访问到你。

createInsecureRfcommSocketToServiceRecord() was included starting with Android API Level 10, so the documentation will encourage you to use it since the docs always follow the latest version of the API. If you are targeting an API lower than 10 (a.k.a. 2.3.3 or Gingerbread), then that method is not publicly accessible to you.

您是通过反射调用方法 createInsecureRfcommSocket()里面 BluetoothDevice类私有方法,它已经$ P因为大约Android 2.0的$ psent。与调用隐藏方法的问题是,他们不能保证在那里上的所有设备,还是将来......所以你赌了一下。我的猜测是你的方法可能会工作的大部分时间在大多数2.0+设备,因为实施公共表弟所需要的服务 createRfcommSocketToServiceRecord()是在堆栈层如此相似

The method you are calling via reflection createInsecureRfcommSocket() is a private method inside BluetoothDevice that has been present since roughly Android 2.0. The problem with calling hidden methods is that they aren't guaranteed to be there on all devices, or in the future...so you're gambling a bit. My guess is your method will probably work most of the time on most 2.0+ devices, since the services required to implement its public cousin createRfcommSocketToServiceRecord() are so similar at the stack layer.

底线,如果你想与你的蓝牙实现保证通用的兼容性,你必须针对2.3.3(API等级10),与应用程序。随着公共API,现在暴露不安全RFCOMM,很难说是否是或多或少可能为潜在的私有实现改变。

Bottom line, if you want guaranteed universal compatibility with your Bluetooth implementation, you'll have to target 2.3.3 (API Level 10) with your application. With a public API now exposed for insecure RFCOMM, it's hard to say whether it's more or less likely for the underlying private implementation to change.