我正在尝试发现蓝牙设备,出于某种原因,我一直得到接收器从未注册的非法例外。
我之前能够在Fragment上发现设备。这使得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: com.naftal.easylock.ui.fragment.HomeFragment$1@4259a7b8
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 :(得分:1)
我的猜测是,您在Intent
注册之前收到了BroadcastReceiver
。你调用一个注册接收器的方法,但是在它到达注册过程的那段时间内,接收器可能会收到Intent
,但它还没有注册。
----编辑----
还有另外一个想法:当你注册接收者之前,你可能会在unregisterReceiver()
未注册时调用onDestroy()
,或者基本上在调用registerReceiver()
方法时调用{{1}}方法。所以它可以基于处理你的Fragment,在它被销毁时尝试调试,然后调试你是否在此之前调用了{{1}}。
答案 1 :(得分:0)
尝试在你的onCreate()方法中注册它,如下所示:
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getApplicationContext().registerReceiver(ActionFoundReceiver, filter);
}
如果广播仅在您的应用中使用,则应使用LocalBoradcastManager,因为这些广播不会离开您的应用。
希望这会有所帮助。