Otimização da gestão da ligação do bluetooth do dispositivo

dev 1.0
João Miranda 2025-11-05 10:19:14 +00:00
parent 66e5b5e30b
commit 23a662099a
1 changed files with 40 additions and 37 deletions

View File

@ -42,6 +42,7 @@ public class MainActivity extends AppCompatActivity {
private BluetoothSocket mBTSocket = null; // bi-directional client-to-client data path private BluetoothSocket mBTSocket = null; // bi-directional client-to-client data path
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private final static int REQUEST_ENABLE_BT = 1; // used to identify adding bluetooth names private final static int REQUEST_ENABLE_BT = 1; // used to identify adding bluetooth names
private boolean bluetoothDenied = false;
@Override @Override
@ -66,8 +67,6 @@ public class MainActivity extends AppCompatActivity {
ActivityCompat.requestPermissions(this, ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN}, new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN},
2); 2);
} else {
ligarBluetooth();
} }
} }
@ -107,46 +106,46 @@ public class MainActivity extends AppCompatActivity {
if (!mBTAdapter.isEnabled()) { if (!mBTAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Toast.makeText(MainActivity.this, "Bluetooth turned on", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Bluetooth is already on", Toast.LENGTH_SHORT).show();
} }
else{
final String address = "98:D3:21:FC:7F:DF";
new Thread() {
public void run() {
boolean fail = false;
final String address = "98:D3:21:FC:7F:DF"; BluetoothDevice device = mBTAdapter.getRemoteDevice(address);
new Thread() {
public void run() {
boolean fail = false;
BluetoothDevice device = mBTAdapter.getRemoteDevice(address);
try {
mBTSocket = createBluetoothSocket(device);
} catch (IOException e) {
fail = true;
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 { try {
mBTSocket = createBluetoothSocket(device);
} catch (IOException e) {
fail = true; fail = true;
mBTSocket.close();
} catch (IOException e2) {
Log.d("bluetooth","Socket creation failed"); 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) {
Log.d("bluetooth","Socket creation failed");
}
}
if (fail == false) {
mConnectedThread = new ConnectedThread(mBTSocket);
mConnectedThread.start();
}
} }
if (fail == false) { }.start();
mConnectedThread = new ConnectedThread(mBTSocket); }
mConnectedThread.start();
}
}
}.start();
} }
@Override @Override
@ -156,9 +155,13 @@ public class MainActivity extends AppCompatActivity {
if (requestCode == REQUEST_ENABLE_BT) { if (requestCode == REQUEST_ENABLE_BT) {
// Make sure the request was successful // Make sure the request was successful
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
Toast.makeText(this, "Enabled", Toast.LENGTH_SHORT).show(); ligarBluetooth();
} else Toast.makeText(this, "Bluetooth Ligado", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Disabled", Toast.LENGTH_SHORT).show(); } else {
bluetoothDenied = true;
Toast.makeText(this, "A App Precisa de Bluetooth para Funcionar", Toast.LENGTH_SHORT).show();
finish();
}
} }
} }
@ -216,7 +219,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if (mBTSocket == null || !mBTSocket.isConnected()) { if ((mBTSocket == null || !mBTSocket.isConnected()) && !bluetoothDenied) {
ligarBluetooth(); ligarBluetooth();
} }
} }