On API < 29 wait for Wi-Fi p2p broadcast before creating group
Merge request reports
Activity
- Resolved by Torsten Grote
- Resolved by Torsten Grote
123 146 } 124 147 125 148 void stopWifiP2pHotspot() { 149 if (SDK_INT < 29) 150 try { 151 ctx.unregisterReceiver(receiver); changed this line in version 7 of the diff
added 1 commit
- e419fc12 - On API < 29 wait for Wi-Fi p2p broadcast before creating group
added 1 commit
- fcb7354e - On API < 29 wait for Wi-Fi p2p broadcast before creating group
added 1 commit
- f2241fb6 - On API < 29 wait for Wi-Fi p2p broadcast before creating group
added 1 commit
- 79c2fb3e - On API < 29 wait for Wi-Fi p2p broadcast before creating group
added 1 commit
- f3785e01 - On API < 29 wait for Wi-Fi p2p broadcast before creating group
mentioned in merge request !13 (closed)
126 @Override 127 public void onReceive(Context context, Intent intent) { 128 int state = intent.getIntExtra(EXTRA_WIFI_STATE, 129 WIFI_P2P_STATE_DISABLED); 130 if (state == WIFI_P2P_STATE_ENABLED) { 131 try { 132 wifiP2pManager.createGroup(channel, HotspotManager.this); 133 } catch (SecurityException e) { 134 // this should never happen, because we request permissions before 135 throw new AssertionError(e); 136 } 137 try { 138 ctx.unregisterReceiver(receiver); 139 } catch (IllegalArgumentException e) { 140 // Can happen if the hotspot got stopped in the meantime and 141 // the receiver already got unregistered. Yes, OK. All methods registering and unregistering run on the UI thread so there's no concurrency issues involved.
I added a flag
receiverRegistered
, but if you prefer a specialnull
of the receiver to represent that it's also fine.Edited by Sebastianchanged this line in version 7 of the diff
- Resolved by Sebastian
135 int state = intent.getIntExtra(EXTRA_WIFI_STATE, 136 WIFI_P2P_STATE_DISABLED); 137 if (state == WIFI_P2P_STATE_ENABLED) { 138 try { 139 wifiP2pManager.createGroup(channel, HotspotManager.this); 140 } catch (SecurityException e) { 141 // this should never happen, because we request permissions before 142 throw new AssertionError(e); 143 } 144 unregisterReceiver(); 145 } 146 } 147 148 }; 149 150 private void unregisterReceiver() { 145 } 146 } 147 148 }; 149 150 private void unregisterReceiver() { 151 if (!receiverRegistered) 152 return; 153 try { 154 ctx.unregisterReceiver(receiver); 155 receiverRegistered = false; 156 } catch (IllegalArgumentException e) { 157 // This gets thrown when we try to unregister an already unregistered 158 // receiver. We do keep a flag whether the receiver is actually 159 // registered, so it should not happen. 160 throw new AssertionError(e); I think the benefit of the current way is that in error log from reports we receive we know right away that we were, at the time of writing the code, assuming that this is a situation that should never happen. While if we just see an IllegalArgumentException we might wonder if we missed something there and haven given some situation a thought yet.
I think we could do that. We have that bit of extra time on higher API levels because the user needs to dismiss the WiFi settings panel and that usually seems to be a lot more time that required for the event to come in. But we can't be sure of course. If we use the broadcast, the fragment might move to the next while the panel is still open, which could be a bit strange. We could then try to make this dependent on the combination of both, the broadcast having been received plus the panel being closed. Not sure if this over-complicates the matter.
Maybe I am misunderstanding something, but isn't the WiFi panel shown as part of the condition checks that we do before trying to start a hotspot?
If we use the broadcast, the fragment might move to the next while the panel is still open, which could be a bit strange
Yeah that would be strange, but how could that happen? Shouldn't we start the hotspot only after we know that Wi-Fi is enabled?
hmm, the user taps the "start hotspot" button, the conditions get checked. Then potientially the panel appears because the Wifi was disabled. The user enables the switch, but then additionally needs to close the panel. Toggling the switch enables the wifi which will also trigger the broadcast. That extra interaction from the user to close the panel is what usually takes longer than the system enabling the wifi.
As things become a bit intertwined, I'm closing this and continuing this as a whole in !13 (closed)