简体   繁体   English

BroadcastReceiver onRecive 无法正常工作

[英]BroadcastReceiver onRecive does not work correctly

I am trying to write my first Android App.我正在尝试编写我的第一个 Android 应用程序。 I want to display all nearby Bluetooth devises in a listview.我想在列表视图中显示所有附近的蓝牙设备。 But my app is always blank and never shows anything in the List view.但是我的应用程序始终是空白的,并且从不在列表视图中显示任何内容。 With some debugging I figured out, that the BroadcastReciver onRecive never gets called.通过一些调试,我发现 BroadcastReciver onRecive 永远不会被调用。 I read a lot of similar post about this, but nothing seems to work.我阅读了很多关于此的类似帖子,但似乎没有任何效果。 According to the documentation from https://developer.android.com/reference/android/content/BroadcastReceiver it should get called when the BroadcastReceiver is receiving an Intent broadcast.根据https://developer.android.com/reference/android/content/BroadcastReceiver的文档,它应该在 BroadcastReceiver 接收到意图广播时被调用。 But for some reason it doesn't.但由于某种原因,它没有。 I have all necessary permissions in the Manifest.我在清单中拥有所有必要的权限。 I can not find the problem.我找不到问题。 Does anybody have an Idea, why my code does not work?有人有想法吗,为什么我的代码不起作用?

MainActivity.java: 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);
            }
        }
    };
}

Manifest:显现:

<?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: 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 might null. mReceiver 可能是 null。 check initialization;检查初始化; BroadcastReceiver mReceiver = new BroadcastReceiver(), registerReceiver(mReceiver; filter); BroadcastReceiver mReceiver = new BroadcastReceiver(), registerReceiver(mReceiver; filter);

UPDATE: For everyone who has a similar issue: GPS/location needs to be on, on your device, When its off.更新:对于有类似问题的每个人:GPS/位置需要在您的设备上打开,当它关闭时。 it will not show anything in the listview.它不会在列表视图中显示任何内容。

Please turn on GPS and try again.请打开 GPS 并重试。 Please make sure that you ask Location permission to your user.请确保您向用户请求位置权限。

Reason: From Android 6.0 you need Location permission for Bluetooth Discovery.原因:从 Android 6.0 开始,您需要蓝牙发现的位置权限。

More reference:更多参考:

  1. https://developer.android.com/guide/topics/connectivity/bluetooth 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- https://getlief.zendesk.com/hc/en-us/articles/360007600233-Why-does-Android-require-Location-Permissions-for-Bluetooth-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM