1

I have a button and a TextView in my layout.

I am trying to get signal strength value of a BLE device, the goal is to find where is the desired devices without need to be connected, and for this I need to get the RSSI of all devices near me. I wrote the following code based on other posts.

The problem is that my device never enters the ScanCallback function, and I don't know why. Can someone give a guide on how to solve the problem? Thanks!

import ...

public class MainActivity extends AppCompatActivity {

    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothLeScanner mBluetoothLeScanner;
    private ScanCallback mScanCallback;
    private Button mButton;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = findViewById(R.id.button1);
        mTextView = findViewById(R.id.textView1);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

        mScanCallback = new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                super.onScanResult(callbackType, result);
                int rssi = result.getRssi();
                mTextView.setText("RSSI: " + rssi);
            }
        };

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {return;}
                mBluetoothLeScanner.startScan(mScanCallback);
            }
        });

    }
}
  • Welcome to Stack Overflow. Does your application have the necessary permissions? – Risto Oct 24 '22 at 06:49
  • Thanks @Risto. I think so, I copied the permissions from the [official website](https://developer.android.com/guide/topics/connectivity/bluetooth/permissions) to put them in the AndroidManifest.xml. – Marc Ledesma Chivite Oct 24 '22 at 19:51
  • late to the party but, your response here appears quite ambiguous. If all you did was "copy the permissions" and put them "in the AndroidManifest", then you didn't follow the complete instructions from the page you refer to. For in runtime, you must request the permissions from the user as well. – axa Jan 27 '23 at 15:04
  • At least in the latter versions of android – axa Jan 27 '23 at 15:18

0 Answers0