How to send a text message to a paired device through bluetooth in android?

41,742

the bluetoothchat sample is actually the perfect thing to use if you are new in using the bluetooth api.

assuming that you are using only one Activity for your application which is the BluetoothChat class :

for sending text to the device you are connected to, use the "sendMessage(String message)" method in the BluetoothChat class to send text.

as for receiving and handling the text, you will find also handleMessage(Message msg) method somewhere in the bluetoothchat class then go this part:

case MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;
            // construct a string from the valid bytes in the buffer
            String readMessage = new String(readBuf, 0, msg.arg1);

see the readMessage String?

this is the text that that you receive from the other device , now you can handle it as you want.

then simply change the main layout that the BluetoothChat class refers to, then in BluetoothChat chat either comment or delete the parts that have errors which actually will be the parts in the UI u have deleted or changed.

i know the code may sound messy but this is the easiest way to use it quickly as possible and watching video tutorials or text tutorials for hours will just make it more complicated, believe me i tried this before.

Share:
41,742
Ekanta Swain
Author by

Ekanta Swain

Updated on July 14, 2022

Comments

  • Ekanta Swain
    Ekanta Swain almost 2 years

    In my app I want to send and receive text message through bluetooth. I can see in my listview a list of paired device name and address.But when I am trying to send a text to a paired device nothing happens. In other device there is no text received.

    This is my Code to send message to a paired device.

    private void sendDataToPairedDevice(String message, String adress) {
            byte[] toSend = message.getBytes();
            try {
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(adress);
                // BluetoothSocket socket
                // =device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
                BluetoothSocket socket = null;
                Method m = null;
                try {
                    m = device.getClass().getMethod("createRfcommSocket",
                            new Class[] { int.class });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    socket = (BluetoothSocket) m.invoke(device, 1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                OutputStream mmOutStream = socket.getOutputStream();
                mBluetoothAdapter.cancelDiscovery();
                socket.connect();
                mmOutStream.write(toSend);
            } catch (Exception e) {
                Log.d("TAG", "Exception during write", e);
            }
        }