我正在编写此代码以了解如何在 Android 中使用蓝牙,但是我不断收到标题中提到的错误。完整错误是:java.lang.RuntimeException: 无法启动 Activity ComponentInfo{com.example.android/com.example.android.MainActivity}: android.content.ActivityNotFoundException: 找不到处理 Intent { act=android 的 Activity .bluetooth.adapter.action.STATE_CHANGED }
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<receiver
android:name=".MainActivity" >
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主要 Activity .java:
package com.example.android;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity
{
private final String LOG_TAG = getClass().getSimpleName();
private static int REQUEST_ENABLE_BT = 1;
private static int REQUEST_STATE_CHANGED_BT = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BluetoothAdapter oBTAdapter = BluetoothAdapter.getDefaultAdapter();
if (oBTAdapter == null) Log.i(LOG_TAG, "This device does not support Bluetooth");
if (!oBTAdapter.isEnabled()) {
Intent iEnableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(iEnableBT, REQUEST_ENABLE_BT);
}
Intent iBTStatus = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
startActivityForResult(iBTStatus, REQUEST_STATE_CHANGED_BT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT)
if (resultCode == RESULT_CANCELED) Log.e(LOG_TAG, "Bluetooth was not enabled on the device");
else if (requestCode == REQUEST_STATE_CHANGED_BT)
if (resultCode == RESULT_OK && !data.getStringExtra(BluetoothAdapter.EXTRA_STATE).equals(BluetoothAdapter.STATE_ON)) Log.e(LOG_TAG, "Bluetooth adapter is not on");
}
}
在我打开蓝牙的设备上,通过日志我看到它确实跳过了 isEnabled() 测试(查看蓝牙当前是否已启用并可以使用)。它能够创建一个 Intent 来观察蓝牙状态,但它在 startActivityForResult() 上崩溃。我做错了什么?
最佳答案
您好,您没有任何此类事件处理 Activity , 你应该尝试像这样制作 BroadcastReceiver
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
setButtonText("Bluetooth off");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
setButtonText("Turning Bluetooth off...");
break;
case BluetoothAdapter.STATE_ON:
setButtonText("Bluetooth on");
break;
case BluetoothAdapter.STATE_TURNING_ON:
setButtonText("Turning Bluetooth on...");
break;
}
}
}
};
在你的 Activity 中这样做......
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);
关于android - 找不到处理 Intent { act=android.bluetooth.adapter.action.STATE_CHANGED } 的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40682758/