Sunday, October 14, 2012

Talking to an Android phone through Wi-Fi

In this system, the master controller sends its serial data to a laptop through a USB-to-Serial converter.  The laptop is responsible for creating a number of colorful animated displays from this data. Now I am interested to send this same data to an Android phone that can create its own custom displays. This can be done either through Wi-Fi or Bluetooth.

I want to implement both, by double-splitting the UART Tx line to a RN-XV Wifly Module, and to a BlueSMiRF Bluetooth Modem.



In this post, I will discuss the Wi-Fi route.

The goal is to establish a peer-to-peer adhoc connection between the Wifly module and the phone, and to use UDP packets to keep a low latency in the transmission of live 10 Hz data. Since Android 4.0, it is possible to programmatically configure the phone as an Access Point (AP) visible to nearby Wi-Fi modules (including those integrated in most modern laptops and computers).

There is currently a limitation to this approach (but see UPDATE below!). In creating the AP, the Android system assigns a random SSID and passphrase to it, and it is not possible to edit these values The Wifly module must have an advanced knowledge of these values in order to connect to the phone. So the AP must first be created by the phone, and its details made visible in order to configure the Wifly module.

I have created a simple app tool to assist in this task. It is available on Google Play under the name ‘Wi-Fi P2P Access Point’. I have set up a small fee to refrain people to install it without having a real interest and knowledge of what it is designed for.

With this application, you can create an access point that will live as long as Wi-Fi is enabled on the phone, or until you manually remove it in the app, or until the Android system arbitrarily decides to destroy it to reclaim resources.



Here is a simple way to test if the AP actually exists. After you create an AP, your laptop will automatically list it as an available network, and if you want to connect, you will have to provide the passphrase. Once connected, nothing more happens because there is no data flow on the connection.

Avoid rapidly creating and removing APs without some pause between the commands. The app will become unresponsive and you may have to disable and re-enable Wi-Fi.

In coming posts, as an intermediate step, we will look at:

- how to configure the Wifly module to send GPS data to the phone;

- how to develop a custom app than can display these data.


UPDATE: With Android 4.2.1, the above limitation seems to have disappeared! The SSID and passphrase remain the same when the access point is removed and recreated.


14 comments:

  1. Hi, i am interest in developing some like that...this android app require that the phone or tablet is root ?

    ReplyDelete
    Replies
    1. Root is not required. Tested only with Android 4.1, but should also work with Android 4.0 (not confirmed yet).

      Delete
    2. Hi, you make this application with Wifi Direct API?
      I am developing a system that send data from medical sensor across wifly rn-xv module and I´m looking now how comunicate with my tablet android 4.0.3 to graph signals in real-time. If you will can give me information about this and help me I would be very grateful, and if you app is useful for me, I will buy it.
      Thanks.

      Delete
    3. Yes I used the Wi-Fi Direct API, specifically the 'android.net.wifi.p2p' package with the class WifiP2pManager and functions createGroup(), removeGroup, requestGroupInfo(), requestConnectionInfo() and associated interfaces. I may publish the code at some point if time permits. It is my understanding that future Android releases may allow editing the SSID and passphrase after creation, so that we could choose permanent well-known values for the configuration of the Wifly module.

      Delete
    4. Thanks for you information. With this app that work with WIFI Direct, the RN-XV module how recognize the network...like ad hoc or normal acces point? You send data in your system across UDP or TCP/IP?
      Thanks

      Delete
    5. In a next post, I will document how to configure the RN-XV module to communicate in ad-hoc mode with the phone. For my application, I will use UDP packets because I don't want any delay in the transmission of live data.

      Delete
  2. Hi, I buyed and install your aplication of google play and was very useful for my proyect, thanks for create it. I will want ask you why the SSID and phrase is created automatically...no way to set own SSID?

    ReplyDelete
    Replies
    1. Unfortunately, no. This is limitation of the current release of Android. Let's hope that future releases will allow the edition of the SSID and passphrase values.

      Delete
    2. UPDATE: With Android 4.2.1, the above limitation seems to have disappeared! The SSID and passphrase remain the same when the access point is removed and recreated.

      Delete
  3. Hi,

    Thanks for your post and the comment that led me to it: http://stackoverflow.com/questions/4098615/connect-an-android-phone-to-the-wifly-board

    I found that Roving Networks have updated their module software and version 2.45 allows the WiFly to create an access point that is visible to Android devices (and the other platforms too). The "Soft AP" allows up to seven devices to connect to it. As a test, I used the Soft AP to distribute GPS data to 3 Android devices, a PC running portsniffer and an iPod.

    Regards,
    Neil

    ReplyDelete
  4. Hello - looking for a code sample for Android 4.0 to connect to a device via wifi ad-hoc. Do you know where I can find it? Or do you have a code snippet for the connection part? Thanks in advance. Werner

    ReplyDelete
    Replies
    1. I don't have specific knowledge of alternate methods (for adhoc mode) other that the one that I present here, where an access point is created on the phone beforehand. When using this method with a Wifly board, the connection code on the device side (Wifly) is described in the following post:
      http://sailboatinstruments.blogspot.ca/2012/11/gps-data-from-wifly-to-android-phone.html
      The connection code on the phone side (which require a separate thread) is described in:
      http://sailboatinstruments.blogspot.ca/2012/11/gps-data-from-wifly-to-android-phone_28.html

      Delete
  5. i bought link sprite wifi to uart module.i installed wifi p2p access point App in my android phone. My laptop and other wifi enabled phone can scan n detect my ssid but the wifi module is not able to detect it.

    please help me..!!

    ReplyDelete
    Replies
    1. I am not familiar with this Linksprite module, you may first check if it detects your home network.

      Delete