Saturday, January 26, 2013

Android 4.0.x reboots when getting Wifi Direct invitation

I have been building an android app that uses Wifi Direct.  I have noticed a lot that when my ICS device receives an invitation from another device to connect, it just reboots. It gives me this stack trace:


01-26 14:37:12.520: E/AndroidRuntime(481): *** FATAL EXCEPTION IN SYSTEM PROCESS: WifiP2pService
01-26 14:37:12.520: E/AndroidRuntime(481): java.lang.NullPointerException
01-26 14:37:12.520: E/AndroidRuntime(481): at android.net.wifi.p2p.WifiP2pService$P2pStateMachine.notifyP2pInvitationReceived(WifiP2pService.java:2331)
01-26 14:37:12.520: E/AndroidRuntime(481): at android.net.wifi.p2p.WifiP2pService$P2pStateMachine.access$8000(WifiP2pService.java:444)
01-26 14:37:12.520: E/AndroidRuntime(481): at android.net.wifi.p2p.WifiP2pService$P2pStateMachine$InactiveState.processMessage(WifiP2pService.java:1368)
01-26 14:37:12.520: E/AndroidRuntime(481): at com.android.internal.util.StateMachine$SmHandler.processMsg(StateMachine.java:881)
01-26 14:37:12.520: E/AndroidRuntime(481): at com.android.internal.util.StateMachine$SmHandler.handleMessage(StateMachine.java:741)
01-26 14:37:12.520: E/AndroidRuntime(481): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 14:37:12.520: E/AndroidRuntime(481): at android.os.Looper.loop(Looper.java:137)
01-26 14:37:12.520: E/AndroidRuntime(481): at android.os.HandlerThread.run(HandlerThread.java:60)

I looked at the source code and it seems to be referencing a group object that is possibly null. There are no workarounds for this but I have found that if you are forming a group you should try to have the ICS device initiate the connection or it may possibly reboot. This I believe was fixed in Jellybean but I am not sure - Jellybean (4.1/4.2) seems to have their own issues, but less-so. They don't reboot the device that often. Too bad the connect() method is a real hit and a miss...