Reputation: 421
I'am writing this code to learn how use Bluetooth in Android however i'am constantly getting the error mentioned in the title. Full error is: 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");
}
}
On the device i have bluetooth on and with the logs i saw that it did skip the isEnabled() test(to see of Bluetooth is currently enabled and ready for use). It was able to create an intent to observe Bluetooth status but it crashes on startActivityForResult(). What did i do wrong?
Upvotes: 1
Views: 1664
Reputation: 1193
Hello you have not any activity with that kind of event handling, you should try to make BroadcastReceiver like this
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;
}
}
}
};
And in your activity do it like this...
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);
Upvotes: 1