This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow usage when hotspot mode is enabled.
- Loading branch information
1 parent
425737d
commit fa6dab3
Showing
3 changed files
with
31 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
import android.widget.TextView; | ||
|
||
import java.util.Locale; | ||
import java.lang.reflect.Method; | ||
|
||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
@@ -70,7 +71,7 @@ protected void onCreate(Bundle savedInstanceState) { | |
floatingActionButtonOnOff.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
if (isConnectedInWifi()) { | ||
if (isConnectedInWifi(false)) { | ||
if (!isStarted && startAndroidWebServer()) { | ||
isStarted = true; | ||
textViewMessage.setVisibility(View.VISIBLE); | ||
|
@@ -134,6 +135,8 @@ private void initBroadcastReceiverNetworkStateChanged() { | |
final IntentFilter filters = new IntentFilter(); | ||
filters.addAction("android.net.wifi.WIFI_STATE_CHANGED"); | ||
filters.addAction("android.net.wifi.STATE_CHANGE"); | ||
filters.addAction("android.net.wifi.WIFI_AP_STATE_CHANGED"); | ||
|
||
broadcastReceiverNetworkState = new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
|
@@ -144,9 +147,15 @@ public void onReceive(Context context, Intent intent) { | |
} | ||
|
||
private String getIpAccess(boolean url) { | ||
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); | ||
int ipAddress = wifiManager.getConnectionInfo().getIpAddress(); | ||
final String formatedIpAddress = String.format(Locale.getDefault(), "%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)); | ||
String formatedIpAddress; | ||
|
||
if (isConnectedInWifi(true)) { | ||
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); | ||
int ipAddress = wifiManager.getConnectionInfo().getIpAddress(); | ||
formatedIpAddress = String.format(Locale.getDefault(), "%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)); | ||
} else { | ||
formatedIpAddress = "192.168.43.1"; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
codestation
Author
Owner
|
||
} | ||
|
||
if (url) { | ||
return "http://" + formatedIpAddress + ":"; | ||
|
@@ -160,11 +169,23 @@ private int getPortFromEditText() { | |
return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; | ||
} | ||
|
||
public boolean isConnectedInWifi() { | ||
public boolean isConnectedInWifi(boolean wifiOnly) { | ||
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); | ||
NetworkInfo networkInfo = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); | ||
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected() | ||
boolean wifiEnabled = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected() | ||
&& wifiManager.isWifiEnabled() && networkInfo.getTypeName().equals("WIFI"); | ||
|
||
if(!wifiOnly && !wifiEnabled) { | ||
try { | ||
final Method method = wifiManager.getClass().getDeclaredMethod("isWifiApEnabled"); | ||
method.setAccessible(true); | ||
wifiEnabled = (Boolean) method.invoke(wifiManager); | ||
} catch(Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
return wifiEnabled; | ||
} | ||
//endregion | ||
|
||
|
Why is this ip hardcoded?