BroadcastReceiver onRecive 无法正常工作

BroadcastReceiver onRecive does not work correctly

我正在尝试编写我的第一个 Android 应用程序。我想在列表视图中显示所有附近的蓝牙设备。但是我的应用程序始终是空白的,并且从不在列表视图中显示任何内容。 通过一些调试,我发现 BroadcastReciver onRecive 永远不会被调用。我读了很多关于此的类似 post,但似乎没有任何效果。根据 https://developer.android.com/reference/android/content/BroadcastReceiver 的文档,它应该在 BroadcastReceiver 接收 Intent 广播时被调用。但出于某种原因,它没有。我在清单中拥有所有必要的权限。我找不到问题所在。 有没有人有想法,为什么我的代码不起作用?

MainActivity.java:

package com.example.bluetoothdevicelist;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Build;
import android.view.Menu;
import android.view.View;
import android.util.Log;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Set;


public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<String> mDeviceList = new ArrayList<String>();
    private BluetoothAdapter mBluetoothAdapter;
    private ArrayAdapter mArrayAdapter;
    private BluetoothDevice bt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("BT", "start!");
        listView = (ListView) findViewById(R.id.listView);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);

        //Turn Bluetooth on
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBTIntent);
        }
        mBluetoothAdapter.startDiscovery();
    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }


    public BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d("BT", "I am here!");
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mDeviceList.add(device.getName() + "\n" + device.getAddress());
                mDeviceList.add("It should work");
                Log.d("BT", device.getName() + "\n" + device.getAddress());

                mArrayAdapter = (new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, mDeviceList));
                listView.setAdapter(mArrayAdapter);
            }
        }
    };
}

清单:

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

    <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_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.bluetoothdevicelist.MainActivity" >

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"/>

mReceiver 可能为空!检查初始化。 BroadcastReceiver mReceiver = new BroadcastReceiver(); registerReceiver(mReceiver, 过滤器);

更新:对于遇到类似问题的每个人:GPS/location 需要在您的设备上打开!当它关闭时,它不会在列表视图中显示任何内容。

请打开 GPS 并重试。请确保您向您的用户请求位置权限。

原因: 从 Android 6.0 开始,您需要获得蓝牙发现的位置权限。

更多参考:

  1. https://developer.android.com/guide/topics/connectivity/bluetooth
  2. https://getlief.zendesk.com/hc/en-us/articles/360007600233-Why-does-Android-require-Location-Permissions-for-Bluetooth-