Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not find all bluetooth devices android

I am trying to display bluetooth devices continuously and showing them on screen but It never show me all the devices instead only 1 at a time. I can not find what I am doing wrong. here is my code, may be you can find any issue in it. Thanks

class monitorBluetooth extends monitor {
private ListView mLvDevices;
private ArrayList<String> mDeviceList = new ArrayList<String>();

public monitorBluetooth(service service) {

super(service);
    bluetooth = BluetoothAdapter.getDefaultAdapter();

this.bReceiver = new  BluetoothReceiver();
}

public void finalize() throws Throwable {

super.finalize();
}

public void run() {        

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    service.registerReceiver(this.bReceiver, filter);

if(service != null) {
        bluetooth = BluetoothAdapter.getDefaultAdapter();
        bluetooth.startDiscovery();
 }       
}
class BluetoothReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
    Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();

    String action = intent.getAction();

    if(BluetoothDevice.ACTION_FOUND.equals(action)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String uuid = intent.getStringExtra(BluetoothDevice.EXTRA_UUID);
    int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
     mDeviceList.add(device.getAddress() + ", " + device.getName()); // get mac address

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mDeviceList);
            mLvDevices.setAdapter(adapter);
    }

    }

}
}

BluetoothAdapter bluetooth;
private BluetoothReceiver bReceiver;
like image 735
JNI_OnLoad Avatar asked Oct 31 '22 16:10

JNI_OnLoad


1 Answers

Try something of this order:

public void displayDetectedDevices(){
    mBluetoothAdapter.startDiscovery();

    // Create a BroadcastReceiver for ACTION_FOUND
    mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceAddress = device.getAddress();
                String s = " ";
                unpairedDevices.add(deviceName + s + deviceAddress +" \n");
                unpairedDevicesList = new ArrayList<String>(unpairedDevices);
                Toast.makeText(getActivity(), unpairedDevicesList.toString(), Toast.LENGTH_LONG).show();
            }
        }
    };
}

Remember in onCreate() declare the following:

public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    unpairedDevicesList = new ArrayList<String>();
    unpairedDevices = new HashSet<String>();
}

Remember #2 declare the following before onCreate():

ArrayList<String> unpairedDevicesList;
Set<String> unpairedDevices;

Final Thoughts:

I used both a Set and a arrayList to place the detected devices in. A Set will contain no duplicates. Depending on what you want to do having both defined is helpful as they provide different functions. Also here is my github page it contains more info and a full working bluetooth app.

As the set populates the devices will start populating the screen depending on how you want your UI to work. I used a simple list.

like image 200
SeahawksRdaBest Avatar answered Nov 08 '22 03:11

SeahawksRdaBest