文章40 | 阅读 20931 | 点赞0
| 接口名 | 描述 | 所需权限 |
| getInstance(Context context) | 获取WLAN功能管理对象实例,通过该实例调用WLAN基本功能API | NA |
| isWifiActive() | 获取当前WLAN打开状态 | ohos.permission.GET_WIFI_INFO |
| scan() | 发起WLAN扫描 | ohos.permission.SET_WIFI_INFO ohos.permission.LOCATION |
| getScanInfoList() | 获取上次扫描结果 | ohos.permission.GET_WIFI_INFO ohos.permission.LOCATION |
| isConnected() | 获取当前WLAN连接状态 | ohos.permission.GET_WIFI_INFO |
| getLinkedInfo() | 获取当前的WLAN连接信息 | ohos.permission.GET_WIFI_INFO |
| getIpInfo() | 获取当前连接的WLAN IP信息 | ohos.permission.GET_WIFI_INFO |
| getSignalLevel(int rssi, int band) | 通过RSSI与频段计算信号格数 | NA |
| getCountryCode() | 获取设备的国家码 | ohos.permission.LOCATION ohos.permission.GET_WIFI_INFO |
| isFeatureSupported(long featureId) | 获取设备是否支持指定的特性 | ohos.permission.GET_WIFI_INFO |
// 获取WLAN管理对象
WifiDevice wifiDevice = WifiDevice.getInstance(context);
// 调用获取WLAN开关状态接口
boolean isWifiActive = wifiDevice.isWifiActive(); // 若WLAN打开,则返回true,否则返回false
// 获取WLAN管理对象
WifiDevice wifiDevice = WifiDevice.getInstance(context);
// 调用WLAN扫描接口
boolean isScanSuccess = wifiDevice.scan();
// 调用获取扫描结果
List<WifiScanInfo> scanInfos = wifiDevice.getScanInfoList();
// 获取WLAN管理对象
WifiDevice wifiDevice = WifiDevice.getInstance(context);
// 调用WLAN连接状态接口,确定当前设备是否连接WLAN
boolean isConnected = wifiDevice.isConnected();
if (isConnected) {
// 获取WLAN连接信息
Optional<WifiLinkedInfo> linkedInfo = wifiDevice.getLinkedInfo();
// 获取连接信息中的SSID
String ssid = linkedInfo.get().getSsid();
// 获取WLAN的IP信息
Optional<IpInfo> ipInfo = wifiDevice.getIpInfo();
// 获取IP信息中的IP地址与网关
int ipAddress = ipInfo.get().getIpAddress();
int gateway = ipInfo.get().getGateway();
}
// 获取WLAN管理对象
WifiDevice wifiDevice = WifiDevice.getInstance(context);
// 获取当前设备的国家码
String countryCode = wifiDevice.getCountryCode();
// 获取WLAN管理对象
WifiDevice wifiDevice = WifiDevice.getInstance(context);
// 获取当前设备是否支持指定的能力
boolean isSupportInfra = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_INFRA);
isSupportInfra5g = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_INFRA_5G);
isSupportPasspoint = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_PASSPOINT);
isSupportP2p = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_P2P);
isSupportHotspot = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_MOBILE_HOTSPOT);
isSupportAware = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_AWARE);
isSupportApSta = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_AP_STA);
isSupportWpa3Sae = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_WPA3_SAE);
isSupportWpa3SuiteB = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_WPA3_SUITE_B);
isSupportOwe = wifiDevice.isFeatureSupported(WifiUtils.WIFI_FEATURE_OWE);
| 接口名 | 描述 | 所需权限 |
| getInstance(Context context) | 获取WLAN功能管理对象实例,通过该实例调用不信任热点配置的API | NA |
| addUntrustedConfig(WifiDeviceConfig config) | 添加不信任热点配置,选网优先级低于已保存热点 | ohos.permission.SET_WIFI_INFO |
| removeUntrustedConfig(WifiDeviceConfig config) | 删除不信任热点配置 | ohos.permission.SET_WIFI_INFO |
// 获取WLAN管理对象
WifiDevice wifiDevice = WifiDevice.getInstance(context);
// 设置三方添加的不信任配置
WifiDeviceConfig config = new WifiDeviceConfig();
config.setSsid("untrusted-exist");
config.setPreSharedKey("123456789");
config.setHiddenSsid(false);
config.setSecurityType(WifiSecurity.PSK);
boolean isSuccess = wifiDevice.addUntrustedConfig(config);
// 获取WLAN管理对象
WifiDevice wifiDevice = WifiDevice.getInstance(context);
// 设置删除三方添加的不信任配置
WifiDeviceConfig config = new WifiDeviceConfig();
config.setSsid("untrusted-exist");
config.setPreSharedKey("123456789");
config.setHiddenSsid(false);
config.setSecurityType(WifiSecurity.PSK);
boolean isSuccess = wifiDevice.removeUntrustedConfig(config);
| 接口名 | 描述 | 所需权限 |
| init(EventRunner eventRunner, WifiP2pCallback callback) | 初始化P2P的信使,当且仅当信使被成功初始化,P2P的其他功能才可以正常使用 | ohos.permission.GET_WIFI_INFO ohos.permission.SET_WIFI_INFO |
| discoverDevices(WifiP2pCallback callback) | 搜索附近可用的P2P设备 | ohos.permission.GET_WIFI_INFO |
| stopDeviceDiscovery(WifiP2pCallback callback) | 停止搜索附近的P2P设备 | ohos.permission.GET_WIFI_INFO |
| createGroup(WifiP2pConfig wifiP2pConfig, WifiP2pCallback callback) | 建立P2P群组 | ohos.permission.GET_WIFI_INFO |
| removeGroup(WifiP2pCallback callback) | 移除P2P群组 | ohos.permission.GET_WIFI_INFO |
| requestP2pInfo(int requestType, WifiP2pCallback callback) | 请求P2P相关信息,如群组信息、连接信息、设备信息等 | ohos.permission.GET_WIFI_INFO |
| connect(WifiP2pConfig wifiP2pConfig, WifiP2pCallback callback) | 向指定设备发起连接 | ohos.permission.GET_WIFI_INFO |
| cancelConnect(WifiP2pCallback callback) | 取消向指定设备发起的连接 | ohos.permission.GET_WIFI_INFO |
try {
// 获取P2P管理对象
WifiP2pController wifiP2pController = WifiP2pController.getInstance(this);
// 初始化P2P管理对象,用于建立P2P信使等行为
wifiP2pController.init(EventRunner.create(true), null);
// 创建P2P回调对象
P2pDiscoverCallBack p2pDiscoverCallBack = new P2pDiscoverCallBack();
// 发起P2P搜索
wifiP2pController.discoverDevices(p2pDiscoverCallBack);
// 停止P2P搜索
wifiP2pController.stopDeviceDiscovery(p2pDiscoverCallBack);
} catch (RemoteException re) {
HiLog.error(LABEL, "exception happened.");
}
// 获取P2P启动与停止搜索的回调信息(失败或者成功)
private class P2pDiscoverCallBack extends WifiP2pCallback {
@Override
public void eventExecFail(int reason) {
HiLog.warn(LABEL, "discoverDevices eventExecFail reason : %{public}d", reason);
}
@Override
public void eventExecOk() {
HiLog.info(LABEL, "discoverDevices eventExecOk");
}
}
try {
// 获取P2P管理对象
WifiP2pController wifiP2pController = WifiP2pController.getInstance(this);
// 初始化P2P管理对象,用于建立P2P信使等行为
wifiP2pController.init(EventRunner.create(true), null);
// 创建用于P2P建组需要的配置
WifiP2pConfig wifiP2pConfig = new WifiP2pConfig("DEFAULT_GROUP_NAME", "DEFAULT_PASSPHRASE");
wifiP2pConfig.setDeviceAddress("02:02:02:02:03:04");
wifiP2pConfig.setGroupOwnerBand(0);
// 创建P2P回调对象
P2pCreateGroupCallBack p2pCreateGroupCallBack = new P2pCreateGroupCallBack();
// 创建P2P群组
wifiP2pController.createGroup(wifiP2pConfig, p2pCreateGroupCallBack);
// 移除P2P群组
wifiP2pController.removeGroup(p2pCreateGroupCallBack);
} catch (RemoteException re) {
HiLog.error(LABEL, "exception happened.");
}
private class P2pCreateGroupCallBack extends WifiP2pCallback {
@Override
public void eventExecFail(int reason) {
HiLog.warn(LABEL, "CreateGroup eventExecFail reason : %{public}d", reason);
}
@Override
public void eventExecOk() {
HiLog.info(LABEL, "CreateGroup eventExecOk");
}
}
try {
// 获取P2P管理对象
WifiP2pController wifiP2pController = WifiP2pController.getInstance(this);
// 初始化P2P管理对象,用于建立P2P信使等行为
wifiP2pController.init(EventRunner.create(true), null);
// 查询可用P2P设备信息,通过回调获取P2P设备信息
P2pRequestPeersCallBack p2pRequestPeersCallBack = new P2pRequestPeersCallBack();
wifiP2pController.requestP2pInfo(WifiP2pController.DEVICE_LIST_REQUEST, p2pRequestPeersCallBack);
} catch (RemoteException re) {
HiLog.error(LABEL, "exception happened.");
}
private class P2pRequestPeersCallBack extends WifiP2pCallback {
@Override
public void eventP2pDevicesList(List<WifiP2pDevice> devices) {
HiLog.info(LABEL, "eventP2pDevicesList when start connect group");
// 根据场景不同,选择不同的设备进行连接,通过MAC地址搜索到指定设备
WifiP2pConfig wifiP2pConfig = getSameP2pConfigFromDevices(devices);
try {
if (wifiP2pConfig != null) {
// 向指定的设备发起连接
wifiP2pController.connect(wifiP2pConfig, null);
}
} catch (RemoteException re) {
HiLog.error(LABEL, "exception happened in connect.");
}
}
}
private WifiP2pConfig getSameP2pConfigFromDevices(List<WifiP2pDevice> devices) {
if (devices == null || devices.isEmpty()) {
return null;
}
for (int i = 0; i < devices.size(); i++) {
WifiP2pDevice p2pDevice = devices.get(i);
HiLog.info(LABEL, "p2pDevice.getDeviceAddress() : %{private}s", p2pDevice.getDeviceAddress());
if (p2pDevice.getDeviceAddress() != null
&& p2pDevice.getDeviceAddress().equals(TARGET_P2P_MAC_ADDRESS)) {
HiLog.info(LABEL, "received same mac address");
WifiP2pConfig wifiP2pConfig = new WifiP2pConfig("DEFAULT_GROUP_NAME", "DEFAULT_PASSPHRASE"); // 根据实际情况配置名字和密码
wifiP2pConfig.setDeviceAddress(p2pDevice.getDeviceAddress());
return wifiP2pConfig;
}
}
return null;
}
try {
// 获取P2P管理对象
WifiP2pController wifiP2pController = WifiP2pController.getInstance(this);
// 初始化P2P管理对象,用于建立P2P信使等行为
wifiP2pController.init(EventRunner.create(true), null);
// 查询可用P2P群组信息,通过回调获取P2P群组信息
P2pRequestGroupInfoCallBack p2pRequestGroupInfoCallBack = new P2pRequestGroupInfoCallBack();
wifiP2pController.requestP2pInfo(WifiP2pController.GROUP_INFO_REQUEST, p2pRequestGroupInfoCallBack);
// 查询可用P2P设备信息,通过回调获取P2P设备信息
P2pRequestDeviceInfoCallBack p2pRequestDeviceInfoCallBack = new P2pRequestDeviceInfoCallBack();
wifiP2pController.requestP2pInfo(WifiP2pController.DEVICE_INFO_REQUEST, p2pRequestDeviceInfoCallBack);
// 通过调用requestP2pInfo接口,可以查询以下关键信息
wifiP2pController.requestP2pInfo(WifiP2pController.NETWORK_INFO_REQUEST, callback); // 网络信息
wifiP2pController.requestP2pInfo(WifiP2pController.DEVICE_LIST_REQUEST, callback); // 设备列表信息
} catch (RemoteException re) {
HiLog.error(LABEL, "exception happened.");
}
// 群组信息回调
private class P2pRequestGroupInfoCallBack extends WifiP2pCallback {
@Override
public void eventP2pGroup(WifiP2pGroup group) {
HiLog.info(LABEL, "P2pRequestGroupInfoCallBack eventP2pGroup");
doSthFor(group);
}
}
// 设备信息回调
private class P2pRequestDeviceInfoCallBack extends WifiP2pCallback {
@Override
public void eventP2pDevice(WifiP2pDevice p2pDevice) {
HiLog.info(LABEL, "P2pRequestDeviceInfoCallBack eventP2pDevice");
doSthFor(p2pDevice);
}
}
| 描述 | 通知名 | 附加参数 |
| WLAN状态 | usual.event.wifi.POWER_STATE | active_state |
| WLAN扫描 | usual.event.wifi.SCAN_FINISHED | scan_state |
| WLAN RSSI变化 | usual.event.wifi.RSSI_VALUE | rssi_value |
| WLAN连接状态 | usual.event.wifi.CONN_STATE | conn_state |
| Hotspot状态 | usual.event.wifi.HOTSPOT_STATE | hotspot_active_state |
| Hotspot连接状态 | usual.event.wifi.WIFI_HS_STA_JOIN<br>usual.event.wifi.WIFI_HS_STA_LEAVE | - |
| P2P状态 | usual.event.wifi.p2p.STATE_CHANGE | p2p_state |
| P2P连接状态 | usual.event.wifi.p2p.CONN_STATE_CHANGE | linked_info<br>net_info<br>group_info |
| P2P设备列表变化 | usual.event.wifi.p2p.DEVICES_CHANGE | - |
| P2P搜索状态变化 | usual.event.wifi.p2p.PEER_DISCOVERY_STATE_CHANGE | peers_discovery |
| P2P当前设备变化 | usual.event.wifi.p2p.CURRENT_DEVICE_CHANGE | p2p_device |
// 构建消息接收者/注册者
class WifiEventSubscriber extends CommonEventSubscriber {
WifiEventSubscriber(CommonEventSubscribeInfo info) {
super(info);
}
@Override
public void onReceiveEvent(CommonEventData commonEventData) {
if (commonEventData == null || commonEventData.getIntent() == null) {
return;
}
if (WifiEvents.EVENT_ACTIVE_STATE.equals(commonEventData.getIntent().getAction())) {
// 获取附带参数
IntentParams params = commonEventData.getIntent().getParams();
if (params == null) {
return;
}
int wifiState= (int) params.getParam(WifiEvents.PARAM_ACTIVE_STATE);
if (wifiState== WifiEvents.STATE_ACTIVE) { // 处理WLAN被打开消息
HiLog.info(LABEL, "Receive WifiEvents.STATE_ACTIVE %{public}d", wifiState);
} else if (wifiState == WifiEvents.STATE_INACTIVE) { // 处理WLAN被关闭消息
HiLog.info(LABEL, "Receive WifiEvents.STATE_INACTIVE %{public}d", wifiState);
} else { // 处理WLAN异常状态
HiLog.warn(LABEL,"Unknown wifi state");
}
}
}
}
// 注册消息
MatchingSkills match = new MatchingSkills();
// 增加获取WLAN状态变化消息
match.addEvent(WifiEvents.EVENT_ACTIVE_STATE);
CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(match);
subscribeInfo.setPriority(100);
WifiEventSubscriber subscriber = new WifiEventSubscriber(subscribeInfo);
try {
CommonEventManager.subscribeCommonEvent(subscriber);
} catch (RemoteException e) {
HiLog.warn(LABEL, "subscribe in wifi events failed!");
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/Forever_wj/article/details/118438995
内容来源于网络,如有侵权,请联系作者删除!