2

I already wrote with the help of tutorials I found on the Internet a app for Android that connect to a Bluetooth device. This app used the InputStream and OutputStream object to connect and establish a connection. Because I had to transfer small amount of data this solution was OK because I could only send bytes.

Now I would like to change my old code to use DataInputStream and DataOutputStream to send complex data easily. I tried to modify my original code by simply adding the Data identifier before my InputStream and OutputStream but this created error in my code. Could someone explain me how to use the DataInputStream and DataOutputStream correctly so I will not get errors. This is my old code :

 private class ConnectedThread extends Thread {
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                hBluetooth.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget();        // Send to message queue Handler
            } catch (IOException e) {
                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void send(String message) {
        if(D)   Log.d(TAG, "...Data to send: " + message + "...");
        if (btSocket != null && btSocket.isConnected()) {

        byte[] msgBuffer = message.getBytes();
        try {
            mmOutStream.write(msgBuffer);
            Log.e(TAG, "Int" +msgBuffer);
        } catch (IOException e) {
            Log.d(TAG, "...Error data send: " + e.getMessage() + "...");     
          }
        }

    }

and here the modified version :

 private class ConnectedThread extends Thread {
     DataInputStream mmInStream;
     DataOutputStream mmOutStream;


    public ConnectedThread(BluetoothSocket socket) {
       InputStream tmpIn = null;
       OutputStream tmpOut = null;

        mmInStream = new DataInputStream(tmpIn);
        mmOutStream = new DataOutputStream(tmpOut);

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

Thanks for any inputs!

user_CC
  • 4,686
  • 3
  • 20
  • 15
Mathieu660
  • 61
  • 1
  • 9

1 Answers1

1

IF I have understood your question correctly then do something like this to get DataInputStream and DataOutputStream objects:

  mmInStream = new DataInputStream(tmpIn);
  mmOutStream = new DataInputStream(tmpOut);

The variables will be declared like below outside the class if you need them to be global as I can see from your edit you have done:

   DataInputStream mmInStream;
   DataOutputStream mmOutStream;

EDIT For the updated question:

public ConnectedThread(BluetoothSocket socket) {
   InputStream tmpIn = null;
   OutputStream tmpOut = null;


    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) { }

    mmInStream = new DataInputStream(tmpIn);
    mmOutStream = new DataOutputStream(tmpOut);


}
user_CC
  • 4,686
  • 3
  • 20
  • 15
  • I think this solution is exactly the same as my modified version that don't work right ? – Mathieu660 Mar 14 '13 at 22:56
  • no it is not in yours you are casting the objects. You need to create the objects of DataInputStream and DataoutputStream by using the right constructor; http://developer.android.com/reference/java/io/DataInputStream.html http://developer.android.com/reference/java/io/DataOutputStream.html Try it and let me know many thanks – user_CC Mar 14 '13 at 22:59
  • OK I have done some modification as you can see on the original post but now I have a type mismatch at mmInStream = tmpIn; and mmOutStream = tmpOut; Maybe I do something wrong .... – Mathieu660 Mar 14 '13 at 23:26
  • look at my answer do like this: mmInStream = new DataInputStream(tmpIn); mmOutStream = new DataInputStream(tmpOut); – user_CC Mar 14 '13 at 23:30
  • I did look at the modified version in my post – Mathieu660 Mar 14 '13 at 23:33
  • look at my updated answer; Acutally you did those before getting the streams, once the objects are created then use the new Data Stream objects – user_CC Mar 14 '13 at 23:33
  • OK great this is working so we put a simple stream in a datastream object ! I get it. thanks It's working – Mathieu660 Mar 14 '13 at 23:39