diff --git a/app/src/main/java/pt/epvc/lazzycofee/MainActivity.java b/app/src/main/java/pt/epvc/lazzycofee/MainActivity.java index ebc760a..dc54ac7 100644 --- a/app/src/main/java/pt/epvc/lazzycofee/MainActivity.java +++ b/app/src/main/java/pt/epvc/lazzycofee/MainActivity.java @@ -42,6 +42,7 @@ public class MainActivity extends AppCompatActivity { private BluetoothSocket mBTSocket = null; // bi-directional client-to-client data path 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 boolean bluetoothDenied = false; @Override @@ -66,8 +67,6 @@ public class MainActivity extends AppCompatActivity { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN}, 2); - } else { - ligarBluetooth(); } } @@ -107,46 +106,46 @@ public class MainActivity extends AppCompatActivity { if (!mBTAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 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"; - new Thread() { - public void run() { - boolean fail = false; + BluetoothDevice device = mBTAdapter.getRemoteDevice(address); - 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 { + mBTSocket = createBluetoothSocket(device); + } catch (IOException e) { fail = true; - mBTSocket.close(); - } catch (IOException e2) { 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) { - mConnectedThread = new ConnectedThread(mBTSocket); - mConnectedThread.start(); - } - } - }.start(); + }.start(); + } + + } @Override @@ -156,9 +155,13 @@ public class MainActivity extends AppCompatActivity { if (requestCode == REQUEST_ENABLE_BT) { // Make sure the request was successful if (resultCode == RESULT_OK) { - Toast.makeText(this, "Enabled", Toast.LENGTH_SHORT).show(); - } else - Toast.makeText(this, "Disabled", Toast.LENGTH_SHORT).show(); + ligarBluetooth(); + Toast.makeText(this, "Bluetooth Ligado", 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 protected void onResume() { super.onResume(); - if (mBTSocket == null || !mBTSocket.isConnected()) { + if ((mBTSocket == null || !mBTSocket.isConnected()) && !bluetoothDenied) { ligarBluetooth(); } }