Remoção de código inútil

dev
João Miranda 2025-11-05 10:02:03 +00:00
parent 5d4ca9abf9
commit 7109027e8e
2 changed files with 43 additions and 7 deletions

View File

@ -7,6 +7,8 @@
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"

View File

@ -1,5 +1,6 @@
package pt.epvc.lazzycofee;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
@ -8,6 +9,7 @@ import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
@ -52,10 +54,22 @@ public class MainActivity extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN},
2);
}
mBTAdapter = BluetoothAdapter.getDefaultAdapter(); // get a handle on the bluetooth radio
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN},
2);
} else {
ligarBluetooth();
}
}
public void cafeCurto(View view) {
Toast.makeText(this, "Café Curto", Toast.LENGTH_SHORT).show();
@ -83,6 +97,12 @@ public class MainActivity extends AppCompatActivity {
}
public void ligarBluetooth() {
if (mBTAdapter == null) {
runOnUiThread(() ->
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 {
if (mConnectedThread != null) {
mConnectedThread.interrupt();
}
if (mBTSocket != null) {
mBTSocket.close();
} catch (IOException e) {}
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();
if (mBTSocket == null || !mBTSocket.isConnected()) {
ligarBluetooth();
}
}
}