Java 在Android Oreo 8.x中更改WiFi热点的SSID和密码
在我的Android应用程序中,我使用以下代码片段:Java 在Android Oreo 8.x中更改WiFi热点的SSID和密码,java,android,android-wifi,android-8.0-oreo,hotspot,Java,Android,Android Wifi,Android 8.0 Oreo,Hotspot,在我的Android应用程序中,我使用以下代码片段: @RequiresApi(api = Build.VERSION_CODES.O) private void turnOnHotspot(){ WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); manager.startLocalOnlyHotspot(new WifiMana
@RequiresApi(api = Build.VERSION_CODES.O)
private void turnOnHotspot(){
WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback(){
@Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
Log.d(TAG, "Wifi Hotspot is on now");
}
@Override
public void onStopped() {
super.onStopped();
Log.d(TAG, "onStopped: ");
}
@Override
public void onFailed(int reason) {
super.onFailed(reason);
Log.d(TAG, "onFailed: ");
}
},new Handler());
}
这段代码创建了一个名为AndroidShare_1234的热点。对于我的一个项目,我需要能够设置一个密码和SSID到这个热点,但我找不到一个方法来做到这一点。我想创建一个具有类似于MyHotspot的SSID和自定义密码的热点
请注意,Android O中不再支持setWifiApEnabled,旧版本的Android就是这样做的。但是,我仍然需要通过编程使用SSID和密码创建wifi热点。我不知道该怎么做。提前谢谢
谁在乎…:
对于一个学校项目,我制作了一个锁柜,每当它可以连接到具有特定证书的无线网络时,锁柜就会解锁,因此需要以编程方式设置热点 我对这个问题只有部分的解决办法。希望它对于您正在设计的应用程序来说已经足够了 启动热点时,SSID和密码由android系统硬编码。通过查看AOSP代码,我发现同一个热点可以被多个应用程序共享。此hotspotclass名称的配置为WifiConfiguration,该配置还与所有请求应用程序共享。此配置将在onStartedLocalOnlyHotspotReservation的回调中传递回应用程序。您可以通过调用reservation.getWifiConfiguration来获取WifiConfiguration。您将从WifiConfiguration对象获得所需的所有信息。因此,您可以读取预共享密钥和访问点名称。但我认为你无法改变它们 仅供参考,设置wifi配置(包括硬编码SSID和WPA2-PSK密钥)的相关代码由以下代码完成
/**
* Generate a temporary WPA2 based configuration for use by the local only hotspot.
* This config is not persisted and will not be stored by the WifiApConfigStore.
*/
public static WifiConfiguration generateLocalOnlyHotspotConfig(Context context) {
WifiConfiguration config = new WifiConfiguration();
config.SSID = context.getResources().getString(
R.string.wifi_localhotspot_configure_ssid_default) + "_"
+ getRandomIntForDefaultSsid();
config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
config.networkId = WifiConfiguration.LOCAL_ONLY_NETWORK_ID;
String randomUUID = UUID.randomUUID().toString();
// first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
config.preSharedKey = randomUUID.substring(0, 8) + randomUUID.substring(9, 13);
return config;
}
我没有答案,但您是否考虑过使用蓝牙执行身份验证或发布网络SSID?请注意,根据StartOcalOnlyHotSpot的文档,hotspot可能在多个应用程序之间共享。这表明修改SSID不太可能得到官方支持。嘿,你找到这个问题的解决方案了吗?@Visionwriter否:@Markinson你找到解决方案了吗?谢谢你的回答……context.getResources.getString R.string.wifi\u localhotspot\u configure\u ssid\u default+\u+getRandomIntForDefaultSsid;请告诉我们格式。请帮助我。sir无法更改SSIDis。对此,没有任何解决方案。我想用一些特定的名称创建热点名称。先生。@Gowthamam,据我所知,没有办法设置特定的名称。