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

Dani AI

Generated

Quick practical diagnosis for : two problems explain the connection failures — the StreamConnection used to query the remote device is never the one opened by the DeviceConnection instance, and the code mixes “server” and “client” roles (class named SampleSPPServer but calling Connector.open on a remote address and a hard-coded channel). RemoteDevice.getRemoteDevice(...) requires a live StreamConnection returned by Connector.open (or acceptAndOpen), so calling it with a null/unused connection will fail. (docs.oracle.com)

Fix the immediate null/lookup bug by using the StreamConnection that DeviceConnection actually opens, or change DeviceConnection.connect to return the StreamConnection instead of a boolean. Example (safe, short change):

if (devcon.connect()) {
    con = devcon.getConnection();              // get the real StreamConnection
    RemoteDevice dev = RemoteDevice.getRemoteDevice(con);
    String addr = dev.getBluetoothAddress();
    String name = dev.getFriendlyName(true);
    // open streams from 'con' and use them
}

RemoteDevice.getRemoteDevice must be passed the connection returned by Connector.open / acceptAndOpen. (docs.oracle.com)

Clarify roles and service discovery: if the MIDlet should be a server, use a StreamConnectionNotifier and acceptAndOpen(); if it should be a client, do not hard-code RFCOMM channels — discover the service or call DiscoveryAgent.selectService / searchServices and use ServiceRecord.getConnectionURL(), then call Connector.open on that URL. The server pattern uses a btspp://localhost:... URL and blocks on acceptAndOpen(); the client pattern uses the connection URL returned by SDP. (docs.oracle.com)

Operational checks: ensure the PC is advertising an SPP/RFCOMM service (or has a mapped COM port), the devices are paired, and the MIDlet has Bluetooth permissions in the JAD/manifest (request client and/or server permissions such as javax.microedition.io.Connector.bluetooth.client and javax.microedition.io.Connector.bluetooth.server). Connector.open will fail with security errors if permissions are missing. Also capture and inspect the exact stack trace from Connector.open — authentication, SDP, or IO errors give different root causes. (oracle.com)

Summary actionable checklist (apply in this order): confirm role (client vs server); make DeviceConnection return or expose the StreamConnection and use that reference; stop using hard-coded channel numbers — use SDP/selectService; add required MIDlet permissions; pair devices and verify SPP on the PC. The combination of these fixes resolves the common causes of the symptoms shown in the posted code.

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.