對於您的用戶至少要訪問登錄頁面一次,您應該允許他們使用某種服務器技術進入Wifi。 你可以在android中使用NanoHTTPD。
這裏是參考link
這是你的移動服務器服務將是什麼樣子
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* Created by Ashish on 10/01/16.
*/
public class ServerService extends Service {
private static final int DEFAULT_PORT = 8912;
private Server server;
private BroadcastReceiver broadcastReceiverNetworkState;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String ip = getIpAccess();
server = new Server(DEFAULT_PORT);
try {
server.start();
showNotification(ip);
} catch (IOException e) {
e.printStackTrace();
}
return START_STICKY;
}
@Override
public void onDestroy() {
server.stop();
super.unregisterReceiver(broadcastReceiverNetworkState);
super.onDestroy();
}
private String getIpAccess() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
return "http://" + formatedIpAddress + ":" + DEFAULT_PORT;
}
private void showNotification(String serverAddress) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Server Running")
.setAutoCancel(false)
.setCategory("SystemServer")
.setContentText(serverAddress);
int mNotificationId = 110;
startForeground(mNotificationId, mBuilder.build());
}
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
}
Server.java
import fi.iki.elonen.NanoHTTPD;
import in.ashish29agre.androidnanohttpd.services.WebService;
/**
* Created by Ashish on 10/01/16.
*/
public class Server extends NanoHTTPD {
private static final String APPLICATION_JSON = "application/json";
public Server(int port) {
super(port);
}
public Server(String hostname, int port) {
super(hostname, port);
}
@Override
public Response serve(IHTTPSession session) {
return new WebService(session).get();
}
}
WebService.java
public class WebService {
private static final String APPLICATION_JSON = "application/json";
private NanoHTTPD.IHTTPSession session;
public WebService(NanoHTTPD.IHTTPSession session) {
this.session = session;
}
public NanoHTTPD.Response get() {
Map<String, String> response = new HashMap<>();
String value = this.session.getUri();
response.put("value", value);
response.put("request_parameters", GsonProvider.getInstance().getGson().toJson(this.session.getParms()));
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.ACCEPTED, APPLICATION_JSON, GsonProvider.getInstance().getGson().toJson(response));
}
}
有關完整的運行示例,請在鏈接中克隆git存儲庫參考。