W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
WLAN P2P 功能用于設(shè)備與設(shè)備之間的點(diǎn)對(duì)點(diǎn)數(shù)據(jù)傳輸,應(yīng)用可以通過接口完成以下功能:
WifiP2pController 提供 WLAN P2P 功能,接口說明如下。
接口名 | 描述 | 所需權(quán)限 |
---|---|---|
init(EventRunner eventRunner, WifiP2pCallback callback) | 初始化 P2P 的信使,當(dāng)且僅當(dāng)信使被成功初始化,P2P 的其他功能才可以正常使用。 | ohos.permission.GET_WIFI_INFOohos.permission.SET_WIFI_INFO |
discoverDevices(WifiP2pCallback callback) | 搜索附近可用的 P2P 設(shè)備。 | ohos.permission.GET_WIFI_INFO |
stopDeviceDiscovery(WifiP2pCallback callback) | 停止搜索附近的 P2P 設(shè)備。 | 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 相關(guān)信息,如群組信息、連接信息、設(shè)備信息等。 | ohos.permission.GET_WIFI_INFO |
connect(WifiP2pConfig wifiP2pConfig, WifiP2pCallback callback) | 向指定設(shè)備發(fā)起連接。 | ohos.permission.GET_WIFI_INFO |
cancelConnect(WifiP2pCallback callback) | 取消向指定設(shè)備發(fā)起的連接。 | ohos.permission.GET_WIFI_INFO |
try {
// 獲取P2P管理對(duì)象
WifiP2pController wifiP2pController = WifiP2pController.getInstance(this);
// 初始化P2P管理對(duì)象,用于建立P2P信使等行為
wifiP2pController.init(EventRunner.create(true), null);
// 創(chuàng)建P2P回調(diào)對(duì)象
P2pDiscoverCallBack p2pDiscoverCallBack = new P2pDiscoverCallBack();
// 發(fā)起P2P搜索
wifiP2pController.discoverDevices(p2pDiscoverCallBack);
// 停止P2P搜索
wifiP2pController.stopDeviceDiscovery(p2pDiscoverCallBack);
} catch (RemoteException re) {
HiLog.warn(LABEL, "exception happened.");
}
// 獲取P2P啟動(dòng)與停止搜索的回調(diào)信息(失敗或者成功)
private class P2pDiscoverCallBack extends WifiP2pCallback {
@Override
public void eventExecFail(int reason) {
HiLog.info(LABEL, "discoverDevices eventExecFail reason : %{public}d", reason);
}
@Override
public void eventExecOk() {
HiLog.info(LABEL, "discoverDevices eventExecOk");
}
}
try {
// 獲取P2P管理對(duì)象
WifiP2pController wifiP2pController = WifiP2pController.getInstance(this);
// 初始化P2P管理對(duì)象,用于建立P2P信使等行為
wifiP2pController.init(EventRunner.create(true), null);
// 創(chuàng)建用于P2P建組需要的配置
WifiP2pConfig wifiP2pConfig = new WifiP2pConfig("DEFAULT_GROUP_NAME", "DEFAULT_PASSPHRASE");
wifiP2pConfig.setDeviceAddress("02:02:02:02:03:04");
wifiP2pConfig.setGroupOwnerBand(0);
// 創(chuàng)建P2P回調(diào)對(duì)象
P2pCreateGroupCallBack p2pCreateGroupCallBack = new P2pCreateGroupCallBack();
// 創(chuàng)建P2P群組
wifiP2pController.createGroup(wifiP2pConfig, p2pCreateGroupCallBack);
// 移除P2P群組
wifiP2pController.removeGroup(p2pCreateGroupCallBack);
} catch (RemoteException re) {
HiLog.warn(LABEL, "exception happened.");
}
private class P2pCreateGroupCallBack extends WifiP2pCallback {
@Override
public void eventExecFail(int reason) {
HiLog.info(LABEL, "CreateGroup eventExecFail reason : %{public}d", reason);
}
@Override
public void eventExecOk() {
HiLog.info(LABEL, "CreateGroup eventExecOk");
}
}
try {
// 獲取P2P管理對(duì)象
WifiP2pController wifiP2pController = WifiP2pController.getInstance(this);
// 初始化P2P管理對(duì)象,用于建立P2P信使等行為
wifiP2pController.init(EventRunner.create(true), null);
// 查詢可用P2P設(shè)備信息,通過回調(diào)獲取P2P設(shè)備信息
P2pRequestPeersCallBack p2pRequestPeersCallBack = new P2pRequestPeersCallBack();
wifiP2pController.requestP2pInfo(WifiP2pController.DEVICE_LIST_REQUEST, p2pRequestPeersCallBack);
} catch (RemoteException re) {
HiLog.warn(LABEL, "exception happened.");
}
private class P2pRequestPeersCallBack extends WifiP2pCallback {
@Override
public void eventP2pDevicesList(List<WifiP2pDevice> devices) {
HiLog.info(LABEL, "eventP2pDevicesList when start connect group");
// 根據(jù)場景不同,選擇不同的設(shè)備進(jìn)行連接,這里,通過MAC地址搜索到指定設(shè)備
WifiP2pConfig wifiP2pConfig = getSameP2pConfigFromDevices(devices);
try {
if (wifiP2pConfig != null) {
// 向指定的設(shè)備發(fā)起連接
wifiP2pController.connect(wifiP2pConfig, null);
}
} catch (RemoteException re) {
HiLog.warn(LABEL, "exception happened in connect.");
}
}
}
private WifiP2pConfig getSameP2pConfigFromDevices(List<WifiP2pDevice> devices) {
if (devices == null) {
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管理對(duì)象
WifiP2pController wifiP2pController = WifiP2pController.getInstance(this);
// 初始化P2P管理對(duì)象,用于建立P2P信使等行為
wifiP2pController.init(EventRunner.create(true), null);
// 查詢可用P2P群組信息,通過回調(diào)獲取P2P群組信息
P2pRequestGroupInfoCallBack p2pRequestGroupInfoCallBack = new P2pRequestGroupInfoCallBack();
wifiP2pController.requestP2pInfo(WifiP2pController.GROUP_INFO_REQUEST, p2pRequestGroupInfoCallBack);
// 查詢可用P2P設(shè)備信息,通過回調(diào)獲取P2P設(shè)備信息
P2pRequestDeviceInfoCallBack p2pRequestDeviceInfoCallBack = new P2pRequestDeviceInfoCallBack();
wifiP2pController.requestP2pInfo(WifiP2pController.DEVICE_INFO_REQUEST, p2pRequestDeviceInfoCallBack);
// 通過調(diào)用requestP2pInfo接口,可以查詢以下關(guān)鍵信息
wifiP2pController.requestP2pInfo(WifiP2pController.NETWORK_INFO_REQUEST, callback); // 網(wǎng)絡(luò)信息
wifiP2pController.requestP2pInfo(WifiP2pController.DEVICE_LIST_REQUEST, callback); // 設(shè)備列表信息
} catch (RemoteException re) {
HiLog.warn(LABEL, "exception happened.");
}
// 群組信息回調(diào)
private class P2pRequestGroupInfoCallBack extends WifiP2pCallback {
@Override
public void eventP2pGroup(WifiP2pGroup group) {
HiLog.info(LABEL, "P2pRequestGroupInfoCallBack eventP2pGroup");
doSthFor(group);
}
}
// 設(shè)備信息回調(diào)
private class P2pRequestDeviceInfoCallBack extends WifiP2pCallback {
@Override
public void eventP2pGroup(WifiP2pDevice p2pDevice) {
HiLog.info(LABEL, "eventP2pGroup");
doSthFor(p2pDevice);
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: