简体   繁体   中英

Im doing a project which continuously gives me RSSI of a previously paired device. Im not getting continuous results

The project is supposed to continuously give me the RSSI of a device whose MAC Address is already known. I get the RSSI about 2-3 times, and then nothing. Even though Im testing this device on a android phone, i can bypass battery restrictions, etc that google throws at me. This project's target device will be a rooted Android M device.

This is my code:

 private final BroadcastReceiver broadcastReceiver=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);
                String deviceName=device.getName();
                String deviceHardwareAddress=device.getAddress();
                int rssi=intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
                if(deviceHardwareAddress.equals(MAC)){
                    deviceFound=true;
                    bluetoothAdapter.cancelDiscovery();
                    Log.i("broadcastReceiver" , "device " + deviceName);
                    Log.i("broadcastReceiver" , "hard"  + deviceHardwareAddress);
                    Log.i("broadcastReceiver","Rssi "+rssi);
                    Log.i("broadcastReceiver","Paired Id "+MAC );
                    tv.setText(deviceName+"     "+rssi);
                    stringArrayList.add(String.valueOf(rssi));
                    arrayAdapter.notifyDataSetChanged();
                    deviceFound=false;
                    bluetoothAdapter.startDiscovery();
                }
                if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                    if(deviceFound==false){
                        bluetoothAdapter.startDiscovery();
                    }
                }
            }

        }
    };

Solved it!

Just Splitting the BroadcastReceiver into 2 parts worked. Please don't copy this answer without understanding what happens. This code will drain a lot of battery. My project allows that. It ll have something like a continuous power supply. And it runs only in some conditions. Cheers!

  private final BroadcastReceiver broadcastReceiver1=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action=intent.getAction();
            if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                if(deviceFound==false) {
                    bluetoothAdapter.cancelDiscovery();
                    bluetoothAdapter.startDiscovery();
                }
            };
        }
    };
private final BroadcastReceiver broadcastReceiver=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);
                String deviceName=device.getName();
                String deviceHardwareAddress=device.getAddress();
                int rssi=intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
                if(deviceHardwareAddress.equals(MAC)){
                    deviceFound=true;
                    bluetoothAdapter.cancelDiscovery();
                    Log.i("broadcastReceiver" , "device " + deviceName);
                    Log.i("broadcastReceiver" , "hard"  + deviceHardwareAddress);
                    Log.i("broadcastReceiver","Rssi "+rssi);
                    Log.i("broadcastReceiver","Paired Id "+MAC );
                    tv.setText(deviceName+"     "+rssi);
                    deviceFound=false;
                    bluetoothAdapter.startDiscovery();
                }
            }

        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM