Question

We are trying to send an object from an android Bluetooth client to a desktop Bluetooth server. We are trying to do this through the ObjectInputStream and ObjectOutput Stream. We are also very new to Bluetooth development, and have tried looking online for different examples and solutions.

Here is our code,

Android Code:

    private static 1.5.0/docs/api/java/util/UUID.html">UUID generalUuid = 1.5.0/docs/api/java/util/UUID.html">UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private static BluetoothSocket socket;


    private static BluetoothSocket getBluetoothSocket(){

        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.cancelDiscovery();
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                if(device.getName().equalsIgnoreCase(("mint-0"))){
                    try {
                        return device.createRfcommSocketToServiceRecord(generalUuid);
                    } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
                        return null;
                    }
                }
            }
        }
        return null;
    }

    public static boolean sendData(1.5.0/docs/api/java/lang/String.html">String first, 1.5.0/docs/api/java/lang/String.html">String last, int age){

            socket = getBluetoothSocket();
            try {
                socket.connect();
            } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
                // TODO Auto-generated catch block
                socket = null;
            }

        if(socket != null){
            try {       
                    1.5.0/docs/api/java/io/ObjectOutputStream.html">ObjectOutputStream oos = new 1.5.0/docs/api/java/io/ObjectOutputStream.html">ObjectOutputStream(socket.getOutputStream());
                    oos.writeChars("X");
                    //send serializable object
                    personTest person = new personTest(first, last, age);
                    oos.writeObject(person);
                    oos.flush();
                    oos.close();
                socket.close();
                return true;
            } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
                socket = null;
                return false;
            }
        }else{
            return false;
        }
    }

PC Code:

public class DataServer {

static final String serverUUID = "11111111111111111111111111111123";

public static void main(String[] args) throws IOException {

    LocalDevice localDevice = LocalDevice.getLocalDevice();

    localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service
    System.out.println("Setting Discoverable");

    String url = "btspp://localhost:" + serverUUID + ";name=BlueToothServer";
    StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url);
    System.out.println("Server: " + url);

    StreamConnection connection = server.acceptAndOpen(); // Wait until client connects
    System.out.println("Client Connected");

    //=== At this point, two devices should be connected ===//
    ObjectInputStream ois = new ObjectInputStream(connection.openInputStream());

    personTest obj = null; 

    System.out.println("Waiting for Object");
    //while(true){
        try{
            obj = (personTest) ois.readObject();

            if(obj.getName() != "Gregg Miller"){
                System.err.println("Name " + obj.getName() + " incorrect");
            }
        } catch(ClassNotFoundException cnfe){
            System.err.println("cnfe "+cnfe.getMessage());
        }
        catch(InvalidClassException ice){
            System.err.println("ice "+ice.getMessage());
        }
        catch(StreamCorruptedException sce){
            System.err.println("sce "+sce.getMessage());
        }
        catch(IOException ioe){
            System.err.println("ioe "+ioe.getMessage());
            System.err.println(ioe.toString());
            System.err.println("ODE Length: " +((OptionalDataException)ioe).length);
            System.err.println("ODE EOF: " +((OptionalDataException)ioe).eof);
        }

    //}

    System.out.println("Recieved, closing connection");

    connection.close();
}

}

The output of the program after running both the android app and the desktop program are as followed:

ioe null java.io.OptionalDataException ODE Length: 2 ODE EOF: false

As requested, here is the PersonTest code:

package edit.rit.ce.whud;

import java.io.Serializable;

public class personTest extends Object implements Serializable {


private String firstName;
private String lastName;
private int age;

public personTest(String first, String last, int age){
    this.firstName = first;
    this.lastName = last;
    this.age = age;

}

public String getName(){
    return firstName +" "+ lastName;
}

public int getAge(){
    return age;
}

}

Was it helpful?

Solution

Remove this:

oos.writeChars("X");

You're confusing the readObject() method at the other end. Or else read those chars at the other end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top