-1

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;
        }

    }
}
Hemangi Pithava
  • 138
  • 1
  • 7
David Innocent
  • 606
  • 5
  • 16

1 Answers1

0

That has to be done over the Wifi P2P - Wifi Direct Network Discovery API in Android API 14+.

You can find the sample code here

https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

Shadab K
  • 1,677
  • 1
  • 16
  • 25