9.5 Writing the Android Client

Writing code for the Android client to send unlock commands to the Android server is easy. Let’s reuse code from the Web Enabled Light Switch Android client application to provide easy user access to the door latch function. This time, we will use a button instead of a toggle switch since we already programmed the lock to unlock for five seconds. This makes the toggle unnecessary. Another feature we will add to this application is to turn on the Wi-Fi radio if it isn’t already active.

The basic flow of the program will be to launch it and check for Wi-Fi access. If the Wi-Fi radio is turned off, turn it on and wait for the client to connect to the network. Allow the user to press the displayed Unlock Door button, which will access the Android Door Lock server URL and unlock the door. Briefly, here are the steps we will take to code the unlock client:

  1. Create an Android project in Eclipse called DoorLockClient.

  2. Check if the Wi-Fi radio is on in the program’s main activity. If Wi-Fi is turned off, activate it.

  3. Add a Button to the main.xml layout description and label it “Unlock Door.”

  4. Reference the button in the DoorLock class and a listener for the button press event. If the Wi-Fi radio is being turned on for the first time when the program starts, keep the Unlock Door button disabled for a few seconds to allow the Wi-Fi interface to authenticate with the wireless access point and establish the client’s IP address.

  5. Add the URL request call in the button press event to the Android Door Lock server (ex: 192.168.1.230).

We will start by following the same procedure used in Section 7.6, Writing the Code for the Android Client. Create a new Android project in Eclipse using the parameters shown in Figure 42, Settings for the new Door Lock Client application.

/epubstore/R/M-Riley/Programming-your-home//images/doorlockclientandroid.png

Figure 42. Settings for the new Door Lock Client application

Add a button called unlockbutton, label its text “Unlock Door,” and set the button’s width to fill the LinearLayout of the parent container. The main.xml file should look like this:

  ​ <?xml version="1.0" encoding="utf-8"?>​
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  ​ android:orientation="vertical"
  ​ android:layout_width="fill_parent" android:layout_height="fill_parent" >
  <Button android:id="@+id/unlockbutton" android:layout_height="wrap_content"
  ​ android:text="Unlock Door" android:layout_width="fill_parent" > </Button>
  </LinearLayout>

Save the changes. Open the DoorLockClient.java file and add references for the unlockbutton Button element and its event listener. Also add Wi-Fi radio detection and activation. The full listing for the DoorLockClient.java class should look like this:

AndroidDoorLock/DoorLockClient/src/com/mysampleapp/doorlockclient/DoorLockClient.java
  package com.mysampleapp.doorlockclient;​
  ​​
  import java.io.InputStream; ​
  import java.net.URL;​
  import android.net.wifi.WifiManager;​
  import android.widget.Button;​
  import android.app.Activity;​
  import android.os.Bundle;​
  import android.util.Log;​
  import android.view.View;​
  ​​
  public class DoorLockClient extends Activity {​
  /** Called when the activity is first created. */
  ​ @Override​
  public void onCreate(Bundle savedInstanceState) {​
  ​ super.onCreate(savedInstanceState);​
  ​ setContentView(R.layout.main);​
  Button unlockbutton = (Button) findViewById(R.id.unlockbutton); ​
  ​ findViewById(R.id.unlockbutton).setOnClickListener​
  ​ (mClickListenerUnlockButton);​
  try {​
  ​ WifiManager wm =​
  ​ (WifiManager) getSystemService(WIFI_SERVICE); ​
  if (!wm.isWifiEnabled()) {​
  ​ unlockbutton.setEnabled(false);​
  ​ wm.setWifiEnabled(true);​
  // Wait 17 seconds for Wi-Fi to turn on and connect
  Thread.sleep(17000);​
  ​ unlockbutton.setEnabled(true);​
  ​ }​
  ​ } catch (Exception e) {​
  ​ Log.e("LightSwitchClient", "Error: " + e.getMessage(), e);​
  ​ }​
  ​​
  ​ }​
  View.OnClickListener mClickListenerUnlockButton =​
  new View.OnClickListener() { ​
  public void onClick(View v) {​
  try {​
  final InputStream is =​
  new URL("http://192.168.1.230:8000").openStream(); ​
  ​ }​
  catch (Exception e) {​
  ​ }​
  ​ }​
  ​ };​
  ​}​

Import the library references for java.io.InputStream, java.net.URL and Android-specific android.widget.Button and android.net.wifi.WifiManager.

Add a reference for the unlockbutton Button and assign it to the mClickListenerUnlockButton View method.

Query the state of the Wi-Fi radio, and if it’s not active, turn the Wi-Fi radio on. Keep the unlockbutton disabled for seventeen seconds to allow enough time for the network connection to initialize.

Create the View.OnClickListener for the unlockbutton.

Request the Android door lock server address when the unlockbutton is clicked.

We have one more task to complete before we can test out the application. Remember how we had to set permission to access the network for the Web Enabled Light Switch Android client? We have to do the same thing for this Door Lock Client. We also have to grant permission to access the state of the Wi-Fi radio as well. These permissions are noted in the AndroidManifest.xml file, which should look like this once these two permissions are added:

  ​<?xml version="1.0" encoding="utf-8"?>​
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  ​ package="com.mysampleapp.doorlockclient"
  ​ android:versionCode="1"
  ​ android:versionName="1.0" >
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  ​​
  <application android:icon="@drawable/icon" android:label="@string/app_name" >
  <activity android:name=".DoorLockClient"
  ​ android:label="@string/app_name" >
  <intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  </activity>
  </application>
  </manifest>

Save the project and test it using an available Android phone. First test its operation with the Wi-Fi radio turned on. The button should be instantly accessible after the program has launched. Quit the program, preferably using a task manager (i.e., make sure the running instance of the program is destroyed and not running silently in the background). Next, turn off the Wi-Fi radio and launch the Door Lock Client application again. This time, the Unlock Door button will be disabled while the program turns on the Wi-Fi radio and waits until a connection with the network has been established. If the radio turned on, you’re ready for a live test with the door lock server.

Click the Unlock Door button. Within a second or two, the electric lock should click open and then close approximately five seconds later. If it did, congratulations on a job well done! If it didn’t, verify that your Android device is indeed connected to the network. Test the URL access via the Android web browser. If you can’t access the URL, make sure the Android door lock server is still set to the static IP we defined earlier and that it is running. Try accessing the URL from a different system just to verify that the rest of your home network can access the Android door lock server.

Programming Your Home
cover.xhtml
f_0000.html
f_0001.html
f_0002.html
f_0003.html
f_0004.html
f_0005.html
f_0006.html
f_0007.html
f_0008.html
f_0009.html
f_0010.html
f_0011.html
f_0012.html
f_0013.html
f_0014.html
f_0015.html
f_0016.html
f_0017.html
f_0018.html
f_0019.html
f_0020.html
f_0021.html
f_0022.html
f_0023.html
f_0024.html
f_0025.html
f_0026.html
f_0027.html
f_0028.html
f_0029.html
f_0030.html
f_0031.html
f_0032.html
f_0033.html
f_0034.html
f_0035.html
f_0036.html
f_0037.html
f_0038.html
f_0039.html
f_0040.html
f_0041.html
f_0042.html
f_0043.html
f_0044.html
f_0045.html
f_0046.html
f_0047.html
f_0048.html
f_0049.html
f_0050.html
f_0051.html
f_0052.html
f_0053.html
f_0054.html
f_0055.html
f_0056.html
f_0057.html
f_0058.html
f_0059.html
f_0060.html
f_0061.html
f_0062.html
f_0063.html
f_0064.html
f_0065.html
f_0066.html
f_0067.html
f_0068.html
f_0069.html
f_0070.html
f_0071.html
f_0072.html
f_0073.html
f_0074.html
f_0075.html
f_0076.html
f_0077.html
f_0078.html
f_0079.html
f_0080.html
f_0081.html
f_0082.html
f_0083.html
f_0084.html
f_0085.html
f_0086.html
f_0087.html
f_0088.html
f_0089.html
f_0090.html
f_0091.html
f_0092.html
f_0093.html
f_0094.html
f_0095.html
f_0096.html
f_0097.html
f_0098.html
f_0099.html
f_0100.html
f_0101.html
f_0102.html
f_0103.html
f_0104.html
f_0105.html
f_0106.html
f_0107.html
f_0108.html
f_0109.html
f_0110.html
f_0111.html
f_0112.html
f_0113.html
f_0114.html
f_0115.html
f_0116.html
f_0117.html
f_0118.html
f_0119.html
f_0120.html
f_0121.html
f_0122.html
f_0123.html
f_0124.html
f_0125.html