如何获得蓝牙的RSSI

时间:2020-05-18 19:15:15

标签: android bluetooth rssi

我正在尝试获取蓝牙连接的RSSI。首先,我想显示所有附近设备的RSSI,因此我检查了互联网-获得了一些有关蓝牙类别的信息并检查了一些示例。我尝试了很多事情,但没有成功。我目前还无法理解问题出在哪里。

目标SDK为29,最小SDK为19。我正在Android 10(SDK 29)上对其进行测试

MainActivity.java

package com.example.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BlendMode;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;

import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Set;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

BluetoothAdapter BA;

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

    registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

        BA = BluetoothAdapter.getDefaultAdapter();
        BA.startDiscovery();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.settings:
                Intent intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    private final BroadcastReceiver receiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)) {
                int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
                TextView rssi_msg = (TextView) findViewById(R.id.textView1);
                rssi_msg.setText(rssi_msg.getText() + "" + rssi + "dBm\n");
            }
        }
    };

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main2Activity"></activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".MainActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我找到了“解决方法”,但仍然无法正确执行。因此,对我有帮助,现在就可以使用的是:我将 targetSdkVersion 设置为 26 (之前为29), minSdkVersion 仍然 19 (像以前一样)和 compileSdkVersion 仍然是 29 (像以前一样)。这是在摇摇欲坠。更改目标SDK之后,将其添加到了 MainActivity

上的 onCreate()方法中
if (ActivityCompat.checkSelfPermission((Activity)this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions((Activity)this, new String[]{
                android.Manifest.permission.ACCESS_COARSE_LOCATION
        }, 10);
    }

现在它可以在android 10上运行,但是我仍然无法在目标SDK 29上实现