Transferir dados de Bluetooth

Depois de ter se conectado a um dispositivo Bluetooth dispositivo, cada um Um tem BluetoothSocket. Agora é possível compartilhar informações entre dispositivos. Usando o BluetoothSocket, o método para transferir dados é o seguinte:

  1. Consiga o InputStream e OutputStream que processam transmissões pelo soquete usando getInputStream() e getOutputStream(), respectivamente.

  2. Leia e grave dados nos streams com o read(byte[]) e write(byte[])

Naturalmente, é necessário considerar detalhes de implementação. Especificamente, você deve usar uma linha de execução dedicada para ler e gravar no stream. Isso é importante porque os métodos read(byte[]) e write(byte[]) chamadas de bloqueio. O método read(byte[]) fica bloqueado até que haja algo para ler o stream. O método write(byte[]) geralmente não bloqueia, mas poderá bloquear o controle de fluxo se o dispositivo remoto não estiver chamando read(byte[]) com rapidez suficiente e, como resultado, os buffers intermediários ficam cheios. Então, você precisa precisa dedicar o loop principal na linha de execução à leitura do InputStream. Você pode usar um método público separado no thread para iniciar gravações no OutputStream:

Exemplo

Confira a seguir um exemplo de como transferir dados entre dois dispositivos conectado por Bluetooth:

private const val TAG = "MY_APP_DEBUG_TAG"

// Defines several constants used when transmitting messages between the
// service and the UI.
const
val MESSAGE_READ: Int = 0
const
val MESSAGE_WRITE: Int = 1
const
val MESSAGE_TOAST: Int = 2
// ... (Add other message types here as needed.)

class MyBluetoothService(
       
// handler that gets info from Bluetooth service
       
private val handler: Handler) {

   
private inner class ConnectedThread(private val mmSocket: BluetoothSocket) : Thread() {

       
private val mmInStream: InputStream = mmSocket.inputStream
       
private val mmOutStream: OutputStream = mmSocket.outputStream
       
private val mmBuffer: ByteArray = ByteArray(1024) // mmBuffer store for the stream

       
override fun run() {
           
var numBytes: Int // bytes returned from read()

           
// Keep listening to the InputStream until an exception occurs.
           
while (true) {
               
// Read from the InputStream.
               numBytes
= try {
                   mmInStream
.read(mmBuffer)
               
} catch (e: IOException) {
                   
Log.d(TAG, "Input stream was disconnected", e)
                   
break
               
}

               
// Send the obtained bytes to the UI activity.
               
val readMsg = handler.obtainMessage(
                       MESSAGE
_READ, numBytes, -1,
                       mmBuffer
)
               readMsg
.sendToTarget()
           
}
       
}

       
// Call this from the main activity to send data to the remote device.
       
fun write(bytes: ByteArray) {
           
try {
               mmOutStream
.write(bytes)
           
} catch (e: IOException) {
               
Log.e(TAG, "Error occurred when sending data", e)

               
// Send a failure message back to the activity.
               
val writeErrorMsg = handler.obtainMessage(MESSAGE_TOAST)
               
val bundle = Bundle().apply {
                   putString
("toast", "Couldn't send data to the other device")
               
}
               writeErrorMsg
.data = bundle
               handler
.sendMessage(writeErrorMsg)
               
return
           
}

           
// Share the sent message with the UI activity.
           
val writtenMsg = handler.obtainMessage(
                   MESSAGE
_WRITE, -1, -1, mmBuffer)
           writtenMsg
.sendToTarget()
       
}

       
// Call this method from the main activity to shut down the connection.
       
fun cancel() {
           
try {
               mmSocket
.close()
           
} catch (e: IOException) {
               
Log.e(TAG, "Could not close the connect socket", e)
           
}
       
}
   
}
}
public class MyBluetoothService {
   
private static final String TAG = "MY_APP_DEBUG_TAG";
   
private Handler handler; // handler that gets info from Bluetooth service

   
// Defines several constants used when transmitting messages between the
   
// service and the UI.
   
private interface MessageConstants {
       
public static final int MESSAGE_READ = 0;
       
public static final int MESSAGE_WRITE = 1;
       
public static final int MESSAGE_TOAST = 2;

       
// ... (Add other message types here as needed.)
   
}

   
private class ConnectedThread extends Thread {
       
private final BluetoothSocket mmSocket;
       
private final InputStream mmInStream;
       
private final OutputStream mmOutStream;
       
private byte[] mmBuffer; // mmBuffer store for the stream

       
public ConnectedThread(BluetoothSocket socket) {
           mmSocket
= socket;
           
InputStream tmpIn = null;
           
OutputStream tmpOut = null;

           
// Get the input and output streams; using temp objects because
           
// member streams are final.
           
try {
               tmpIn
= socket.getInputStream();
           
} catch (IOException e) {
               
Log.e(TAG, "Error occurred when creating input stream", e);
           
}
           
try {
               tmpOut
= socket.getOutputStream();
           
} catch (IOException e) {
               
Log.e(TAG, "Error occurred when creating output stream", e);
           
}

           mmInStream
= tmpIn;
           mmOutStream
= tmpOut;
       
}

       
public void run() {
           mmBuffer
= new byte[1024];
           
int numBytes; // bytes returned from read()

           
// Keep listening to the InputStream until an exception occurs.
           
while (true) {
               
try {
                   
// Read from the InputStream.
                   numBytes
= mmInStream.read(mmBuffer);
                   
// Send the obtained bytes to the UI activity.
                   
Message readMsg = handler.obtainMessage(
                           
MessageConstants.MESSAGE_READ, numBytes, -1,
                           mmBuffer
);
                   readMsg
.sendToTarget();
               
} catch (IOException e) {
                   
Log.d(TAG, "Input stream was disconnected", e);
                   
break;
               
}
           
}
       
}

       
// Call this from the main activity to send data to the remote device.
       
public void write(byte[] bytes) {
           
try {
               mmOutStream
.write(bytes);

               
// Share the sent message with the UI activity.
               
Message writtenMsg = handler.obtainMessage(
                       
MessageConstants.MESSAGE_WRITE, -1, -1, mmBuffer);
               writtenMsg
.sendToTarget();
           
} catch (IOException e) {
               
Log.e(TAG, "Error occurred when sending data", e);

               
// Send a failure message back to the activity.
               
Message writeErrorMsg =
                       handler
.obtainMessage(MessageConstants.MESSAGE_TOAST);
               
Bundle bundle = new Bundle();
               bundle
.putString("toast",
                       
"Couldn't send data to the other device");
               writeErrorMsg
.setData(bundle);
               handler
.sendMessage(writeErrorMsg);
           
}
       
}

       
// Call this method from the main activity to shut down the connection.
       
public void cancel() {
           
try {
               mmSocket
.close();
           
} catch (IOException e) {
               
Log.e(TAG, "Could not close the connect socket", e);
           
}
       
}
   
}
}

Depois que o construtor adquire os streams necessários, a linha de execução aguarda dados sejam disponibilizadas pela InputStream. Quando read(byte[]) retornar com dados de no stream, os dados são enviados para a atividade principal usando um Handler da classe mãe. A linha de execução espera que mais bytes sejam lidos do InputStream.

Para enviar dados de saída, chame o método write() da linha de execução no a atividade e passar os bytes a serem enviados. Esse método chama write(byte[]) para envie os dados para o dispositivo remoto. Se um IOException é gerado ao chamar write(byte[]), a linha de execução envia um aviso à atividade principal, explicando ao o usuário de que o dispositivo não conseguiu enviar os bytes fornecidos para o outro (conectado).

O método cancel() da linha de execução permite encerrar a conexão a qualquer horário fechando BluetoothSocket. Sempre chame esse método quando terminar usando a conexão Bluetooth.

Para ver uma demonstração do uso das APIs Bluetooth, consulte o exemplo de bate-papo Bluetooth App no GitHub.