Bluetooth connection issues between Android devices
I just started using Android, but this year for exams, I decided to create an Android app that uses Bluetooth to control the robot. I was able to activate bluetooth, show connected devices and open new devices. But now I am having difficulty connecting to devices.
This is the detection code:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { //tramite il BroadcastReceiver mi metto in ascolto
@SuppressWarnings({ "unchecked", "rawtypes" })
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
ArrayList lista = new ArrayList();
// Quando un il bluetooth trova un dispositivo
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Ottengo i dispositivi
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Devices.add(device);
// Aggiungo il nome e l'indirizzo ad un arrayAdapter che verrà mostrato nella listview
lista.add("Discovered: " + device.getName() + "\n" + device.getAddress());
Toast.makeText(getApplicationContext(),"Showing New Devices",
Toast.LENGTH_SHORT).show();
adapt = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, lista);
lsv.setAdapter(adapt);
lsv.setOnItemClickListener(new AdapterView.OnItemClickListener() { // registro una sentinella per monitorare il click/tocco sulla lista. Item indica la riga della lista
@Override
public void onItemClick(AdapterView<?> adattore,View view, int position, long id) { //qui stabilisco che fare dopo il click sulla riga
// TODO Auto-generated method stub
final String titoloriga = (String)adattore.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),"Hai cliccato su " + titoloriga,
Toast.LENGTH_SHORT).show();
BluetoothDevice selectedDevice = Devices.get(position);
ConnectThread connect = new ConnectThread(selectedDevice);
connect.start();
/*try {
Class controlD = Class.forName("com.roborcarcontroller.ListDevices");
Intent startCommand = new Intent(MainActivity.this , controlD);
startActivity(startCommand);
} catch (ClassNotFoundException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}*/
}
});
}
}
};
While this is the stream used to start the connection with the selected Bluetooth device:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
//manageConnectedSocket(mmSocket);
mHandler.obtainMessage(SUCCESS_CONNECT,mmSocket).sendToTarget();
}
When I click on the "Discover" button, I get this error:
ZygoteInit$MethodandArgsCaller.run() line 1394
I would be very glad if someone could help me find a solution or some other way to transfer the Bluetooth device selected from the list to Thread.
+3
source to share
No one has answered this question yet
See similar questions:
or similar: