Remoção de código inútil
parent
5d4ca9abf9
commit
7109027e8e
|
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
|
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package pt.epvc.lazzycofee;
|
package pt.epvc.lazzycofee;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothSocket;
|
import android.bluetooth.BluetoothSocket;
|
||||||
|
|
@ -8,6 +9,7 @@ import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
|
|
@ -52,10 +54,22 @@ public class MainActivity extends AppCompatActivity {
|
||||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||||
return insets;
|
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
|
mBTAdapter = BluetoothAdapter.getDefaultAdapter(); // get a handle on the bluetooth radio
|
||||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
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();
|
ligarBluetooth();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void cafeCurto(View view) {
|
public void cafeCurto(View view) {
|
||||||
Toast.makeText(this, "Café Curto", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "Café Curto", Toast.LENGTH_SHORT).show();
|
||||||
|
|
@ -83,6 +97,12 @@ public class MainActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ligarBluetooth() {
|
public void ligarBluetooth() {
|
||||||
|
if (mBTAdapter == null) {
|
||||||
|
runOnUiThread(() ->
|
||||||
|
Toast.makeText(MainActivity.this, "Dispositivo não suporta Bluetooth", Toast.LENGTH_LONG).show()
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
|
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (!mBTAdapter.isEnabled()) {
|
if (!mBTAdapter.isEnabled()) {
|
||||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||||
|
|
@ -104,20 +124,21 @@ public class MainActivity extends AppCompatActivity {
|
||||||
mBTSocket = createBluetoothSocket(device);
|
mBTSocket = createBluetoothSocket(device);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
fail = true;
|
fail = true;
|
||||||
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
Log.d("bluetooth","Socket creation failed");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
mBTAdapter.cancelDiscovery();
|
||||||
mBTSocket.connect();
|
mBTSocket.connect();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
try {
|
try {
|
||||||
fail = true;
|
fail = true;
|
||||||
mBTSocket.close();
|
mBTSocket.close();
|
||||||
} catch (IOException e2) {
|
} catch (IOException e2) {
|
||||||
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
Log.d("bluetooth","Socket creation failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (fail == false) {
|
if (fail == false) {
|
||||||
|
|
@ -157,6 +178,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
try {
|
try {
|
||||||
tmpOut = socket.getOutputStream();
|
tmpOut = socket.getOutputStream();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
Log.e("Bluetooth", "Erro ao obter OutputStream", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
mmOutStream = tmpOut;
|
mmOutStream = tmpOut;
|
||||||
|
|
@ -166,13 +188,23 @@ public class MainActivity extends AppCompatActivity {
|
||||||
byte[] bytes = input.getBytes(); //converts entered String into bytes
|
byte[] bytes = input.getBytes(); //converts entered String into bytes
|
||||||
try {
|
try {
|
||||||
mmOutStream.write(bytes);
|
mmOutStream.write(bytes);
|
||||||
} catch (IOException e) { }
|
} catch (IOException e) {
|
||||||
|
Log.e("Bluetooth", "Erro ao obter OutputStream", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void desligarBluetooth() {
|
private void desligarBluetooth() {
|
||||||
try {
|
try {
|
||||||
|
if (mConnectedThread != null) {
|
||||||
|
mConnectedThread.interrupt();
|
||||||
|
}
|
||||||
|
if (mBTSocket != null) {
|
||||||
mBTSocket.close();
|
mBTSocket.close();
|
||||||
} catch (IOException e) {}
|
mBTSocket = null;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e("Bluetooth", "Erro ao desligar", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -184,6 +216,8 @@ public class MainActivity extends AppCompatActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
if (mBTSocket == null || !mBTSocket.isConnected()) {
|
||||||
ligarBluetooth();
|
ligarBluetooth();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue