本文介绍了通过 OBEX Object PushProfile 通过蓝牙接收文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用 OBEX 对象推送配置文件 (OPP) 通过蓝牙发送数据的设备.

I have device which sends data via Bluetooth using OBEX Object Push Profile (OPP).

使用 adb logcat 我看到我的 android 设备收到一个连接(但中止了这个连接?)

Using adb logcat i see that my android device receives a connection (but abort this connection?)

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

当我安装蓝牙文件传输(市场上的免费应用程序)时,我就能够接收文件.但我不想安装其他应用程序.

When I install Bluetooth File Transfer (Free application from market) then i'm able to receive files.But I wouldn't like to install other application.

推荐答案

我相信我有(至少是部分)解决方案,然后应该允许通过 OPP 拦截文件并添加自定义代码.第一步是转到设置> 应用程序> 运行> 蓝牙共享并杀死BluetoothOppService

I believe I have (at least a partial) solution which should then allow files to be intercepted via OPP and custom code added. The first step is to go to settings > apps > running > Bluetooth Share and kill the BluetoothOppService

然后我使用反射来访问 BluetoothAdapter(下面的代码)上的一个方法,该方法允许侦听特定端口.之后我们可以拦截传入的 OPP 通信并与输入和输出流交互. SO 线程将有助于 OPP 通信部分,但作为初始步骤,我读取数据流并回复 OPPOK"消息,即 os.writeByte(ObexSession.OBEX_SUCCESS | ObexSession.OBEX_FINAL_BIT);

Then I used reflection to access a method on BluetoothAdapter (code below) which allows listening on a specific port. After which we can intercept the incoming OPP communication and interact with the input and output streams. This SO thread will help with the OPP communication part, but as an initial step I read the data stream and reponded with an OPP 'OK' message ie 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();

After which, use the following from a separate Thread (to prevent blocking) and remote devices can connect via socket = serverSocket.accept();

这篇关于通过 OBEX Object PushProfile 通过蓝牙接收文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 08:31