我有设备,通过蓝牙发送数据使用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当我安装蓝牙文件传输(市场上的免费应用程序),然后我能够接收文件。但是我不想安装其他的应用程序。
发布于 2013-01-03 16:15:02
我相信我有(至少是部分的)解决方案,它应该允许通过OPP和添加自定义代码来拦截文件。第一步是进入设置>应用程序>运行>蓝牙共享和kill BluetoothOppService
然后,我使用反射来访问BluetoothAdapter上的一个方法(下面的代码),该方法允许侦听特定的端口。之后,我们可以拦截传入的OPP通信,并与输入和输出流进行交互。This所以线程将帮助处理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();进行连接
https://stackoverflow.com/questions/12071268
复制相似问题