BroadcastReceiver onRecive无法正常工作

问题描述 投票:0回答:3

我正在尝试编写我的第一个Android应用。我想在列表视图中显示所有附近的蓝牙设备。但是我的应用程序始终为空白,并且在“列表”视图中从未显示任何内容。通过一些调试,我弄清楚了BroadcastReciver onRecive永远不会被调用。我读过很多类似的文章,但是似乎没有任何效果。根据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"/>

java android android-studio broadcastreceiver android-studio-3.0
3个回答
0
投票

请打开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-

0
投票

mReceiver可能为空!检查初始化。BroadcastReceiver mReceiver =新的BroadcastReceiver();registerReceiver(mReceiver,filter);


0
投票

更新:对于有类似问题的每个人:您的设备上的GPS /定位功能必须打开!禁用时,它不会在列表视图中显示任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.