2016-11-18 81 views
1

我寫這段代碼來學習如何在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 } 沒有找到處理意圖的活動{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

你到底爲什麼試圖達到爲什麼接收者和活動的名字與'MainActivity'相同? –

+0

接收者的名字是否會接收到意圖的類? –

+0

添加關於接收機和活動的確切內容的詳細信息 –

回答

1

你好,你還沒有跟那種事件處理的任何活動, 你應該努力使廣播接收器這樣

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); 
+0

我需要一個BrodcastReceiver,因爲我在AndroidManifest.xml中使用?以前我只在MainActivity內部有一個intent過濾器,就像這個' '但它沒有工作,所以我然後嘗試接收器。我會嘗試你的解決方案。我想知道爲什麼不在MainActivity工作中添加意圖過濾器。我發送了一個意圖,並宣佈MainActivity能夠處理該意圖。爲什麼這不起作用? –

+0

你需要的是接收器不是一項活動。嘗試如果我的解決方案適合你。 – AJay

+0

它確實有效。我做得有點不同。我宣佈了一個擴展BroadcastReceiver並在其中添加代碼的類。然後在AndroidManifest文件中,我添加了以及擴展BroadcastReceiver的類的名稱。我不需要Java代碼中的IntentFilter的最後部分。如果有人認爲這是不好的做法,讓我知道,但現在所有的作品。 –

相關問題