1

I am trying to do communication between a mobile application (using J2ME and JSR82) and Desktop application (in C# using InTheHand Library).

I am using RFComm protocol with UUID: 00000003-0000-1000-8000-00805f9b34fb.

I have manually specified uuid on both devices. I have mobile application to wait for incoming connections, while desktop application sends data to it.

But, my mobile application just does not listen to incoming connection. It just hangs at the message: "Waiting for incoming connection..."

J2ME code in Mobile Application:

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# Code in Desktop App:

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

Please help me out on this.

Shubham
  • 21,300
  • 18
  • 66
  • 89

0 Answers0