Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the address and names of all available bluetooth devices in android

I am able to retrieve the paired device names and address, but I don't know how I can retrieve the available bluetooth devices name and address?

   String outV="";
   b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    tv=(TextView)findViewById(R.id.textView1);
    b1.setOnClickListener(new View.OnClickListener() {

        BluetoothAdapter Badap = BluetoothAdapter.getDefaultAdapter();



        @Override
        public void onClick(View arg0) {




            Set<BluetoothDevice> pairedDevices= Badap.getBondedDevices();

            if (pairedDevices.size() > 0) {

                for (BluetoothDevice device : pairedDevices) {

                    outV+= device.getName() + " ::" + device.getAddress()+"\n";


                }
                tv.setText(outV);







            }

this is the code for paired device..

like image 293
Anandhu Avatar asked Mar 22 '23 20:03

Anandhu


1 Answers

In my OnCreate I'm doing

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(MydeviceReceiver, filter);  

Where MydeviceReceiver is

    private final BroadcastReceiver deviceReceiver = new BroadcastReceiver() 
        {
            @Override
            public void onReceive(Context context, Intent intent) 
            {
                String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) 
            {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                if( !PairedDeviceNames.contains(device) && !newDevices.contains(device))
                    newDevices.add(device);                
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) 
            {
                Log.v(" ","discovery Finished ");
                if(newDevices.size() != 0)
                {                   
                    deviceList.invalidateViews();
                    sectionAdapter.notifyDataSetChanged();
                }
                else
                {
                    Toast.makeText(YourActivity.this, "No New Devices Found", Toast.LENGTH_LONG).show();
                }
            }
        }
    };
like image 168
SSS Avatar answered Apr 25 '23 19:04

SSS