From 7109027e8e154b7545c4f7eac71c3ba23cd205cc Mon Sep 17 00:00:00 2001 From: joaomiranda Date: Wed, 5 Nov 2025 10:02:03 +0000 Subject: [PATCH] =?UTF-8?q?Remo=C3=A7=C3=A3o=20de=20c=C3=B3digo=20in=C3=BA?= =?UTF-8?q?til?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 + .../java/pt/epvc/lazzycofee/MainActivity.java | 48 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3e34856..09763de 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,8 @@ + + + Toast.makeText(MainActivity.this, "Dispositivo não suporta Bluetooth", Toast.LENGTH_LONG).show() + ); + return; + } mBTAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBTAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); @@ -104,20 +124,21 @@ public class MainActivity extends AppCompatActivity { mBTSocket = createBluetoothSocket(device); } catch (IOException e) { fail = true; - Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show(); + Log.d("bluetooth","Socket creation failed"); } try { if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { return; } + mBTAdapter.cancelDiscovery(); mBTSocket.connect(); } catch (IOException e) { try { fail = true; mBTSocket.close(); } catch (IOException e2) { - Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show(); + Log.d("bluetooth","Socket creation failed"); } } if (fail == false) { @@ -157,6 +178,7 @@ public class MainActivity extends AppCompatActivity { try { tmpOut = socket.getOutputStream(); } catch (IOException e) { + Log.e("Bluetooth", "Erro ao obter OutputStream", e); } mmOutStream = tmpOut; @@ -166,13 +188,23 @@ public class MainActivity extends AppCompatActivity { byte[] bytes = input.getBytes(); //converts entered String into bytes try { mmOutStream.write(bytes); - } catch (IOException e) { } + } catch (IOException e) { + Log.e("Bluetooth", "Erro ao obter OutputStream", e); + } } } private void desligarBluetooth() { try { - mBTSocket.close(); - } catch (IOException e) {} + if (mConnectedThread != null) { + mConnectedThread.interrupt(); + } + if (mBTSocket != null) { + mBTSocket.close(); + mBTSocket = null; + } + } catch (IOException e) { + Log.e("Bluetooth", "Erro ao desligar", e); + } } @Override @@ -184,6 +216,8 @@ public class MainActivity extends AppCompatActivity { @Override protected void onResume() { super.onResume(); - ligarBluetooth(); + if (mBTSocket == null || !mBTSocket.isConnected()) { + ligarBluetooth(); + } } } \ No newline at end of file