Remoção de código inútil
parent
ee6e2cd2fb
commit
114c86b480
|
|
@ -67,53 +67,53 @@ public class MainActivity extends AppCompatActivity {
|
||||||
cafeLongo = (ImageButton) findViewById(R.id.cafeLongo);
|
cafeLongo = (ImageButton) findViewById(R.id.cafeLongo);
|
||||||
|
|
||||||
|
|
||||||
mBTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
|
// mBTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
|
||||||
mBTAdapter = BluetoothAdapter.getDefaultAdapter(); // get a handle on the bluetooth radio
|
mBTAdapter = BluetoothAdapter.getDefaultAdapter(); // get a handle on the bluetooth radio
|
||||||
|
|
||||||
|
|
||||||
mHandler = new Handler() {
|
// mHandler = new Handler() {
|
||||||
public void handleMessage(android.os.Message msg) {
|
// public void handleMessage(android.os.Message msg) {
|
||||||
if (msg.what == MESSAGE_READ) {
|
// if (msg.what == MESSAGE_READ) {
|
||||||
String readMessage = null;
|
// String readMessage = null;
|
||||||
try {
|
// try {
|
||||||
readMessage = new String((byte[]) msg.obj, "UTF-8");
|
// readMessage = new String((byte[]) msg.obj, "UTF-8");
|
||||||
} catch (UnsupportedEncodingException e) {
|
// } catch (UnsupportedEncodingException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
//mReadBuffer.setText(readMessage);
|
// //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 (mBTArrayAdapter == null) {
|
||||||
if (msg.arg1 == 1)
|
// // Device does not support Bluetooth
|
||||||
Toast.makeText(MainActivity.this, "Connected to Device: " + (String) (msg.obj), Toast.LENGTH_SHORT).show();
|
// //mBluetoothStatus.setText("Status: Bluetooth not found");
|
||||||
//mBluetoothStatus.setText("Connected to Device: " + (String)(msg.obj));
|
// Toast.makeText(MainActivity.this, "Bluetooth device not found!", Toast.LENGTH_SHORT).show();
|
||||||
else
|
// } else {
|
||||||
Toast.makeText(MainActivity.this, "Connection Failed", Toast.LENGTH_SHORT).show();
|
// conetar = (Switch) findViewById(R.id.conetar);
|
||||||
//mBluetoothStatus.setText("Connection Failed");
|
// 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();
|
||||||
if (mBTArrayAdapter == null) {
|
// ligarBluetooth();
|
||||||
// Device does not support Bluetooth
|
// } else {
|
||||||
//mBluetoothStatus.setText("Status: Bluetooth not found");
|
// Toast.makeText(MainActivity.this, "Desconetado", Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(MainActivity.this, "Bluetooth device not found!", Toast.LENGTH_SHORT).show();
|
// desligarBluetooth();
|
||||||
} 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);
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||||
ligarBluetooth();
|
ligarBluetooth();
|
||||||
}
|
}
|
||||||
|
|
@ -154,7 +154,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(MainActivity.this, "Bluetooth is already on", Toast.LENGTH_SHORT).show();
|
Toast.makeText(MainActivity.this, "Bluetooth is already on", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
//listarAparelhos();
|
|
||||||
final String address = "98:D3:21:FC:7F:DF";
|
final String address = "98:D3:21:FC:7F:DF";
|
||||||
new Thread() {
|
new Thread() {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
@ -219,97 +219,74 @@ public class MainActivity extends AppCompatActivity {
|
||||||
//mBluetoothStatus.setText("Disabled");
|
//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() {
|
// private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
|
||||||
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
// public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
|
||||||
// TODO: Consider calling
|
//
|
||||||
// ActivityCompat#requestPermissions
|
// if (!mBTAdapter.isEnabled()) {
|
||||||
// here to request the missing permissions, and then overriding
|
// Toast.makeText(getBaseContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show();
|
||||||
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
// return;
|
||||||
// int[] grantResults)
|
// }
|
||||||
// to handle the case where the user grants the permission. See the documentation
|
// Toast.makeText(MainActivity.this, "Connecting...", Toast.LENGTH_SHORT).show();
|
||||||
// for ActivityCompat#requestPermissions for more details.
|
// //mBluetoothStatus.setText("Connecting...");
|
||||||
return;
|
// // Get the device MAC address, which is the last 17 chars in the View
|
||||||
}
|
// String info = ((TextView) v).getText().toString();
|
||||||
mPairedDevices = mBTAdapter.getBondedDevices();
|
// final String address = info.substring(info.length() - 17);
|
||||||
if (mBTAdapter.isEnabled()) {
|
// final String name = info.substring(0, info.length() - 17);
|
||||||
// put it's one to the adapter
|
//
|
||||||
for (BluetoothDevice device : mPairedDevices)
|
// // Spawn a new thread to avoid blocking the GUI one
|
||||||
mBTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
|
// new Thread() {
|
||||||
|
// public void run() {
|
||||||
Toast.makeText(getApplicationContext(), "Show Paired Devices", Toast.LENGTH_SHORT).show();
|
// boolean fail = false;
|
||||||
} else
|
//
|
||||||
Toast.makeText(getApplicationContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show();
|
// BluetoothDevice device = mBTAdapter.getRemoteDevice(address);
|
||||||
}
|
//
|
||||||
|
// try {
|
||||||
public void desligarBluetooth() {
|
// mBTSocket = createBluetoothSocket(device);
|
||||||
//mBTAdapter.disable(); // turn off
|
// } catch (IOException e) {
|
||||||
//mBluetoothStatus.setText("Bluetooth disabled");
|
// fail = true;
|
||||||
Toast.makeText(MainActivity.this, "Bluetooth turned Off", Toast.LENGTH_SHORT).show();
|
// Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
||||||
}
|
// }
|
||||||
|
// // Establish the Bluetooth socket connection.
|
||||||
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
|
// try {
|
||||||
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
|
// if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
// // TODO: Consider calling
|
||||||
if (!mBTAdapter.isEnabled()) {
|
// // ActivityCompat#requestPermissions
|
||||||
Toast.makeText(getBaseContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show();
|
// // here to request the missing permissions, and then overriding
|
||||||
return;
|
// // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
||||||
}
|
// // int[] grantResults)
|
||||||
Toast.makeText(MainActivity.this, "Connecting...", Toast.LENGTH_SHORT).show();
|
// // to handle the case where the user grants the permission. See the documentation
|
||||||
//mBluetoothStatus.setText("Connecting...");
|
// // for ActivityCompat#requestPermissions for more details.
|
||||||
// Get the device MAC address, which is the last 17 chars in the View
|
// return;
|
||||||
String info = ((TextView) v).getText().toString();
|
// }
|
||||||
final String address = info.substring(info.length() - 17);
|
// mBTSocket.connect();
|
||||||
final String name = info.substring(0, info.length() - 17);
|
// } catch (IOException e) {
|
||||||
|
// try {
|
||||||
// Spawn a new thread to avoid blocking the GUI one
|
// fail = true;
|
||||||
new Thread() {
|
// mBTSocket.close();
|
||||||
public void run() {
|
// mHandler.obtainMessage(CONNECTING_STATUS, -1, -1)
|
||||||
boolean fail = false;
|
// .sendToTarget();
|
||||||
|
// } catch (IOException e2) {
|
||||||
BluetoothDevice device = mBTAdapter.getRemoteDevice(address);
|
// //insert code to deal with this
|
||||||
|
// Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
||||||
try {
|
// }
|
||||||
mBTSocket = createBluetoothSocket(device);
|
// }
|
||||||
} catch (IOException e) {
|
// if (fail == false) {
|
||||||
fail = true;
|
// mConnectedThread = new ConnectedThread(mBTSocket);
|
||||||
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
|
// mConnectedThread.start();
|
||||||
}
|
//
|
||||||
// Establish the Bluetooth socket connection.
|
// mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name)
|
||||||
try {
|
// .sendToTarget();
|
||||||
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
// }
|
||||||
// TODO: Consider calling
|
// }
|
||||||
// ActivityCompat#requestPermissions
|
// }.start();
|
||||||
// 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 {
|
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
|
||||||
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue