如果您想在您的 Android 应用程序中以编程方式实现 wifi 热点功能,这里是完整的解决方案。
API
对于设备 reflection 访问私有 API。不建议这样做,但如果您没有其他选择,那么这里有一个技巧。
首先,你需要在你的清单中有这个权限,
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
这是在运行时询问它的方法:
private boolean showWritePermissionSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
if (!Settings.System.canWrite(this)) {
Log.v("DANG", " " + !Settings.System.canWrite(this));
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
return false;
}
}
return true; //Permission already given
}
然后您可以通过反射访问setWifiEnabled 方法。如果您要求的操作正在正确处理,即启用/禁用热点,则返回 true。
public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) {
WifiManager wifiManager;
try {
if (enabled) { //disables wifi hotspot if it's already enabled
wifiManager.setWifiEnabled(false);
}
Method method = wifiManager.getClass()
.getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
return (Boolean) method.invoke(wifiManager, wifiConfig, enabled);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return false;
}
}
您还可以通过反射获取热点的wifi配置。对于 StackOverflow 上的这个问题,我有 answered 那个方法。
P.S:如果您不想以编程方式打开热点,您可以启动此intent 并打开 wifi 设置屏幕供用户手动打开。
API 解决方案 >= 26:
最后,android 为 >= Oreo 发布了官方 API。您可以只使用 android 公开的 API,即startLocalOnlyHotspot
它会在没有互联网访问的情况下打开本地热点。因此可用于托管服务器或传输文件。
它需要Manifest.permission.CHANGE_WIFI_STATE 和ACCESS_FINE_LOCATION 权限。
这里有一个简单的示例,说明如何使用此 API 打开热点。
private WifiManager wifiManager;
WifiConfiguration currentConfig;
WifiManager.LocalOnlyHotspotReservation hotspotReservation;
开启热点的方法:
@RequiresApi(api = Build.VERSION_CODES.O)
public void turnOnHotspot() {
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
@Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
hotspotReservation = reservation;
currentConfig = hotspotReservation.getWifiConfiguration();
Log.v("DANG", "THE PASSWORD IS: "
+ currentConfig.preSharedKey
+ " \n SSID is : "
+ currentConfig.SSID);
hotspotDetailsDialog();
}
@Override
public void onStopped() {
super.onStopped();
Log.v("DANG", "Local Hotspot Stopped");
}
@Override
public void onFailed(int reason) {
super.onFailed(reason);
Log.v("DANG", "Local Hotspot failed to start");
}
}, new Handler());
}
`
您可以通过以下方式获取本地创建的热点的详细信息
private void hotspotDetaisDialog()
{
Log.v(TAG, context.getString(R.string.hotspot_details_message) + "\n" + context.getString(
R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString(
R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey);
}
如果它抛出安全异常,即使在授予所需权限后,您也应该尝试使用 GPS 启用您的位置。这是solution。
最近,我开发了一个名为Spotserve 的演示应用程序。这将为所有 API>=15 的设备打开 wifi 热点,并在该热点上托管一个演示服务器。您可以检查以获取更多详细信息。希望这会有所帮助!