|
|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
package pt.epvc.lazzycofee;
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
|
|
import android.bluetooth.BluetoothSocket;
|
|
|
|
|
@ -9,7 +8,6 @@ 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;
|
|
|
|
|
@ -37,12 +35,21 @@ import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
|
|
private ImageButton cafeCurto;
|
|
|
|
|
private ImageButton cafeMédio;
|
|
|
|
|
private ImageButton cafeLongo;
|
|
|
|
|
private Switch conetar;
|
|
|
|
|
private BluetoothAdapter mBTAdapter;
|
|
|
|
|
private Set<BluetoothDevice> mPairedDevices;
|
|
|
|
|
private ArrayAdapter<String> mBTArrayAdapter;
|
|
|
|
|
private ListView mDevicesListView;
|
|
|
|
|
private Handler mHandler; // Our main handler that will receive callback notifications
|
|
|
|
|
private ConnectedThread mConnectedThread; // bluetooth background worker thread to send and receive data
|
|
|
|
|
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;
|
|
|
|
|
private final static int MESSAGE_READ = 2; // used in bluetooth handler to identify message update
|
|
|
|
|
private final static int CONNECTING_STATUS = 3; // used in bluetooth handler to identify message status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ -55,19 +62,60 @@ 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);
|
|
|
|
|
}
|
|
|
|
|
cafeCurto = (ImageButton) findViewById(R.id.cafeCurto);
|
|
|
|
|
cafeMédio = (ImageButton) findViewById(R.id.cafeMedio);
|
|
|
|
|
cafeLongo = (ImageButton) findViewById(R.id.cafeLongo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mBTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mHandler = new Handler() {
|
|
|
|
|
public void handleMessage(android.os.Message msg) {
|
|
|
|
|
if (msg.what == MESSAGE_READ) {
|
|
|
|
|
String readMessage = null;
|
|
|
|
|
try {
|
|
|
|
|
readMessage = new String((byte[]) msg.obj, "UTF-8");
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
//mReadBuffer.setText(readMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.what == CONNECTING_STATUS) {
|
|
|
|
|
if (msg.arg1 == 1)
|
|
|
|
|
Toast.makeText(MainActivity.this, "Connected to Device: " + (String) (msg.obj), Toast.LENGTH_SHORT).show();
|
|
|
|
|
//mBluetoothStatus.setText("Connected to Device: " + (String)(msg.obj));
|
|
|
|
|
else
|
|
|
|
|
Toast.makeText(MainActivity.this, "Connection Failed", Toast.LENGTH_SHORT).show();
|
|
|
|
|
//mBluetoothStatus.setText("Connection Failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (mBTArrayAdapter == null) {
|
|
|
|
|
// Device does not support Bluetooth
|
|
|
|
|
//mBluetoothStatus.setText("Status: Bluetooth not found");
|
|
|
|
|
Toast.makeText(MainActivity.this, "Bluetooth device not found!", Toast.LENGTH_SHORT).show();
|
|
|
|
|
} else {
|
|
|
|
|
conetar = (Switch) findViewById(R.id.conetar);
|
|
|
|
|
conetar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
|
if (conetar.isChecked()) {
|
|
|
|
|
Toast.makeText(MainActivity.this, "Conetado", Toast.LENGTH_SHORT).show();
|
|
|
|
|
ligarBluetooth();
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(MainActivity.this, "Desconetado", Toast.LENGTH_SHORT).show();
|
|
|
|
|
desligarBluetooth();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
|
|
|
ligarBluetooth();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cafeCurto(View view) {
|
|
|
|
|
@ -96,19 +144,17 @@ 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);
|
|
|
|
|
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
|
|
|
|
//mBluetoothStatus.setText("Bluetooth enabled");
|
|
|
|
|
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{
|
|
|
|
|
//listarAparelhos();
|
|
|
|
|
final String address = "98:D3:21:FC:7F:DF";
|
|
|
|
|
new Thread() {
|
|
|
|
|
public void run() {
|
|
|
|
|
@ -120,34 +166,43 @@ public class MainActivity extends AppCompatActivity {
|
|
|
|
|
mBTSocket = createBluetoothSocket(device);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
fail = true;
|
|
|
|
|
Log.d("bluetooth","Socket creation failed");
|
|
|
|
|
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
// Establish the Bluetooth socket connection.
|
|
|
|
|
try {
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
|
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mBTAdapter.cancelDiscovery();
|
|
|
|
|
mBTSocket.connect();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
try {
|
|
|
|
|
fail = true;
|
|
|
|
|
mBTSocket.close();
|
|
|
|
|
mHandler.obtainMessage(CONNECTING_STATUS, -1, -1)
|
|
|
|
|
.sendToTarget();
|
|
|
|
|
} catch (IOException e2) {
|
|
|
|
|
Log.d("bluetooth","Socket creation failed");
|
|
|
|
|
//insert code to deal with this
|
|
|
|
|
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (fail == false) {
|
|
|
|
|
mConnectedThread = new ConnectedThread(mBTSocket);
|
|
|
|
|
mConnectedThread.start();
|
|
|
|
|
|
|
|
|
|
mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, "ASD")
|
|
|
|
|
.sendToTarget();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
|
|
|
|
|
// Check which request we're responding to
|
|
|
|
|
@ -155,72 +210,170 @@ public class MainActivity extends AppCompatActivity {
|
|
|
|
|
if (requestCode == REQUEST_ENABLE_BT) {
|
|
|
|
|
// Make sure the request was successful
|
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
// The user picked a contact.
|
|
|
|
|
// The Intent's data Uri identifies which contact was selected.
|
|
|
|
|
//mBluetoothStatus.setText("Enabled");
|
|
|
|
|
Toast.makeText(this, "Enabled", Toast.LENGTH_SHORT).show();
|
|
|
|
|
} else
|
|
|
|
|
Toast.makeText(this, "Disabled", Toast.LENGTH_SHORT).show();
|
|
|
|
|
//mBluetoothStatus.setText("Disabled");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void listarAparelhos() {
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mPairedDevices = mBTAdapter.getBondedDevices();
|
|
|
|
|
if (mBTAdapter.isEnabled()) {
|
|
|
|
|
// put it's one to the adapter
|
|
|
|
|
for (BluetoothDevice device : mPairedDevices)
|
|
|
|
|
mBTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
|
|
|
|
|
|
|
|
|
|
Toast.makeText(getApplicationContext(), "Show Paired Devices", Toast.LENGTH_SHORT).show();
|
|
|
|
|
} else
|
|
|
|
|
Toast.makeText(getApplicationContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void desligarBluetooth() {
|
|
|
|
|
//mBTAdapter.disable(); // turn off
|
|
|
|
|
//mBluetoothStatus.setText("Bluetooth disabled");
|
|
|
|
|
Toast.makeText(MainActivity.this, "Bluetooth turned Off", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
|
|
|
|
|
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
|
|
|
|
|
|
|
|
|
|
if (!mBTAdapter.isEnabled()) {
|
|
|
|
|
Toast.makeText(getBaseContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Toast.makeText(MainActivity.this, "Connecting...", Toast.LENGTH_SHORT).show();
|
|
|
|
|
//mBluetoothStatus.setText("Connecting...");
|
|
|
|
|
// Get the device MAC address, which is the last 17 chars in the View
|
|
|
|
|
String info = ((TextView) v).getText().toString();
|
|
|
|
|
final String address = info.substring(info.length() - 17);
|
|
|
|
|
final String name = info.substring(0, info.length() - 17);
|
|
|
|
|
|
|
|
|
|
// Spawn a new thread to avoid blocking the GUI one
|
|
|
|
|
new Thread() {
|
|
|
|
|
public void run() {
|
|
|
|
|
boolean fail = false;
|
|
|
|
|
|
|
|
|
|
BluetoothDevice device = mBTAdapter.getRemoteDevice(address);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
mBTSocket = createBluetoothSocket(device);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
fail = true;
|
|
|
|
|
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
// Establish the Bluetooth socket connection.
|
|
|
|
|
try {
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mBTSocket.connect();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
try {
|
|
|
|
|
fail = true;
|
|
|
|
|
mBTSocket.close();
|
|
|
|
|
mHandler.obtainMessage(CONNECTING_STATUS, -1, -1)
|
|
|
|
|
.sendToTarget();
|
|
|
|
|
} catch (IOException e2) {
|
|
|
|
|
//insert code to deal with this
|
|
|
|
|
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (fail == false) {
|
|
|
|
|
mConnectedThread = new ConnectedThread(mBTSocket);
|
|
|
|
|
mConnectedThread.start();
|
|
|
|
|
|
|
|
|
|
mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name)
|
|
|
|
|
.sendToTarget();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}.start();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return device.createRfcommSocketToServiceRecord(BTMODULEUUID);
|
|
|
|
|
//creates secure outgoing connection with BT device using UUID
|
|
|
|
|
}
|
|
|
|
|
private class ConnectedThread extends Thread {
|
|
|
|
|
private final BluetoothSocket mmSocket;
|
|
|
|
|
private final InputStream mmInStream;
|
|
|
|
|
private final OutputStream mmOutStream;
|
|
|
|
|
|
|
|
|
|
public ConnectedThread(BluetoothSocket socket) {
|
|
|
|
|
mmSocket = socket;
|
|
|
|
|
InputStream tmpIn = null;
|
|
|
|
|
OutputStream tmpOut = null;
|
|
|
|
|
|
|
|
|
|
// Get the input and output streams, using temp objects because
|
|
|
|
|
// member streams are final
|
|
|
|
|
try {
|
|
|
|
|
tmpIn = socket.getInputStream();
|
|
|
|
|
tmpOut = socket.getOutputStream();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.e("Bluetooth", "Erro ao obter OutputStream", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mmInStream = tmpIn;
|
|
|
|
|
mmOutStream = tmpOut;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
byte[] buffer = new byte[1024]; // buffer store for the stream
|
|
|
|
|
int bytes; // bytes returned from read()
|
|
|
|
|
// Keep listening to the InputStream until an exception occurs
|
|
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
// Read from the InputStream
|
|
|
|
|
bytes = mmInStream.available();
|
|
|
|
|
if (bytes != 0) {
|
|
|
|
|
SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed.
|
|
|
|
|
bytes = mmInStream.available(); // how many bytes are ready to be read?
|
|
|
|
|
bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
|
|
|
|
|
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
|
|
|
|
|
.sendToTarget(); // Send the obtained bytes to the UI activity
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void enviarComando(String input){
|
|
|
|
|
byte[] bytes = input.getBytes(); //converts entered String into bytes
|
|
|
|
|
try {
|
|
|
|
|
mmOutStream.write(bytes);
|
|
|
|
|
} 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();
|
|
|
|
|
mBTSocket = null;
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.e("Bluetooth", "Erro ao desligar", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPause() {
|
|
|
|
|
super.onPause();
|
|
|
|
|
desligarBluetooth();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
super.onResume();
|
|
|
|
|
if ((mBTSocket == null || !mBTSocket.isConnected()) && !bluetoothDenied) {
|
|
|
|
|
ligarBluetooth();
|
|
|
|
|
} catch (IOException e) { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|