Android设备之间的蓝牙连接问题

时间:2015-05-15 15:02:32

标签: android bluetooth android-bluetooth

我刚开始使用Android,但今年参加考试我决定创建一个Android应用程序,它使用蓝牙来控制机器人机器。我能够激活蓝牙,显示相关设备并发现新设备。但是现在我在设备连接方面遇到了困难。

这是发现的代码:

      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();
                        }*/ 
                    }
                }); 
            }
        }
    };

虽然这是用于启动与所选蓝牙设备连接的线程:

    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();
        }

当我按下“发现”按钮时,我收到此错误:

ZygoteInit$MethodandArgsCaller.run() line 1394

如果有人能帮助我找到解决方案或其他方式将从列表视图中选择的蓝牙设备传递给线程,我将非常高兴。

0 个答案:

没有答案