1

A fundamental question I have been concerned about is whether or not it would be possible to connect to a WiFi network within the Android code, considering the conventional way Google Glass connects to a network with QR codes. If it is possible, my saving grace would be a snippet of code that would allow me to connect to a specified network with SSID and a key. That way I can communicate and send data through sockets or some other method.

For testing purposes I have tried connecting to my laptop's WiFi hotspot before trying to connect to the Arduino, but to no avail. Here is the code in the ConnectActivity.java file so far:

package josuablom.gimbalcontrol;

+import...

public class ConnectActivity extends Activity
{
    private CardScrollView mCardScroller;
    private List<Card> mCards;
    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect);
    }

    public ConnectActivity()
    {

        //Code to connect to wifi
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        // setup a wifi configuration
        WifiConfiguration wc = new WifiConfiguration();
        wc.SSID = "\"JosuaLaptop\"";
        wc.preSharedKey = "\"123456789\"";
        wc.status = WifiConfiguration.Status.ENABLED;
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        // connect to and enable the connection
        int netId = wifiManager.addNetwork(wc);
        wifiManager.enableNetwork(netId, true);
        wifiManager.setWifiEnabled(true);

    }

}

Please bare in mind that I did not write the code in the ConnectActivity() method, but I got it from another thread on stackoverflow, so I do not have a fundamental understanding of how it works, although I can follow the logic.

The permissions in my manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="josuablom.gimbalcontrol" >

<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
...

Please excuse my ignorance, this is part of a undergraduate thesis project, and the focus of my project is the design of a system, not to become a Android expert. I have wasted far too much time trying to figure out how to solve this problem.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Josua B
  • 71
  • 6

0 Answers0