蓝牙ACTION_FOUND broadcastReceiver无法正常工作

L. *_*Woo 3 android bluetooth

我正在尝试计算蓝牙RSSI并找到一些例子,但是broadcastReceiver无效.代码是这样的:

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);
            String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
            TextView rssi_msg = (TextView) findViewById(R.id.textView1);
            rssi_msg.setText(rssi_msg.getText() + name + " => " + rssi + "dBm\n");
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

通过此注册:

registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
Run Code Online (Sandbox Code Playgroud)

当点击按钮时,BTAdapter.startDiscovery(); 工作中.但是在textview中没有任何改变.

你能帮我个忙吗?

再次编辑:我更改了我的代码,我将展示我的整个代码.

public class Blutooth extends Activity {

private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blutooth);

    Button boton = (Button) findViewById(R.id.button1);
    boton.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            BTAdapter.startDiscovery();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.blutooth, menu);
    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)

接收器类是

public class TestReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String name = intent.getAction();
    String device = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
    String rssi_msg = Integer.toString(intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE));
    Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
  }

}
Run Code Online (Sandbox Code Playgroud)

我正试着吐司,intent.getAction();但什么都没发生.我认为BluetoothDevice.ACTION_FOUND不是广播.

再次编辑:我正在使用android 6.0版本.而我的全部表现是这样的:

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

    <uses-feature android:name="android.hardware.bluetooth" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-sdk
        android:minSdkVersion="23"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Blutooth"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".TestReceiver">
            <intent-filter>
                <action android:name="BluetoothDevice.ACTION_FOUND">
            </action></intent-filter>
        </receiver> 
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

我听说超过6.0版本需要额外的权限才能使用ACTION_FOUND.所以,我说ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION.我filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);在我的活动中添加了代码. ACTION_NAME_CHANGED工作得很好,我可以得到BluetoothDevice.EXTRA_NAME.但是BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE没有用.它打印默认值-32768.还是BluetoothDevice.ACTION_FOUND不行.

L. *_*Woo 11

我终于找到了答案.Android 6.0需要额外的权限,但只是向Manifest添加使用权限不起作用.您必须检查用户的权限.我写这样的代码:

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION);

        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!bluetoothAdapter.isEnabled())
          bluetoothAdapter.enable();
        leScanner = bluetoothAdapter.getBluetoothLeScanner();

        Button boton = (Button) findViewById(R.id.button1);
        boton.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                leScanner.startScan(scanCallback);
            }
        });
Run Code Online (Sandbox Code Playgroud)

此代码显示您的权限弹出.谢谢你的答案.


小智 9

您需要强制ACCESS_COARSE_LOCATION许可:


if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_CODE);
}

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
...
Run Code Online (Sandbox Code Playgroud)

https://www.sitepoint.com/requesting-runtime-permissions-in-android-m-and-n中找到


小智 5

我通过物理方式打开设备上的位置服务来实现此功能,或者您可以通过编程方式完成此操作。

只需在运行应用程序之前将其打开,您就可以看到设备列表。

如果位置服务关闭,蓝牙 API 将无法工作。