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