Reputation: 89
I want to connect a generic midp2 j2me device supporting bluetooth (jsr082) to an android device, running a similar custom application I am writing. The Android device only appears to "see" other Android bluetooth devices nearby?
Is this even possible?
Upvotes: 1
Views: 1494
Reputation: 16
Yes it's possible, you'll need to use reflection to hit a couple of methods (that aren't documented) to get the same behaviour as j2me.
createRfcommSocket(int channel)
in android.bluetooth.BluetoothDevice
like
Class cls = Class.forName("android.bluetooth.BluetoothDevice");
java.lang.reflect.Method meth = cls.getMethod("createRfcommSocket", new Class[] { Integer.TYPE});
BluetoothSocket iBluetoothSocket = (BluetoothSocket) meth.invoke(theBTDevice,new Object[] { new Integer(channel)});
and
// getUuids() also in android.bluetooth.BluetoothDevice
// i.e.
Class cls = Class.forName("android.bluetooth.BluetoothDevice");
java.lang.reflect.Method meth = cls.getMethod("getUuids", new Class[0]);
ParcelUuid[] uuidlist = (ParcelUuid[]) meth.invoke(theBTDevice);
a bit of info on the usage can be found here:
The methods are available in earlier versions of android (I think from 2.0) but are private therefore reflection is needed (maybe they were untested and therefore hidden).
Upvotes: 0
Reputation: 618
I recently had to do this. I used api 8 so only the
BluetoothAdapter.listenUsingRfcommWithServiceRecord(String,UUID);
method is officially available.
Since Android does not allow permanent visibility I worked with the list of devices my application saved.
Here's a simplification of my code that sets Android as the server with the listening socket and the j2me phone (I used a Nokia N95) as the client to create an RFCOMM connection.
Here the Android stuff:
int bytes = 0, offset = 0;
byte[] buffer;
BluetoothServerSocket btServerSocket = btAdapter.listenUsingRfcommWithServiceRecord("serv",myUUID);
BluetoothSocket btSocket = btServerSocket.accept();
OutputStream out = btSocket.getOutputStream();
InputStream in = btSocket.getInputStream();
bytes = in.read();
buffer = new byte[bytes];
out.write(0);
out.flush();
while (true)
{
bytes = in.read(buffer, offset, buffer.length - offset);
offset += bytes;
if (bytes == -1 || offset >= buffer.length)
{
Log.d("MyBT", "message: " + new String(buffer));
break;
}
}
out.close();
in.close();
btSocket.close();
btServerSocket.close();
and here's the J2ME stuff:
class ARemoteDevice extends javax.bluetooth.RemoteDevice
{
public ARemoteDevice(String btaddr)
{
super(btaddr);
}
}
RemoteDevice targetDevice = new ARemoteDevice("00FF00FF00FF");
String message = "Hello my dear android device";
UUID[] uuidSet = {new UUID("yourUUIDHERE",false)};
Vector URLS = new Vector();
int ENCRYPTMODE = ServiceRecord.AUTHENTICATE_ENCRYPT;
final Object serviceSearchCompletedEvent = new Object();
DiscoveryListener listener = new DiscoveryListener()
{
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {}
public void inquiryCompleted(int discType) {}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
{
for (int i = 0; i < servRecord.length; i++)
{
String connectionURL = servRecord[i].getConnectionURL(ENCRYPTMODE, false);
if (connectionURL != null)
{
URLS.addElement(connectionURL);
}
}
}
public void serviceSearchCompleted(int transID, int respCode) {
synchronized (serviceSearchCompletedEvent) {
serviceSearchCompletedEvent.notifyAll();
}
}
};
synchronized (serviceSearchCompletedEvent) {
discAgent.searchServices(null, uuidSet, targetDevice, listener);
serviceSearchCompletedEvent.wait();
}
if (URLS.isEmpty())
{
System.out.println("no connectionurl found");
return;
}
try {
for (int i = 0; i < URLS.size(); ++i) {
System.out.println("Trying: " + (String) URLS.elementAt(i));
try {
sc = (StreamConnection) Connector.open((String) URLS.elementAt(i));
in = sc.openInputStream();
out = sc.openOutputStream();
out.write(length);
out.flush();
in.read();
try {
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
break;
} catch (Exception e) {
sc = null;
System.out.println("Failed BT COnn");
}
} // TODO: handle case if all URLS fail
int bytesSent = 0;
int bytesToSend = 0;
int length=message.length();
int MTU=256;//or whatever..?
while (true) {
if ((length - bytesSent) > MTU) {
bytesToSend = MTU;
} else {
bytesToSend = length - bytesSent;
}
out.write(message.getBytes(), bytesSent, bytesToSend);
out.flush();
bytesSent += bytesToSend;
if (bytesSent >= length) {
Thread.sleep(200);
break;
}
}
if (in != null) {
in.close();
in = null;
}
if (out != null) {
out.close();
out = null;
}
if (sc != null) {
sc.close();
sc = null;
}
}
this got my android device to accept the messages sent from j2me. I copied the important parts from my code so I'm not sure if it all works because I haven't tried executing it inline as I present it here but you should be able to fill in any mistakes I made when copy pasting this.
Good Luck!
Upvotes: 1