我的设备使用 OBEX 对象推送配置文件 (OPP) 通过蓝牙发送数据。
使用 adb logcat 我看到我的 Android 设备收到一个连接(但中止此连接?)
08-22 11:14:37.939: I/BtOppRfcommListener(22586): Accepted connectoin from 00:07:CF:5F:52:A0
08-22 11:14:37.939: I/BtOpp Service(22586): Start Obex Server
08-22 11:14:38.109: D/Obex ServerSession(22586): java.io.IOException: Software caused connection abort
08-22 11:14:38.109: D/PowerManagerService(180): @PowerManagement: 'BtOppObexServer' releaseWakeLock when screen locked
08-22 11:14:39.219: D/BluetoothEventLoop(180): Device property changed: 00:07:CF:5F:52:A0 property: Connected value: false
当我安装蓝牙文件传输(市场上的免费应用程序)时,我就可以接收文件。
但我不想安装其他应用程序。
我相信我有(至少是部分)解决方案,该解决方案应该允许通过 OPP 拦截文件并添加自定义代码。第一步是转到设置 > 应用程序 > 运行 > 蓝牙共享,然后杀死BluetoothOppService
然后我使用反射来访问方法BluetoothAdapter
(下面的代码)允许监听特定端口。之后我们可以拦截传入的 OPP 通信并与输入和输出流进行交互。ThisSO 线程将帮助 OPP 通信部分,但作为初始步骤,我读取数据流并回复 OPP“OK”消息,即os.writeByte(ObexSession.OBEX_SUCCESS | ObexSession.OBEX_FINAL_BIT);
// simplified exception handling
public class BluetoothAdapterProxy
{
public static final int CHANNEL_OPP = 12;
final BluetoothAdapter target;
static final Class<?> targetClass = BluetoothAdapter.class;
Method listenOn;
public BluetoothAdapterProxy(BluetoothAdapter target)
{
this.target = target;
Class<?>[] args = new Class[] { int.class };
try
{
this.listenOn = targetClass.getDeclaredMethod(
"listenUsingRfcommOn", args);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
}
public BluetoothServerSocket listenUsingRfcommOn(int channel)
{
try
{
return (BluetoothServerSocket) (listenOn.invoke(target,
new Object[] { channel }));
}
catch (Exception e)
{
// complain loud, complain long
throw new RuntimeException(ex);
}
}
}
用法:初始化使用
serverSocket = new BluetoothAdapterProxy(BluetoothAdapter.getDefaultAdapter())
.listenUsingRfcommOn(BluetoothAdapterProxy.CHANNEL_OPP);
之后,使用来自单独的以下内容Thread
(以防止阻塞)和远程设备可以通过socket = serverSocket.accept();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)