Android< - > J2ME蓝牙

时间:2010-12-16 13:50:14

标签: android java-me bluetooth

我想将支持蓝牙(jsr082)的通用midp2 j2me设备连接到Android设备,运行我正在编写的类似自定义应用程序。 Android设备似乎只能“看到”附近的其他Android蓝牙设备?

这甚至可能吗?

2 个答案:

答案 0 :(得分:0)

是的,有可能,您需要使用反射来触及几个方法(不是documented)以获得与j2me相同的行为。

createRfcommSocket(int channel)中的

android.bluetooth.BluetoothDevice

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)});

// 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);

可以找到有关使用情况的一些信息here

这些方法在早期版本的android中可用(我认为从2.0开始),但它们是私有的,因此需要反射(可能它们未经测试,因此被隐藏)。

答案 1 :(得分:0)

我最近不得不这样做。我使用api 8只有

    BluetoothAdapter.listenUsingRfcommWithServiceRecord(String,UUID);

方法正式可用。

由于Android不允许永久可见性,因此我使用了应用程序保存的设备列表。

这是我的代码的简化,它将Android设置为具有侦听套接字的服务器,并将j2me手机(我使用诺基亚N95)作为客户端来创建RFCOMM连接。

Android内容:

    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();

这是J2ME的东西:

    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;
        }
    } 

这让我的Android设备接受了从j2me发送的消息。我从我的代码中复制了重要的部分,所以我不确定它是否全部有效,因为我没有尝试内联执行它,因为我在这里提出它但你应该能够填写我在复制粘贴时犯的任何错误。 / p>

祝你好运!

相关问题