gpt4 book ai didi

android - 通过 OBEX 对象 PushProfile 通过蓝牙接收文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:47 25 4
gpt4 key购买 nike

我的设备使用 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 通信并与输入和输出流进行交互。 This SO 线程将有助于 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();

连接

关于android - 通过 OBEX 对象 PushProfile 通过蓝牙接收文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12071268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com