java BroadcastReceiver OnReceive无法正常工作
我正在尝试编写我的第一个Android应用程序。我想在列表视图中显示附近的所有蓝牙设备。但我的应用程序总是空的,在列表视图中从不显示任何内容。 经过一些调试,我发现BroadcastReceiver onReceive从未被调用。我读了很多类似的帖子,但似乎什么都不管用。根据来自https://developer.安卓.com/reference/安卓/content/BroadcastReceiver的文档,它应该在广播接收器接收到意图广播时被调用。但出于某种原因,它没有。我在清单上有所有必要的权限。我找不到问题。 有人知道为什么我的代码不起作用吗
主要活动。爪哇:
package com.example.bluetoothdevicelist;
import 安卓.Manifest;
import 安卓.bluetooth.BluetoothAdapter;
import 安卓.bluetooth.BluetoothDevice;
import 安卓.content.BroadcastReceiver;
import 安卓.content.Context;
import 安卓.content.IntentFilter;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.os.Build;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.util.Log;
import 安卓.content.Intent;
import 安卓.widget.AdapterView;
import 安卓.widget.ArrayAdapter;
import 安卓.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, 安卓.R.layout.simple_list_item_1, mDeviceList));
listView.setAdapter(mArrayAdapter);
}
}
};
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.bluetoothdevicelist">
<uses-feature 安卓:name="安卓.hardware.bluetooth" />
<uses-permission 安卓:name="安卓.permission.BLUETOOTH" />
<uses-permission 安卓:name="安卓.permission.BLUETOOTH_ADMIN" />
<uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION" />
<uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION" />
<uses-permission 安卓:name="安卓.permission.BLUETOOTH_PRIVILEGED" />
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:roundIcon="@mipmap/ic_launcher_round"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
梅因。xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.bluetoothdevicelist.MainActivity" >
<ListView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:id="@+id/listView"/>
# 1 楼答案
艾西弗先生可能会无效!检查初始化。 BroadcastReceiver mReceiver=新的BroadcastReceiver(); registerReceiver(mrReceiver,过滤器)
# 2 楼答案
请打开GPS并重试。请确保您向您的用户请求位置许可
原因:在Android 6.0中,蓝牙发现需要位置许可
更多参考资料:
# 3 楼答案
更新:对于所有有类似问题的人:GPS/位置需要在你的设备上打开!关闭时,它不会在列表视图中显示任何内容