设备不接受J2Me蓝牙中的传入连接?

时间:2012-03-22 06:50:39

标签: java-me bluetooth jsr82

我正在尝试在移动应用程序(使用J2ME和JSR82)和桌面应用程序(使用InTheHand库的C#中)之间进行通信。

我正在使用带有UUID的RFComm协议:00000003-0000-1000-8000-00805f9b34fb。

我在两台设备上手动指定了uuid。我有移动应用程序等待传入连接,而桌面应用程序则向其发送数据。

但是,我的移动应用程序只是不听取传入的连接。它只是挂在消息:“等待传入连接...”

移动应用程序中的J2ME代码:

public void startApp() {
    if (midletPaused) {
        resumeMIDlet();
    } else {
        initialize();
        startMIDlet();

        form.append("UID: "+ uuid.toString() +"\n");
        //set the device discoverable
        try {
            LocalDevice localDevice = LocalDevice.getLocalDevice();
            localDevice.setDiscoverable(DiscoveryAgent.GIAC);
            form.append("Device Address: "+localDevice.getBluetoothAddress()+"\n");
            form.append("Name: "+ localDevice.getFriendlyName()+"\n");
        }
        catch (BluetoothStateException exception) {
            form.append(exception.toString()+"\n");
        }

        //setup a server socket
        StreamConnectionNotifier streamConnectionNotifier = null;
        try {
            String url = "btspp://localhost:000300001000800000805f9b34fb;name=rfcommtest;authorize=true";
            //form.append(url);
            streamConnectionNotifier = (StreamConnectionNotifier)Connector.open(url);
            if (streamConnectionNotifier == null) {
                form.append("Error: streamConnectionNotifier is null\n");
                return;
            }
        }
        catch (Exception exception) {
            form.append(exception.toString()+"\n");
        }

        //wait for an incoming connection
        StreamConnection streamConnection = null;
        try {
            form.append("Waiting for incoming connection...\n");
            streamConnection = streamConnectionNotifier.acceptAndOpen();
            if (streamConnection == null) {
                form.append("Error: streamConnection is null\n");
            } else {
                form.append("Connection received.\n");
            }
        }
        catch (Exception exception) {
            form.append(exception.toString()+"\n");
        }

        //write hello and then exit
        try {
            OutputStream out = streamConnection.openOutputStream();
            form.append("Stream \n");
            String s = "hello";
            out.write(s.getBytes());
            out.flush();
            streamConnection.close();
            form.append("Text Written to stream\n");
        }
        catch (Exception exception) {
            form.append(exception.toString()+"\n");
        }


    }
    midletPaused = false;
}

桌面应用程序中的C#代码:

        cli = new BluetoothClient();

        BluetoothEndPoint ep1 = new BluetoothEndPoint(info[listBox1.SelectedIndex].DeviceAddress, BluetoothService.RFCommProtocol);

        cli.Connect(ep1);

        Stream stream = cli.GetStream();

        StreamWriter sw = new StreamWriter(stream);

        sw.WriteLine("Tesing");
        sw.WriteLine("testing");
        sw.Flush();
        sw.Close();

        stream.Close();

请帮我解决这个问题。

0 个答案:

没有答案
相关问题