I am trying to remake a java application into an Android application, but I cannot get it to work. The application is designed to communicate with a device using OBEX Push. The device cannot accept incoming connections, and it does not have a user interface, except for some LEDs.
The code snippet I'm trying to redo is as follows:
LocalDevice local;
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.LIAC);
String sConnectionURL = "btspp://localhost:" + myUUID + ";name=" + obexName;
this.server = (StreamConnectionNotifier) Connector.open(sConnectionURL);
I'm not a java expert, but as far as I know, this snippet should register an SPP service named obexName and start listening for incoming connections through the UUID myUUID. It works as intended.
When the device connects to a phone running java midlet, it will set the bit to send to SPP with the UUID on the phone or not send at all. If he cannot find the SPP with the UUID during pairing, he will try to connect to the phone using a regular OBEX.
This is a technique that I canβt work with on an Android phone, neither on HTC Hero nor on HTC Desire, as with version 2.1-update1. No matter how I try, the phone only connects to the phone, and not to the application as desired.
I created a class similar to the developer.android.com example:
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");
}
__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;
}
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, .
, .
" ", .
.NET.NET, , , UUID, , , , OBEX, .
. - ?
,
/Trygg
:
, . ( ) , . "".
, , , , . , , .
Legend Hero . - , . , , .
, , .
/Trygg
, . BroadcastReceiver BluetoothDevice.ACTION_ACL_DISCONNECTED, , . "", bluetooth.
, , , . .