Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. public class CommunicationExample extends Thread {
  2.     private static final UUID UUID_PROBE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  3.     private static final String TAG = CommunicationExample.class.getSimpleName();
  4.     private CommunicationExample.CommunicationListener mListener;
  5.     private boolean mRunning;
  6.     private BluetoothSocket mBluetoothSocket;
  7.     private InputStream mInputStream;
  8.     private OutputStream mOutputStream;
  9.  
  10.     public interface CommunicationListener {
  11.         void onMessageReceived(String msg);
  12.     }
  13.  
  14.     public CommunicationExample(
  15.             @NonNull BluetoothDevice device,
  16.             @Nullable CommunicationExample.CommunicationListener listener) throws IOException {
  17.         // Faz a conexão utilizando o mesmo UUID que o servidor utilizou, no caso, o aferidor
  18.         final BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID_PROBE);
  19.         socket.connect();
  20.         this.mBluetoothSocket = socket;
  21.         this.mInputStream = socket.getInputStream();
  22.         this.mOutputStream = socket.getOutputStream();
  23.         this.mListener = listener;
  24.     }
  25.  
  26.     @Override
  27.     public void run() {
  28.         mRunning = true;
  29.         // Faz a leitura
  30.         final byte[] bytes = new byte[1024];
  31.         int length;
  32.         // Fica em loop para receber as mensagens
  33.         while (mRunning) {
  34.             try {
  35.                 Log.d(TAG, "Aguardando mensagem");
  36.                 // Lê a mensagem (fica bloqueado até receber)
  37.                 length = mInputStream.read(bytes);
  38.                 final String msg = new String(bytes, 0, length);
  39.                 Log.d(TAG, "Mensagem: " + msg);
  40.                 // Recebeu a mensagem (informa o listener)
  41.                 if (mListener != null)
  42.                     mListener.onMessageReceived(msg);
  43.             } catch (Exception e) {
  44.                 Log.e(TAG, "Error na leitura da mensagem", e);
  45.             }
  46.         }
  47.     }
  48.    
  49.     public void stopCommunication() {
  50.         mRunning = false;
  51.         mListener = null;
  52.         try {
  53.             if (mBluetoothSocket != null) {
  54.                 mBluetoothSocket.close();
  55.             }
  56.             if (mInputStream != null) {
  57.                 mInputStream.close();
  58.             }
  59.             if (mOutputStream != null) {
  60.                 mOutputStream.close();
  61.             }
  62.         } catch (IOException e) {
  63.             Log.e(TAG, "Erro", e);
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement