I have the following code to connect to another phone's hotspot wifi.
The idea is to have the two devices send each other simple data; when on each end, the phones are connected to my app. Then the app responds to the data that was sent.
private void ConnectWifi(String networkSSID, String networkPass) {
List<ScanResult> scanResult = wifiManager.getScanResults();
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
String lstItems = new String();
for (ScanResult scanResults : scanResult) {
lstItems += "\n" + scanResults.SSID;
if (scanResults.capabilities.contains("WEP")) {
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (scanResults.capabilities.contains("WPA")) {
conf.preSharedKey = "\"" + networkPass + "\"";
} else {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
}
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("KEBWARO 3")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
//subscriberData.setNetworkID(i.SSID);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setIcon(R.drawable.beer);
alert.setTitle("CONNECTED SUCCESSFULLY");
alert.setMessage("CONNECTED TO " + i.SSID);
alert.show();
networkID = i.SSID;
}
}
}