Search code examples
androidjava-mebluetooth

Android <-> J2ME Bluetooth


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?


Solution

  • 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).