3

I have just started to use Android but this year for the exams I decided to create an Android application, which uses Bluetooth to control a robot machine. I was able to activate the bluetooth, display associated devices and discover new devices. But now I have difficulties with regard to the connection to devices.

This is the code of the discovery:

      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 Thread 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's 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 press the button "Discover" I get this error:

ZygoteInit$MethodandArgsCaller.run() line 1394

I would be very happy if someone could help me to find a solution or another way to pass a bluetooth device selected from the listview to the Thread.

iSaMx
  • 31
  • 2

0 Answers0