how to send data (a string) to a paired device in android?

前端 未结 1 698
北荒
北荒 2021-01-14 11:13

I am working on bluetooth for the first time. I got the list of paired devices. Now my requirement is I need to send some data (a string) to the device. how can I do that? I

相关标签:
1条回答
  • 2021-01-14 11:55

    Something like this might suffice:

    DataOutputStream os;
    
    BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
    
    BroadcastReceiver discoveryResult = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
            BluetoothDevice remoteDevice;
    
            remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    
            Toast.makeText(getApplicationContext(), "Discovered: " + remoteDeviceName + " address " + remoteDevice.getAddress(), Toast.LENGTH_SHORT).show();
    
            try{
                BluetoothDevice device = bluetooth.getRemoteDevice(remoteDevice.getAddress());
    
                Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
    
                BluetoothSocket clientSocket =  (BluetoothSocket) m.invoke(device, 1);
    
                clientSocket.connect();
    
                os = new DataOutputStream(clientSocket.getOutputStream());
    
                new clientSock().start();
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("BLUETOOTH", e.getMessage());
            }
        }
    };
    
    registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    
    bluetooth.enable();
    if (!bluetooth.isDiscovering()) {
        bluetooth.startDiscovery();
    }
    
    public class clientSock extends Thread {
        public void run () {
            try {
                os.writeBytes("anything you want"); // anything you want
                os.flush();
            } catch (Exception e1) {
                e1.printStackTrace();
                return;
            }
        }
    }
    

    You will also need a lot of imports such as these:

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.lang.reflect.Method;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.List;
    import java.util.UUID;
    
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothSocket;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.Toast;
    

    note that not all imports are necessary for this example code, your IDE might help you to sort them out for you.

    Pass data on the os.writeBytes("anything you want"); // anything you want line.

    You will also need Permissions

    0 讨论(0)
提交回复
热议问题