1# P2P Mode Development Guide 2 3## Introduction 4The peer-to-peer (P2P) mode provides a point-to-point connection between devices in a WLAN. It allows a TCP/IP connection to be set up between two stations (STAs) without an access point (AP). 5 6## When to Use 7You can use the APIs provided by the **wifiManager** module to: 8 9- Create or remove a P2P group. 10- Set up a P2P connection. 11 12## Available APIs 13 14For details about the JavaScript APIs and sample code, see [WLAN](../../reference/apis-connectivity-kit/js-apis-wifiManager.md). 15 16The following table describes the APIs used in this topic. 17 18| API| Description| 19| -------- | -------- | 20| createGroup() | Creates a P2P group.| 21| removeGroup() | Removes a P2P group.| 22| startDiscoverDevices() | Starts to discover devices.| 23| getP2pPeerDevices() | Obtains the P2P peer device list.| 24| p2pConnect() | Sets up a P2P connection.| 25| getP2pLinkedInfo() | Obtains P2P connection information.| 26| on(type: 'p2pPersistentGroupChange') | Subscribes to P2P persistent group state changes.| 27| off(type: 'p2pPersistentGroupChange') | Unsubscribes from P2P persistent group state changes.| 28| on(type: 'p2pPeerDeviceChange') | Subscribes to P2P peer device state changes.| 29| off(type: 'p2pPeerDeviceChange') | Unsubscribes from P2P peer device state changes.| 30| on(type: 'p2pConnectionChange') | Subscribes to P2P connection state changes.| 31| off(type: 'p2pConnectionChange') | Unsubscribes from P2P connection state changes.| 32 33## How to Develop 34 35### Creating or Removing a P2P Group 361. Import the **wifiManager** module. 372. Enable Wi-Fi on the device. 383. Check that the device has the SystemCapability.Communication.WiFi.P2P capability. 394. Create or remove a P2P group. 40 41Example: 42 43```ts 44import { wifiManager } from '@kit.ConnectivityKit'; 45 46// Create a P2P group. To use the current device as the group owner (GO), set: 47// netId: The value -1 means to create a temporary P2P group. When a device in the group is to be connected next time, GO negotiation and WPS session negotiation must be performed again. 48// The value -2 means to create a persistent group. The device in the group can be reconnected without GO negotiation or WPS session negotiation. 49 50let recvP2pPersistentGroupChangeFunc = () => { 51 console.info("p2p persistent group change receive event"); 52 53 // Services to be processed after the persistent group is created. 54} 55// Create a persistent group, and register a listener callback for the persistent group status changes. 56wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 57try { 58 let config:wifiManager.WifiP2PConfig = { 59 deviceAddress: "00:11:22:33:44:55", 60 deviceAddressType: 1, 61 netId: -2, 62 passphrase: "12345678", 63 groupName: "testGroup", 64 goBand: 0 65 } 66 wifiManager.createGroup(config); 67}catch(error){ 68 console.error("failed:" + JSON.stringify(error)); 69} 70 71// Remove a P2P group. 72try { 73 wifiManager.removeGroup(); 74}catch(error){ 75 console.error("failed:" + JSON.stringify(error)); 76} 77``` 78 79For details about error codes, see [Wi-Fi Error Codes](../../reference/apis-connectivity-kit/errorcode-wifi.md). 80 81### Creating a P2P Connection 821. Import the **wifiManager** module. 832. Enable Wi-Fi on the device. 843. Check that the device has the SystemCapability.Communication.WiFi.P2P capability. 854. Register a callback for **p2pPeerDeviceChange** and set up a P2P connection in the callback implementation. 865. Start P2P device discovery. 87 88Example: 89 90```ts 91import { wifiManager } from '@kit.ConnectivityKit'; 92 93let recvP2pConnectionChangeFunc = (result:wifiManager.WifiP2pLinkedInfo) => { 94 console.info("p2p connection change receive event: " + JSON.stringify(result)); 95 wifiManager.getP2pLinkedInfo((err, data) => { 96 if (err) { 97 console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); 98 return; 99 } 100 console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); 101 // Add the service processing logic after a successful or failed P2P connection. 102 }); 103} 104// After the P2P connection is set up, the callback for p2pConnectionChange will be called. 105wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 106 107let recvP2pPeerDeviceChangeFunc = (result:wifiManager.WifiP2pDevice[]) => { 108 console.info("p2p peer device change receive event: " + JSON.stringify(result)); 109 wifiManager.getP2pPeerDevices((err, data) => { 110 if (err) { 111 console.error('failed to get peer devices: ' + JSON.stringify(err)); 112 return; 113 } 114 console.info("get peer devices: " + JSON.stringify(data)); 115 let len = data.length; 116 for (let i = 0; i < len; ++i) { 117 // Select the peer P2P device that meets the conditions. 118 if (data[i].deviceName === "my_test_device") { 119 console.info("p2p connect to test device: " + data[i].deviceAddress); 120 let config:wifiManager.WifiP2PConfig = { 121 deviceAddress:data[i].deviceAddress, 122 deviceAddressType: 1, 123 netId:-2, 124 passphrase:"", 125 groupName:"", 126 goBand:0, 127 } 128 // Set up a P2P connection. The GO cannot initiate connections. 129 wifiManager.p2pConnect(config); 130 } 131 } 132 }); 133} 134// The callback for p2pPeerDeviceChange will be called when the P2P scanning result is reported. 135wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 136 137setTimeout(() => {wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); 138setTimeout(() => {wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); 139// Start to discover P2P devices, that is, start P2P scanning. 140console.info("start discover devices -> " + wifiManager.startDiscoverDevices()); 141``` 142 143For details about error codes, see [Wi-Fi Error Codes](../../reference/apis-connectivity-kit/errorcode-wifi.md).