Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Commit

Permalink
fix: allow usage when hotspot mode is enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
codestation committed Aug 9, 2016
1 parent 425737d commit fa6dab3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.codestation.henkakuserver"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
}
buildTypes {
release {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
</intent-filter>
</activity>
</application>

</manifest>
</manifest>
33 changes: 27 additions & 6 deletions app/src/main/java/com/codestation/henkakuserver/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import android.widget.TextView;

import java.util.Locale;
import java.lang.reflect.Method;


public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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.

Copy link
@balika011

balika011 Aug 9, 2016

Why is this ip hardcoded?

This comment has been minimized.

Copy link
@codestation

codestation Aug 9, 2016

Author Owner

That ip address is hardcoded for wifi tethering in the android source code and cannot be changed by usermode applications. Check https://android.googlesource.com/platform/frameworks/base/+/jb-release/wifi/java/android/net/wifi/WifiStateMachine.java#1210

I didn't find a way to query for this value without involving hardcoded strings ("wlanX") or parsing the output of a command line program.

This comment has been minimized.

Copy link
@balika011

balika011 via email Aug 9, 2016

}

if (url) {
return "http://" + formatedIpAddress + ":";
Expand All @@ -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

Expand Down

0 comments on commit fa6dab3

Please sign in to comment.