11

I have been following this Android guide for Bluetooth communication

To explain exactly what I want to do, when the two devices are paired, two different activities open up on each device (server and client) where on the server activity I have different buttons, and on the client activity there is just a textview. I want to be able to press a button on the server device and display it on the client.

I have managed to establish a connection between the two devices, but now I want to send data which I have not been able to do.

They give this code for data transfer:

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

public ConnectedThread(BluetoothSocket socket) {
    mmSocket = 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);
            // Send the obtained bytes to the UI activity
            mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                    .sendToTarget();
        } 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) { }
}

/* Call this from the main activity to shutdown the connection */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
}
}

But this line generates an error

// Send the obtained bytes to the UI activity
            mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();

And is not explained in the guide. I don't know what the mHandler is or does.

Apart from the error, I don't even really understand where to put this code. Should it be in the second activities (server and client) that I open or in the main? If in the Server activity, should it be in the onClick method for all the buttons with a different byte code to send for each button? And in this code, how do we distinguish who is sending and who is receiving?

2
  • Have you added appropriate permissions in manifest.xml file ?? <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    – addy123
    Jan 19, 2014 at 18:50
  • And <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> or <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> . Aug 26, 2018 at 11:36

5 Answers 5

13

Check out the BluetoothChat example that Google provides in the SDK. It'll show you how to implement basic sending of text over bluetooth.

1
1

You can also try the tutorial example here

0

Can you please describe the error as seen by you?

As informed by Ankit and Addy, BlueToothChat is the best code for you to refer. Conduct an experiment by loading it on 2 android devices - use one as server other as client to exchange the messages between them. Such experiment will help you to understand it's code and decide your coding logic.

0

mHandler is used for passing message from your BluetoothHandle.java to your Activity. This will help you to update messages on your screen which are returned by BluetoothHandler.

you have to create mHandler from your activity and call your handler like this -

mBluetoothHandler = new BluetoothHandler(this, mHandler);

and your BluetoothHandler.java has constructor like this -

public class BluetoothHandler { 

    public BluetoothHandler(Context context, Handler handler) {
            mAdapter = BluetoothAdapter.getDefaultAdapter();
            mState = STATE_NONE;
            mHandler = handler;
            mcontext = context;
   }

}

For more details, please refer Android sample project of Bluetooth Chat . You can also use this link : http://myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html

1
0
// Enter code here

Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        byte[] writeBuf = (byte[]) msg.obj;
        int begin = (int)msg.arg1;
        int end = (int)msg.arg2;

        switch(msg.what) {
            case 1:
                String writeMessage = new String(writeBuf);
                writeMessage = writeMessage.substring(begin, end);
                break;
        }
    }
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.