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

[亡魂溺海] 提交于 2019-12-10 23:47:34

问题


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);
        }
    }

回答1:


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.



来源:https://stackoverflow.com/questions/21476835/how-to-send-a-text-message-to-a-paired-device-through-bluetooth-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!