MeandJava 0 Light Poster

Hello everybody,

I'm trying to make a connection between my mobile and my pc.
Im using J2me midp 2.0 to create my code.
The jar of my mobile wants to make a connection to the bluetoothaddress of my pc.
My mobilecode is based on a MIDlet.
But everytime the connection fails. Could someone give me some tips and look to my code.

package bt;

import hello.AlertMsg;
import java.io.IOException;
import javax.bluetooth.RemoteDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

/**
 *
 * 
 */
public class DeviceConnection {

    private StreamConnection con = null;
    private String devAddress = null;
    private int port = 0;

    public DeviceConnection(String devAddress, int port) {
        this.devAddress = devAddress;
        this.port = port;
        
    }

    public boolean connect() {
        String connectionURL = "btspp://" + devAddress + ":" + port + ";authenticate=false;encrypt=false;master=false;";
        try {
            con = (StreamConnection) Connector.open(connectionURL);
            
            return true;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean disconnect() {
        try {
            if (con != null) {
                con.close();
            }
            return true;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean isConnected() {
        if (con != null) {
            return true;
        }
        return false;
    }

    public StreamConnection getConnection() {
        return con;
    }
}
package bt;

/**
 *
 * 
 */

import hello.AlertMsg;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
 
import javax.bluetooth.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.midlet.MIDlet;
 
/**
*
*/
public class SampleSPPServer implements Runnable {
   
    byte b[];
    Thread t;
    AlertMsg amsg;
    MIDlet midlet;
    String connectionURL = "001112070632";     //     0007804C4730
    private StreamConnection con = null;
    private DeviceConnection devcon;
    private StringBuffer message;
    
    public SampleSPPServer(MIDlet midlet_){
        
        midlet = midlet_;
        amsg = new AlertMsg(midlet);
        t = new Thread(this, "BTServer");
        t.start();
        devcon = new DeviceConnection(connectionURL, 1);
    }
    
    private void startServer() throws IOException{
        
        if(devcon.connect()){
            message = new StringBuffer();
        
            RemoteDevice dev = RemoteDevice.getRemoteDevice(con);
                    
            message.append("Remote device address: "+dev.getBluetoothAddress() + "\n");
            message.append("Remote device name: "+dev.getFriendlyName(true) + "\n");
        
            amsg.setAlert("Server", message);
            
        } else{
            message = new StringBuffer();
            message.append("Not connected!");
            
            amsg.setAlert("Server", message);
        }
        
        while(true){
            
        }
        
        
    }
    
    public void run() {
        
        try {
            
            //display local device address and name
            LocalDevice localDevice = LocalDevice.getLocalDevice();
            System.out.println("Address: "+localDevice.getBluetoothAddress());
            System.out.println("Name: "+localDevice.getFriendlyName());
            try {
                startServer();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            
        } catch (BluetoothStateException ex) {
            ex.printStackTrace();
        }
    }
}

Thanks in advance

Greetz

Meandjava

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.