2014-02-05 74 views
0

我想要發現藍牙設備,由於某種原因,我不斷收到接收機從未註冊的非法異常。BroadcastReceiver沒有設置

我能夠在碎片上發現設備。這使得UI在發現過程完成之前等待發現過程完成,因此我嘗試將此代碼移至AsyncTask。同樣的問題。我不知道我哪裏出錯了。歡迎任何幫助/建議。

public class HomeFragment extends Fragment{ 

private View rootView; 
private ToggleButton toggleButton; 
private TextView lockStatus; 
private TextView connectionToLock; 
private ArrayList<BluetoothDevice> availableDevices; 
private BluetoothAdapter bluetoothAdapter; 


public HomeFragment(){}; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    rootView = inflater.inflate(R.layout.fragment_home, container, false); 
    toggleButton = (ToggleButton) rootView.findViewById(R.id.toggleLock); 
    lockStatus = (TextView) rootView.findViewById(R.id.lockStatus); 
    connectionToLock = (TextView)rootView.findViewById(R.id.connectionToLock); 
    availableDevices = new ArrayList<BluetoothDevice>(); 

    lockStatus.setText("Lock Status: DEFAULT"); 
    connectionToLock.setText("Searching for BT Devices ..."); 

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    btDeviceDiscovery(); 
    return rootView; 
} 

private void btDeviceDiscovery(){ 
    //assume bt is turned on for now 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    filter.addAction(BluetoothDevice.ACTION_UUID); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 

    getActivity().getApplicationContext().registerReceiver(ActionFoundReceiver, filter); 
    Log.i("BT Discovery", "Devices found: "+availableDevices.size()); 
} 

@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    try { 
     getActivity().unregisterReceiver(ActionFoundReceiver); 
    } catch (Exception e) { 
     Log.i("onDestroy", e.toString()); 
     e.printStackTrace(); 
    } 
} 

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i("onReceive", "Finally Called"); 
     String action = intent.getAction(); 
     connectionToLock.setText("Found Devices :- \n"); 

     if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      Log.i("onReceive", "\n Device: " + device.getName() + ", " + device); 
      availableDevices.add(device); 
      connectionToLock.append(device.getName()+ " \n"); 
     } else { 
      connectionToLock.setText("No Bluetooth Devices in range."); 
     } 

    } 

}; 

}

而且堆棧跟蹤:

02-05 15:17:45.227: W/System.err(11086): java.lang.IllegalArgumentException: Receiver not registered: [email protected] 
02-05 15:17:45.227: W/System.err(11086): at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:667) 
02-05 15:17:45.227: W/System.err(11086): at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1743) 
02-05 15:17:45.227: W/System.err(11086): at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:475) 
02-05 15:17:45.227: W/System.err(11086): at com.naftal.easylock.ui.fragment.HomeFragment.onDestroy(HomeFragment.java:69) 
02-05 15:17:45.227: W/System.err(11086): at android.app.Fragment.performDestroy(Fragment.java:1908) 
02-05 15:17:45.237: W/System.err(11086): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026) 
02-05 15:17:45.237: W/System.err(11086): at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1180) 
02-05 15:17:45.237: W/System.err(11086): at android.app.BackStackRecord.run(BackStackRecord.java:639) 
02-05 15:17:45.237: W/System.err(11086): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455) 
02-05 15:17:45.237: W/System.err(11086): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 
02-05 15:17:45.237: W/System.err(11086): at android.os.Handler.handleCallback(Handler.java:730) 
02-05 15:17:45.237: W/System.err(11086): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-05 15:17:45.237: W/System.err(11086): at android.os.Looper.loop(Looper.java:137) 
02-05 15:17:45.237: W/System.err(11086): at android.app.ActivityThread.main(ActivityThread.java:5455) 
02-05 15:17:45.237: W/System.err(11086): at java.lang.reflect.Method.invokeNative(Native Method) 
02-05 15:17:45.237: W/System.err(11086): at java.lang.reflect.Method.invoke(Method.java:525) 
02-05 15:17:45.237: W/System.err(11086): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
02-05 15:17:45.237: W/System.err(11086): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
02-05 15:17:45.247: W/System.err(11086): at dalvik.system.NativeStart.main(Native Method) 
+0

堆棧跟蹤在哪裏? – nKn

+0

增加了Stacktrace。它只是告訴我接收器從來沒有註冊;但是我願意。我不能解釋爲什麼它這樣做。 – 1000Suns

回答

1

我的猜測是,你做你的BroadcastReceiver註冊的Intent之前接收。您可以調用註冊接收方的方法,但在這段時間內,直到註冊過程中,接收方可能會收到一個Intent,但尚未註冊。

---- ----編輯

還有另一個想法:你可能會調用unregisterReceiver()時,它是未經註冊已經或基本在您註冊接收器之前你onDestroy()方法被調用。所以它可能基於處理你的片段,嘗試調試什麼時候被破壞,然後調試你之前是否調用過registerReceiver()

+0

那麼你會建議我做什麼?在每個filter.addAction()之後調用registerReceiver? – 1000Suns

+0

我更新了我的答案,請檢查。 – nKn

+0

你是對的。 onDestroy在我註冊之前被調用。 onDestroy被調用,然後我的代碼嘗試設置接收器。 onReceive方法永遠不會被調用。我不知道如何解決這個問題。 – 1000Suns

0

試試把它註冊在YOUT onCreate()方法是這樣的:

@Override 
public void onCreate (Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getActivity().getApplicationContext().registerReceiver(ActionFoundReceiver, filter); 
} 

如果廣播在您的應用程序只用你應該使用LocalBoradcastManager,因爲這些節目不離開你的應用程序。

希望這會有所幫助。