我正在编写此代码以了解如何在Android中使用蓝牙,但我不断收到标题中提到的错误。完整错误是:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.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>
MainActivity.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()测试(看到蓝牙目前已启用并可以使用)。它能够创建一个观察蓝牙状态的意图,但它在startActivityForResult()上崩溃了。我做错了什么?
答案 0 :(得分:1)
您好,您没有任何关于此类事件处理的活动, 你应该尝试像这样制作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;
}
}
}
};
在你的活动中这样做......
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);