Finding Android Bluetooth Paired Devices

前端 未结 3 1813
谎友^
谎友^ 2020-12-19 17:23

I\'m trying to create an image button that, when pressed, presents the users a list of Paired Bluetooth devices to connect to.

However, I get \"Set cannot be resolve

3条回答
  •  梦毁少年i
    2020-12-19 17:58

    Hi, You can also try this code where you just have get the Set of bonded devices.

     private ArrayAdapter bondedAdapter = null;
        private ListView listViewPairedDevices = null;
    
    @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            try {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        listViewPairedDevices = (ListView) findViewById(R.id.listViewPairedDevices);
        bondedAdapter = new ArrayAdapter(this, R.layout.lyt_scanlist_textview);
    
        Set bondedSet = bluetoothAdapter.getBondedDevices();
                    Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet);
    
                    int count = 0;
                    if(bondedSet.size() > 0){
                        for(BluetoothDevice device : bondedSet){
                            textViewPairedDevice.setVisibility(View.VISIBLE);
                            bondedAdapter.add(device.getName()+"\n"+device.getAddress());
                            Log.v(BluetoothDemoActivity.LOG, " count = "+count++);
                        }
                    }else{
                        bondedAdapter.add("No Devices");
                    }
    
        listViewPairedDevices.setAdapter(bondedAdapter);
    } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e(BluetoothDemoActivity.LOG, e.toString(),e.fillInStackTrace());
            }
        }//onStart ends 
    

提交回复
热议问题