1# P2P Connection Development 2 3## Introduction 4The peer-to-peer (P2P) mode provides a point-to-point connection between devices in a WLAN. It enables the direct establishment of a TCP/IP connection between two stations (STAs) without the involvement of 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 the [P2P API Reference](../../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 Wi-Fi 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. 405. Example: 41 42```ts 43import { wifiManager } from '@kit.ConnectivityKit'; 44 45// Create a P2P group. To use the current device as the group owner (GO), set: 46// 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 key negotiation must be performed again. 47// The value -2 means to create a persistent group. The device in the group can be reconnected without GO negotiation or WPS key negotiation. 48 49let recvP2pPersistentGroupChangeFunc = () => { 50 console.info("p2p persistent group change receive event"); 51 52 // Services to be processed after the persistent group is created. 53} 54// Create a persistent group, and register a listener callback for the persistent group status changes. 55wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 56try { 57 let config:wifiManager.WifiP2PConfig = { 58 deviceAddress: "00:11:22:33:44:55", 59 deviceAddressType: 1, 60 netId: -2, 61 passphrase: "12345678", 62 groupName: "testGroup", 63 goBand: 0 64 } 65 wifiManager.createGroup(config); 66}catch(error){ 67 console.error("failed:" + JSON.stringify(error)); 68} 69 70// Remove a P2P group. 71try { 72 wifiManager.removeGroup(); 73}catch(error){ 74 console.error("failed:" + JSON.stringify(error)); 75} 76``` 77 786. For details about error codes, see [Wi-Fi Error Codes](../../reference/apis-connectivity-kit/errorcode-wifi.md). 79 80### Setting Up a P2P Connection 811. Import the Wi-Fi module. 822. Enable Wi-Fi on the device. 833. Check that the device has the SystemCapability.Communication.WiFi.P2P capability. 844. Register a callback for **p2pPeerDeviceChange** and set up a P2P connection in the callback implementation. 855. Start P2P device discovery. 866. Example: 87 88```ts 89import { wifiManager } from '@kit.ConnectivityKit'; 90 91let recvP2pConnectionChangeFunc = (result:wifiManager.WifiP2pLinkedInfo) => { 92 console.info("p2p connection change receive event: " + JSON.stringify(result)); 93 wifiManager.getP2pLinkedInfo((err, data) => { 94 if (err) { 95 console.error("failed to get P2pLinkedInfo: " + JSON.stringify(err)); 96 return; 97 } 98 console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); 99 // Add the service processing logic after a successful or failed P2P connection. 100 }); 101} 102// After the P2P connection is set up, the callback for p2pConnectionChange will be called. 103wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 104 105let recvP2pPeerDeviceChangeFunc = (result:wifiManager.WifiP2pDevice[]) => { 106 console.info("p2p peer device change receive event: " + JSON.stringify(result)); 107 wifiManager.getP2pPeerDevices((err, data) => { 108 if (err) { 109 console.error("failed to get peer devices: " + JSON.stringify(err)); 110 return; 111 } 112 console.info("get peer devices: " + JSON.stringify(data)); 113 let len = data.length; 114 for (let i = 0; i < len; ++i) { 115 // Select the peer P2P device that meets the conditions. 116 if (data[i].deviceName === "my_test_device") { 117 console.info("p2p connect to test device: " + data[i].deviceAddress); 118 let config:wifiManager.WifiP2PConfig = { 119 deviceAddress:data[i].deviceAddress, 120 deviceAddressType: 1, 121 netId:-2, 122 passphrase:"", 123 groupName:"", 124 goBand:0, 125 } 126 // Set up a P2P connection. The GO cannot initiate connections. 127 wifiManager.p2pConnect(config); 128 } 129 } 130 }); 131} 132// The callback for p2pPeerDeviceChange will be called when the P2P scanning result is reported. 133wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 134 135setTimeout(() => {wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); 136setTimeout(() => {wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); 137// Start to discover P2P devices, that is, start P2P scanning. 138console.info("start discover devices -> " + wifiManager.startDiscoverDevices()); 139``` 140 1417. For details about error codes, see [Wi-Fi Error Codes](../../reference/apis-connectivity-kit/errorcode-wifi.md). 142