This code provides methods for working with WiFi and hotspot functionality on Android, including: Note it might have some errors as I am not a perfect developer
getScanResults() and startScan(): get a list of available WiFi networks and start a new scan for networks
connectToWifi(String ssid, String password), disconnectFromWifi(), and isWifiConnected(): connect to and disconnect from a WiFi network and check if the device is currently connected to a WiFi network
getWifiIpAddress(), getWifiLinkSpeed(), getWifiMacAddress(), and getWifiSSID(): get the IP address, link speed, MAC address, and SSID of the currently connected WiFi network
isWifiHotspotSupported(): check if the device supports WiFi hotspot functionality
sendWifiData(String ipAddress, int port, byte[] data): send data over a WiFi connection to the specified IP address and port
WifiDataReceiver: a nested class that provides a way to receive data over a WiFi connection on a specified port and handle the received data using a callback method.
Java :
package com.example;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class WifiManagerWrapper {
private static final String TAG = "WifiManagerWrapper";
private WifiManager wifiManager;
public WifiManagerWrapper(Context context) {
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
public boolean isWifiEnabled() {
return wifiManager.isWifiEnabled();
}
public boolean setWifiEnabled(boolean enabled) {
return wifiManager.setWifiEnabled(enabled);
}
public boolean isWifiApEnabled() {
try {
Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
return (Boolean) method.invoke(wifiManager);
} catch (NoSuchMethodException e) {
Log.e(TAG, "isWifiApEnabled() method not found", e);
} catch (IllegalAccessException e) {
Log.e(TAG, "isWifiApEnabled() method access failed", e);
} catch (InvocationTargetException e) {
Log.e(TAG, "isWifiApEnabled() method invocation failed", e);
}
return false;
}
public boolean setWifiApEnabled(boolean enabled, String ssid, String password) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Method method = wifiManager.getClass().getMethod("setWifiApEnabled",
WifiConfiguration.class, boolean.class);
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = ssid;
wifiConfig.preSharedKey = password;
return (Boolean) method.invoke(wifiManager, wifiConfig, enabled);
} else {
Method method = wifiManager.getClass().getMethod("setWifiApEnabled",
WifiConfiguration.class, boolean.class);
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = ssid;
wifiConfig.preSharedKey = password;
return (Boolean) method.invoke(wifiManager, wifiConfig, enabled);
}
} catch (NoSuchMethodException e) {
Log.e(TAG, "setWifiApEnabled() method not found", e);
} catch (IllegalAccessException e) {
Log.e(TAG, "setWifiApEnabled() method access failed", e);
} catch (InvocationTargetException e) {
Log.e(TAG, "setWifiApEnabled() method invocation failed", e);
}
return false;
}
public List<ScanResult> getScanResults() {
return wifiManager.getScanResults();
}
public boolean startScan() {
return wifiManager.startScan();
}
public boolean connectToWifi(String ssid, String password) {
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
wifiConfig.preSharedKey = "\"" + password + "\"";
int networkId = wifiManager.addNetwork(wifiConfig);
return wifiManager.enableNetwork(networkId, true);
}
public void disconnectFromWifi() {
wifiManager.disconnect();
}
public boolean isWifiConnected() {
return wifiManager.getConnectionInfo().getSupplicantState() == android.net.wifi.SupplicantState.COMPLETED;
}
public String getWifiIpAddress() {
int ip = wifiManager.getConnectionInfo().getIpAddress();
return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + (ip >> 24 & 0xFF);
}
public int getWifiLinkSpeed() {
return wifiManager.getConnectionInfo().getLinkSpeed();
}
public String getWifiMacAddress() {
return wifiManager.getConnectionInfo().getMacAddress();
}
public String getWifiSSID() {
return wifiManager.getConnectionInfo().getSSID().replaceAll("\"", "");
}
public boolean isWifiHotspotSupported() {
try {
Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
public boolean sendWifiData(String ipAddress, int port, byte[] data) {
return new WifiDataSender(ipAddress, port).sendData(data);
}
public WifiDataReceiver startWifiDataReceiver(int port) {
return new WifiDataReceiver(port);
}
private class WifiDataSender {
private String ipAddress;
private int port;
public WifiDataSender(String ipAddress, int port) {
this.ipAddress = ipAddress;
this.port= port;
}
public boolean sendData(byte[] data) {
try {
java.net.Socket socket = new java.net.Socket(ipAddress, port);
socket.getOutputStream().write(data);
socket.close();
return true;
} catch (Exception e) {
Log.e(TAG, "Failed to send Wi-Fi data", e);
return false;
}
}
}
public class WifiDataReceiver {
private int port;
private boolean running = false;
private java.net.ServerSocket serverSocket;
public WifiDataReceiver(int port) {
this.port = port;
}
public void start() {
try {
serverSocket = new java.net.ServerSocket(port);
running = true;
new Thread(new Runnable() {
@Override
public void run() {
while (running) {
try {
java.net.Socket socket = serverSocket.accept();
byte[] buffer = new byte[1024];
int bytesRead = socket.getInputStream().read(buffer);
byte[] data = new byte[bytesRead];
System.arraycopy(buffer, 0, data, 0, bytesRead);
onWifiDataReceived(data);
socket.close();
} catch (Exception e) {
Log.e(TAG, "Failed to receive Wi-Fi data", e);
}
}
}
}).start();
} catch (Exception e) {
Log.e(TAG, "Failed to start Wi-Fi data receiver", e);
}
}
public void stop() {
running = false;
try {
serverSocket.close();
} catch (Exception e) {
Log.e(TAG, "Failed to stop Wi-Fi data receiver", e);
}
}
public void onWifiDataReceived(byte[] data) {
// Override this method to handle received data
}
}
}
Haxe :
import lime.system.JNI;
import lime.app.Application;
class WifiActivity extends Application {
private var wifi:Dynamic;
private var wifiReceiver:Dynamic;
public function new() {
super();
}
override public function onCreate():Void {
super.onCreate();
wifi = JNI.createStaticObject("com.example.WifiManagerWrapper", [this]);
// Check if WiFi is enabled and enable or disable it
if (JNI.callMethod(wifi, "isWifiEnabled", [], true)) {
JNI.callMethod(wifi, "setWifiEnabled", [false], null);
} else {
JNI.callMethod(wifi, "setWifiEnabled", [true], null);
}
// Check if the WiFi hotspot is enabled and enable or disable it
if (JNI.callMethod(wifi, "isWifiApEnabled", [], true)) {
JNI.callMethod(wifi, "setWifiApEnabled", [false, "MyHotspot", "password"], null);
} else {
JNI.callMethod(wifi, "setWifiApEnabled", [true, "MyHotspot", "password"], null);
}
// Get a list of available WiFi networks and start a new scan for networks
var scanResults:Dynamic = JNI.callMethod(wifi, "getScanResults", [], true);
if (scanResults != null) {
for (i in 0...scanResults.length) {
var ssid:String = scanResults[i].SSID;
var level:Int = scanResults[i].level;
trace("SSID: " + ssid + ", level: " + level);
}
}
JNI.callMethod(wifi, "startScan", [], null);
// Connect to a WiFi network, send data, and receive data
JNI.callMethod(wifi, "connectToWifi", ["MyNetwork", "password"], true);
JNI.callMethod(wifi, "sendWifiData", ["192.168.1.100", 1234, [1, 2, 3, 4]], true);
wifiReceiver = JNI.callMethod(wifi, "startWifiDataReceiver", [1234], true);
wifiReceiver.onWifiDataReceived = function(data:Array<Int>):Void {
trace("Received data: " + data);
}
}
override public function onDestroy():Void {
super.onDestroy();
// Disconnect from the WiFi network and stop receiving data
JNI.callMethod(wifi, "disconnectFromWifi", [], null);
wifiReceiver.stop();
}
}
Note that the startWifiDataReceiver() method returns a WifiDataReceiver object, which has an onWifiDataReceived() callback that you can override to handle received data. In the example above, we’re setting the callback function to trace the received data, but you can replace this with your own custom logic as needed.