0

I want to build an application looking for available devices via Bluetooth has Tact of manifest and I did not show my devices available in ListView I do not know what the problem?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ArrayAdapter<String> item = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1);
        final Switch sw =(Switch) findViewById(R.id.switch1);
        final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        final ListView lv =(ListView)findViewById(R.id.listView);
        final Button bu= (Button)findViewById(R.id.button);

    sw.setChecked(false);
    sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if (!mBluetoothAdapter.isEnabled()&&sw.isChecked())
             {
                 mBluetoothAdapter.enable();

                 Toast.makeText(MainActivity.this, " Bluetooth  enabled", Toast.LENGTH_SHORT).show();
             }
             else if(mBluetoothAdapter.isEnabled()&&!sw.isChecked()) {
                 mBluetoothAdapter.disable();
                 Toast.makeText(MainActivity.this, "Bluetooth disabled", Toast.LENGTH_SHORT).show();
             }
        }
    });

    bu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                      if (!sw.isChecked())
                          item.clear();
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    item.add(device.getName() + "\n" + device.getAddress());

                }


            }
            mBluetoothAdapter.startDiscovery();
            final BroadcastReceiver   ActionFoundReceiver=new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();

                    if (BluetoothDevice.ACTION_FOUND.equals(action))
                    {
                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                        item.add(device.getName()+"\n"+device.getAddress());
                        Toast.makeText(MainActivity.this, device.getName(), Toast.LENGTH_SHORT).show();



                    }
                }
            };
            lv.setAdapter(item);

        }

    });
}

}
reTs
  • 1,808
  • 1
  • 13
  • 26
  • 2
    Possible duplicate of [Programmatically register a broadcast receiver](http://stackoverflow.com/questions/4805269/programmatically-register-a-broadcast-receiver) – Bertrand Martel Aug 04 '16 at 22:50

1 Answers1

0

There is a couple of issues in your code:

  • You have not registered the BroadcastReceiver class.

  • After adding an item to that ArrayAdapter, you should call notifyDataSetChanged method of that ListView. This method call makes the ListView to refresh.

Graham
  • 7,431
  • 18
  • 59
  • 84
frogatto
  • 28,539
  • 11
  • 83
  • 129