0
votes

Hi anyone knows how to send data from the Android phone to the remote Bluetooth device (through OutputStream) without needing to wait for a desired number of bytes to be read by InputStream?

I tried to implement the code as shown below to send data to the remote Bluetooth device but nothing was being sent across.

EDIT: Added in the connectedThread codes:

ConnectedThread.java:

public class ConnectedThread extends Thread{
        public final BluetoothSocket mmSocket;
        public final InputStream mmInStream;
        public final OutputStream mmOutStream;

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

            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);
                } catch (IOException e) {
                    break;
                }
            }  
        }


        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) { }
        }   

public void sendData() throws IOException{
            write("1");
        }

ConnectThread.java:

public class ConnectThread extends Thread{
        public final BluetoothSocket mmSocket;
        public final BluetoothDevice mmDevice;
        public final BluetoothAdapter mmAdapter;

        BluetoothConnection mConnectedThread = new BluetoothConnection();

        public ConnectThread(BluetoothDevice device, BluetoothAdapter mAdapter) throws SecurityException, NoSuchMethodException,  IllegalArgumentException, IllegalAccessException, InvocationTargetException {
            mmAdapter = mAdapter;
            mmDevice = device = mmAdapter.getRemoteDevice("00:06:66:4F:90:F8");
            BluetoothSocket tmp = null;

        try{
            tmp = mmDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            }catch(IOException e){
                System.out.println("Failed to create Rfcomm and connect to remote device");
            }
            mmSocket = tmp;
        }

        public void run(){
            mmAdapter.cancelDiscovery();

            //sleep to allow time for bluetooth device to get ready 
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e5) {
                e5.printStackTrace();
            }

            //handler get message from arduino: that bluetooth device is ready 

            //Get BluetoothDevice object
            try{
                //Connect to device 
                mmSocket.connect();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }catch(IOException e){
            try{
                //Connect to device 
                mmSocket.connect();
            }catch(IOException e1){
                }
                return; 
        }
            return;
        }

            mConnectedThread.ConnectedThread(mmSocket);
        }

    }

BluetoothConnection.java:

public class BluetoothConnection extends Service {

public final BluetoothAdapter mAdapter = null;
public ConnectThread mConnectThread;
    public ConnectedThread mConnectedThread;

private final IBinder mBinder = new BluetoothService();

public class BluetoothService extends Binder{       
    BluetoothConnection getService(){
            return BluetoothConnection.this;
    }
}

@Override 
public IBinder onBind (Intent intent){
    Log.d("Blueooth Connection", "Intent Binder");
    return mBinder;
} 

public synchronized void connect(BluetoothDevice device, BluetoothAdapter mAdapter)     throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{

    try{
        mConnectThread = new ConnectThread(device, mAdapter);
        mConnectThread.start();

    }catch(SecurityException e){
        System.out.println("SecurityException at connect()");
    } catch(IllegalArgumentException e){
        System.out.println("IllegalArgumentException at connect()");
    }catch(NullPointerException e)
 {System.out.println("Null Pointer Exception at connect()");
    }
}

public synchronized void ConnectedThread(BluetoothSocket socket){
    mConnectedThread = new ConnectedThread(socket);
    mConnectedThread.start();
}

public void write(String out){
    //create temporary object 
    ConnectedThread r;
    synchronized(this){
        r = mConnectedThread;
    }           
    r.write(out);
}    

@Override
public void onDestroy() {
}
}

In another Activity: I called sendData(); to write "1" to Arduino.

1
What is the name of your Android device (with its Android version number) and the exact model name of your bluetooth device? - Stephan Branczyk
It's most likely a compatibility issue. Bluetooth in Android is actually quite fragmented. And it only improves as you go up the version numbers. I'm sorry I don't have an answer for you. This question requires too much research for me. - Stephan Branczyk
Please show your connection related code. - Heeryu
@hemerly I have added in the connection related code. - shannon

1 Answers

0
votes

Edit:

Maybe it's not what is causing your problem, but you should not do something like this:

    //Get BluetoothDevice object
    try{
        //Connect to device 
        mmSocket.connect();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }catch(IOException e){
        try{
            //Connect to device 
            mmSocket.connect();
        }catch(IOException e1){
        }
        return; 
    }

You may end with an unconnected mmSocket because you are just ignoring IOException and trying again. Try to e.printStackTrace() in the first try/catch block to see if you have connection issues.

In general, you should never just ignore an exception, as they represent abnormalities in program flow. At worst, print an error message to help you while debugging.

Edit 2:

Please change

public void write(byte[] bytes) {
    try {
        mmOutStream.write(bytes);
    } catch (IOException e) { }
}   

to

public void write(byte[] bytes) {
    try {
        mmOutStream.write(bytes);
    } catch (IOException e) { 
        e.printStackTrace();
    }
}   

and take a look at the logcat output to see if it shows something wrong.