Android RFComm 与 OBEX 推送无法正常工作
我正在尝试将 java 应用程序重新制作为 android 应用程序,但我无法使其工作。该应用程序旨在与使用 OBEX Push 的设备进行通信。该设备无法接受任何传入连接,并且没有用户界面,但有一些 LED。
我试图重制的java代码片段如下:
LocalDevice local;
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.LIAC);
String sConnectionURL = "btspp://localhost:" + myUUID + ";name=" + obexName;
this.server = (StreamConnectionNotifier) Connector.open(sConnectionURL);
我不是java专家,但据我所知,此片段应该使用名称obexName注册SPP服务,并开始通过UUID myUUID侦听传入连接。这按预期工作。
当设备与运行 java midlet 的手机配对时,它将设置一个位以发送到手机上带有 UUID 的 SPP,或者根本不发送。如果在配对过程中找不到任何具有 UUID 的 SPP,它将尝试使用普通 OBEX 连接到手机。
这是我无法在 Android 手机上使用的技术,无论是 HTC Hero 还是 HTC Desire,两者的版本都是 2.1-update1。无论我如何尝试,手机都只能连接到手机,而不能连接到所需的应用程序。
我创建了一个与developer.android.com 上的示例非常相似的类:
private class AcceptThread extends Thread
{
private final BluetoothServerSocket __serverSocket;
public AcceptThread()
{
BluetoothServerSocket tmpSocket = null;
trace("Creating AcceptThread");
try
{
trace("Starting to listen");
tmpSocket = _bluetoothAdapter.listenUsingRfcommWithServiceRecord(obexName, myUUID);
trace("Listening successful");
}
catch (Exception e)
{
trace("Listening NOT successful");
// TODO: handle exception
}
__serverSocket = tmpSocket;
trace("AcceptThread created");
}
public void run()
{
BluetoothSocket socket = null;
trace("AcceptThread started");
while(true)
{
try
{
trace("Waiting for socket acceptance");
socket = __serverSocket.accept();
trace( "Socket accepted");
}
catch (Exception e)
{
trace("Error when accepting socket: " + e.getLocalizedMessage());
break;
// TODO: handle exception
}
if (socket != null)
{
synchronized (BTTransfer.this)
{
trace("Socket exists");
try
{
__serverSocket.close();
trace("Socket successfully closed");
}
catch (Exception e) {}
break;
}
}
trace("Socket does not exist");
}
}
public void cancel()
{
try
{
__serverSocket.close();
}
catch (Exception e) {}
trace("AcceptThread cancelled and Socket successfully closed");
}
}
关于代码的评论:
跟踪函数是一个同步函数,将文本提供给 Handler 对象,提供 UI 信息。
应用程序故意不执行任何操作,只是在成功连接后关闭连接。
应用程序到达“等待套接字接受”,但此后再也没有跟踪。
我有一个 PC .NET 应用程序,可以将自己伪装成设备,并且通过使用正确的 UUID,它可以完美地工作,但是 PC 已经与手机配对,并且没有说明它应该通过正常的 OBEX 发送(如果它是)找不到指定的。
我已经为此工作好几天了,但无法想出解决方案。那里有人有什么想法吗?
预先感谢,
/Trygg
对于第二条消息,我的意思是:
当设备联系手机时,下拉菜单中会出现一条通知,告知设备正在联系。几秒钟后(取决于文件大小)文件被传输,并且有第二个通知告知文件已传输。这是第二个“消息”。
由于我侦听与设备的断开连接,并且客户通过我的程序已经知道文件已传输,因此第二个通知完全没有用。尽管如此,每次设备向手机发送文件时它都会出现。
不过,在我们对 Legend 和 Hero 的测试中,我们从未遇到过第二个通知。这就是这些手机失败的地方。第一个通知出现,然后什么也没有,几秒钟后设备返回错误。
希望这有助于澄清我的意思。
/Trygg
我没有让这个工作,但做了某种解决方法。我在事件BluetoothDevice.ACTION_ACL_DISCONNECTED 上注册了一个BroadcastReceiver,然后检查哪个设备已断开连接。如果是“我的”,我会在蓝牙收件箱中搜索文件。
我收到设备制造商发来的消息,说它还无法工作,但他们正在开发新固件。这就是为什么我没有在这里积极行动,也没有寻求更好的解决方案。
I am trying to remake a java application into an android application, but I cannot make it work. The application is meant to talk to a device that uses OBEX Push. The device cannot take any incoming connections and it has no user interface but some LEDs.
The java code snippet I am trying to remake is following:
LocalDevice local;
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.LIAC);
String sConnectionURL = "btspp://localhost:" + myUUID + ";name=" + obexName;
this.server = (StreamConnectionNotifier) Connector.open(sConnectionURL);
I am no java expert, but as far as I know, this snippet should register an SPP service with the name obexName and start to listen for incoming connections via the UUID myUUID. This works as intended.
When the device is pairing to the phone running the java midlet, it will set a bit to send to an SPP with the UUID on the phone or not send at all. If it cannot find any SPP with the UUID during the pairing, it will try to connect to the phone using normal OBEX instead.
This is the technique I cannot make work on an android phone, neither an HTC Hero, nor an HTC Desire, both with version 2.1-update1. No matter how I try, the phone is only connecting to the phone, and not to the application as desired.
I created a class much like the example on developer.android.com:
private class AcceptThread extends Thread
{
private final BluetoothServerSocket __serverSocket;
public AcceptThread()
{
BluetoothServerSocket tmpSocket = null;
trace("Creating AcceptThread");
try
{
trace("Starting to listen");
tmpSocket = _bluetoothAdapter.listenUsingRfcommWithServiceRecord(obexName, myUUID);
trace("Listening successful");
}
catch (Exception e)
{
trace("Listening NOT successful");
// TODO: handle exception
}
__serverSocket = tmpSocket;
trace("AcceptThread created");
}
public void run()
{
BluetoothSocket socket = null;
trace("AcceptThread started");
while(true)
{
try
{
trace("Waiting for socket acceptance");
socket = __serverSocket.accept();
trace( "Socket accepted");
}
catch (Exception e)
{
trace("Error when accepting socket: " + e.getLocalizedMessage());
break;
// TODO: handle exception
}
if (socket != null)
{
synchronized (BTTransfer.this)
{
trace("Socket exists");
try
{
__serverSocket.close();
trace("Socket successfully closed");
}
catch (Exception e) {}
break;
}
}
trace("Socket does not exist");
}
}
public void cancel()
{
try
{
__serverSocket.close();
}
catch (Exception e) {}
trace("AcceptThread cancelled and Socket successfully closed");
}
}
Comments about the code:
The trace function is a synchronized function feeding text to the Handler object, giving the UI information.
The application is knowingly not doing anything but closing the connection after a successful connection.
The application reaches "Waiting for socket acceptance" but never the trace after that.
I have a PC .NET application that can disguise itself as the device, and by using correct UUID it works flawlessly, but then the PC is already paired to the phone, and do not have the bit saying it should send over normal OBEX if it cannot find the specified one.
I have been working with this for several days, and can not come up with a solution. Does anyone out there have any ideas?
Thanks in advance,
/Trygg
With the second message I mean following:
When the device contacts the phone, there is a notification in the drop down menu telling a device is contacting. A few seconds later (depending on the file size) the file is transferred, and there is a second notification telling the file is transmitted. This is the second "message".
Since I listen for the disconnection from the device, and the customer then through my program already knows that the file is transmitted, this second notification is utterly useless. Still, it will come up every time the device sends files to the phone.
In our tests on Legend and Hero, we never come to the second notification, though. This is where those phones fail. The first notification comes, then nothing, and the device returns an error after a few seconds.
Hope this helps to clarify what I meant.
/Trygg
I did not get this working, but made sort of a workaround. I registerad a BroadcastReceiver on the event BluetoothDevice.ACTION_ACL_DISCONNECTED and then checked for what device was disconnected. If it was "mine" I searched for the files in the bluetooth inbox.
I got a message from the manufacturer of the device saying that it will not work yet, but they are working on a new firmware. This is why I haven't been active here nor worked for a better solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了确定,您是否在 Android 清单中设置了正确的权限?
Just to be sure, did you set the right permissions in the Android manifest?