Johnaudi
Johnaudi

Reputation: 257

Android get RSSI of specific device (or the whole list!)

I've seen some apps on the playstore that can read the RSSI of all devices in range via Bluetooth, how is that possible? I'm currently running a code that can get me one and only one RSSI (which is the nearest one available).

Any possible way to get the RSSI of a specific device? Or even better, get the RSSI of all the devices in range?

I'm using the EXTRA_RSSI code at the moment, but I need to get the RSSI of the whole list or a specific device.

Here's the method I'm using (it's working fine, but again, only the nearest is getting capted)

private final BroadcastReceiver receiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            //BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            //Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

            //for (BluetoothDevice dv : pairedDevices) {

                String action = intent.getAction();
                if(BluetoothDevice.ACTION_FOUND.equals(action)) {
                    int rssit = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
                    int rssi = Math.abs(rssit); // Just for visual analysis
                    String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);

                    if (!l_name.contains(name)) {
                        RadioButton Rb = new RadioButton(getBaseContext());
                        Rb.setId(l_name.size());
                        Rb.setText(name);
                        Rb.setTypeface(Typeface.DEFAULT_BOLD, R.style.TextAppearance_AppCompat_Widget_DropDownItem);
                        Rb.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                s_name = ((RadioButton) view).getText().toString();
                            }
                        });

                        l_name.add(name);
                        ((RadioGroup) findViewById(R.id.radiogroup)).addView(Rb);
                    }

                    //if (s_name != name) return;

                    String RSSI = "";
                    ((TextView) findViewById(R.id.textView)).setText(name);
                    ((TextView) findViewById(R.id.range)).setText(RSSI + " " + rssit);
                }
            //}
        }
    };

Upvotes: 0

Views: 1178

Answers (1)

Doug Koellmer
Doug Koellmer

Reputation: 437

Your onReceive() callback should be getting called for every nearby advertising device. If you do BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); then you should see that you're actually getting different device instances for each call to onReceive(). Eventually they will start to repeat, but if there are 3 advertising devices nearby you should get at least 3 callbacks. Log BluetoothDevice#getAddress() to confirm the behavior.

Upvotes: 0

Related Questions