java - BroadcastReceveir 不适用于蓝牙
问题描述
我尝试使用蓝牙在 android 中创建简单的应用程序。我在查找设备时遇到问题。
我有相同的代码,就像在教程中一样:https ://www.youtube.com/watch?v=hv_-tX1VwXE&index=3&list= PLgCYzUzKIBE8KHMzpp6JITZ2JxTgWqDH2 运行教程中的这个例子(源代码)。我检查了我的 Android 清单和 MainActivity。我没有更多了。
我的安卓清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.bluetooth_discoverdevices">
<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: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>
我在 MainActivity 中的源代码:
public class MainActivity extends AppCompatActivity {
BluetoothAdapter bluetoothAdapter;
private Button mFind;
private Switch mBluetooth;
public ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();
public DeviceListAdapter mdeviceListAdapter;
ListView lnewDevices;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d("MainActivity", "RUNNING-5");
if (action.equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBTDevices.add(device);
Log.d("MainActivity", "Device " + device.getName());
Log.d("MainActivity", "RUNNING-6");
mdeviceListAdapter = new DeviceListAdapter(context,R.layout.device_list_adapter_view, mBTDevices);
lnewDevices.setAdapter(mdeviceListAdapter);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBluetooth.isChecked())
{
RunBluetooth();}
if (!mBluetooth.isChecked())
{
Log.d("MainActivity","I run off");
}
}
});
mFind = (Button) findViewById(R.id.mFind);
mFind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DiscoverDevice();
}
});
}
public void RunBluetooth()
{
if(!bluetoothAdapter.isEnabled())
{
Log.d("MainActivity", "run-1");
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBTIntent,1);
Log.d("MainActivity", "run-2");
}
if(bluetoothAdapter.isEnabled())
{
DiscoverDevice();
}
}
public void DiscoverDevice(){
Log.d("MainActivity", "RUNNING-1");
if (bluetoothAdapter.isDiscovering()){
Log.d("MainActivity", "RUNNING-2");
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver, discoverDevicesIntent);
}
if (!bluetoothAdapter.isDiscovering())
{
Log.d("MainActivity", "RUNNING-3");
bluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver, discoverDevicesIntent);
Log.d("MainActivity", "RUNNING-4");
}
}}
如您所见 - 我在应用程序中有原始调试器。当我运行应用程序并单击一个按钮时,在我的 Logcat 中是:
05-27 15:22:06.005 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-1
05-27 15:22:06.016 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-3
05-27 15:22:06.025 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-4
我尝试添加这个:
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3, discoverDevicesIntent);
保护 void onCreate 但我只有
05-27 14:22:06.025 9021-9021/com.example.kacper.kaccar D/MainActivity: RUNNING-5
解决方案
在设置 onClickListener 之前,您需要在 onCreate 中初始化 mBluetooth 开关。您应该在 onCreate() 中的 setContentView() 之后注册 Receiver,并在 onDestroy() 中取消注册 Receiver。
推荐阅读
- tcl - 如何在 Expect/Tcl 中发送 ctrl+d?
- exception - Snakemake-歧义规则异常
- javascript - 用于展平多个输入数组的 Javascript 函数
- javascript - 无法使用 Axios 在 Laravel Vue 中放置会话
- python-3.x - 使用 np.random.uniform 生成一批变量的有效方法
- reactjs - 在 React JS Carousel 可重用组件中更改幻灯片时暂停视频?
- power-automate - 在 Microsoft 流程中使用 Freshdesk 自定义连接器
- python - 使用 Flask App 和 sqlalchemy 在 Heroku 中出错
- reactjs - React 组件没有在单元测试中重新渲染获取数据
- kubernetes - 幸存的k8s CronJob jobId重新启动