我想要發現藍牙設備,由於某種原因,我不斷收到接收機從未註冊的非法異常。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)
堆棧跟蹤在哪裏? – nKn
增加了Stacktrace。它只是告訴我接收器從來沒有註冊;但是我願意。我不能解釋爲什麼它這樣做。 – 1000Suns