Advertisement
Guest User

Untitled

a guest
Jan 18th, 2024
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | Source Code | 0 0
  1. // Create a WifiP2pManager instance
  2. WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
  3.  
  4. // Create a WifiP2pManager.Channel instance
  5. WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null);
  6.  
  7. // Discover nearby devices
  8. manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
  9.     @Override
  10.     public void onSuccess() {
  11.         // Peers discovered successfully
  12.     }
  13.  
  14.     @Override
  15.     public void onFailure(int reasonCode) {
  16.         // Failed to discover peers
  17.     }
  18. });
  19.  
  20. // Connect to a specific device
  21. WifiP2pConfig config = new WifiP2pConfig();
  22. config.deviceAddress = "device_mac_address";
  23. manager.connect(channel, config, new WifiP2pManager.ActionListener() {
  24.     @Override
  25.     public void onSuccess() {
  26.         // Connection successful
  27.     }
  28.  
  29.     @Override
  30.     public void onFailure(int reasonCode) {
  31.         // Connection failed
  32.     }
  33. });
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement