Reputation: 1371
I want to exchange data over hotspot between two android devices. I've tried to properly connection.
First, I created a portable hotspot:
Network SSID - my_hotspot
Security - WPA PSK
Password - password
I'm attempting to connect when application is launched. Here is my code<
mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.preSharedKey = "\""+ networkPass +"\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
int res = mWifiManager.addNetwork(conf);
boolean b = setWifiApEnabled(null, true);
I assume it was unsuccessful. Then I'm trying sending data via socket. I've learned from JavaCodeGeeks. I configured SERVER_IP 192.168.49.1, SERVER_PORT:8888.
How to communicate correctly between two Android devices using hotspot?
Upvotes: 11
Views: 8800
Reputation: 542
Why don't you use Wi-Fi Direct? It is a p2p protocol. You can share data between two Android devices without any need of hotspot. It uses android.net.wifi.p2p package.
Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later devices with the appropriate hardware to connect directly to each other via Wi-Fi without an intermediate access point (Android's Wi-Fi P2P framework complies with the Wi-Fi Alliance's Wi-Fi Direct™ certification program). Using these APIs, you can discover and connect to other devices when each device supports Wi-Fi P2P, then communicate over a speedy connection across distances much longer than a Bluetooth connection. This is useful for applications that share data among users, such as a multiplayer game or a photo sharing application.
You can find more information on Android developer site about this technology. Here are some useful links:
Upvotes: 3