예제 #1
0
    public void run() {
      byte[] buffer = new byte[1024];
      int bytes;

      // Keep listening to the InputStream while connected
      while (true) {
        try {
          // Read from the InputStream
          bytes = mmInStream.read(buffer);
          final byte[] finalBuff = Arrays.copyOf(buffer, bytes);

          mLoggerListener.log("BLUETOOTH: received " + Convert.bytesToHex(finalBuff));
          mContext.post(
              new Runnable() {
                @Override
                public void run() {
                  mContext.getCommunicatorInstance().received(finalBuff);
                }
              });
        } catch (IOException e) {
          Log.e(TAG, "disconnected", e);
          connectionLost();
          retryConnection(mmDevice);
          break;
        }
      }
    }
예제 #2
0
    /**
     * Write to the connected OutStream.
     *
     * @param buffer The bytes to write
     */
    public void write(final byte[] buffer) {
      try {
        mmOutStream.write(buffer);

        // Share the sent message back to the UI Activity
        mLoggerListener.log("BLUETOOTH: sent " + Convert.bytesToHex(buffer));
      } catch (IOException e) {
        // Connection was lost
        Log.e(TAG, "Exception during write", e);
        connectionLost();
        retryConnection(mmDevice);
      }
    }