Bluetooth data transfer between two Android devices
Asked Answered
V

5

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?

Vapory answered 19/1, 2014 at 18:42 Comment(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" />Cicatrize
And <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> or <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> .Freshet
M
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.

Monsignor answered 19/1, 2014 at 18:46 Comment(1)
The link in this answer is very old. There is an updated official example of the BluetoothChat app here: github.com/googlesamples/android-BluetoothChatFreshet
C
1

You can also try the tutorial example here

Cicatrize answered 19/1, 2014 at 18:59 Comment(0)
A
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

Auklet answered 20/1, 2014 at 10:21 Comment(1)
To link in the question doesn't work, but you can find the (current) official BluetoothChat example app here: github.com/googlesamples/android-BluetoothChatFreshet
W
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.

Wentzel answered 5/2, 2014 at 18:7 Comment(0)
P
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;
        }
    }
};
Parasynthesis answered 11/10, 2014 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.