diff --git a/app/src/main/java/pt/epvc/lazzycofee/MainActivity.java b/app/src/main/java/pt/epvc/lazzycofee/MainActivity.java index a0e05d1..2ee966e 100644 --- a/app/src/main/java/pt/epvc/lazzycofee/MainActivity.java +++ b/app/src/main/java/pt/epvc/lazzycofee/MainActivity.java @@ -67,53 +67,53 @@ public class MainActivity extends AppCompatActivity { cafeLongo = (ImageButton) findViewById(R.id.cafeLongo); - mBTArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1); +// mBTArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1); mBTAdapter = BluetoothAdapter.getDefaultAdapter(); // get a handle on the bluetooth radio - 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); - } +// 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 (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(); - } - } - }); - - } +// 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(); } @@ -154,7 +154,7 @@ public class MainActivity extends AppCompatActivity { } else { Toast.makeText(MainActivity.this, "Bluetooth is already on", Toast.LENGTH_SHORT).show(); } - //listarAparelhos(); + final String address = "98:D3:21:FC:7F:DF"; new Thread() { public void run() { @@ -219,97 +219,74 @@ public class MainActivity extends AppCompatActivity { //mBluetoothStatus.setText("Disabled"); } } +// public void desligarBluetooth() { +// //mBTAdapter.disable(); // turn off +// //mBluetoothStatus.setText("Bluetooth disabled"); +// Toast.makeText(MainActivity.this, "Bluetooth turned Off", Toast.LENGTH_SHORT).show(); +// } - 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 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) {