Mobile native Wearable native

Wi-Fi: Managing Wi-Fi and Monitoring Its State

This tutorial demonstrates how you can manage Wi-Fi connections and monitor the state of Wi-Fi.

Warm-up

Become familiar with the Wi-Fi API basics by learning about:

Initializing Wi-Fi

To use the functions and data types of the Wi-Fi API (in mobile and wearable applications), include the <wifi.h> header file in your application:

#include <wifi.h>

To be able to use all Wi-Fi functions, initialize Wi-Fi using the wifi_initialize() function:

int error_code;

error_code = wifi_initialize();
if (error_code != WIFI_ERROR_NONE)
   return;

When Wi-Fi is no longer needed or the application is exiting, release Wi-Fi:

wifi_deinitialize();

Activating a Wi-Fi Device

To activate and deactivate a local Wi-Fi device, and to check that Wi-Fi is activated:

  1. Activate a Wi-Fi device using the wifi_activate() function. After the wifi_activate() function is completed, the __wifi_activated_cb() function is invoked.
    error_code = wifi_activate(__wifi_activated_cb, NULL);
    

    Define the __wifi_activated_cb() function for a callback. In case of Wi-Fi device activation, an information message is printed using dlog util (or shown to the user in another way).

    static void __wifi_activated_cb(wifi_error_e result, void *user_data) 
    {
       if (result == WIFI_ERROR_NONE)
          dlog_print(DLOG_INFO, LOG_TAG, "Success to activate Wi-Fi device!");
    }
    
  2. Check the Wi-Fi connection using the wifi_is_activated() function. The parameter indicates, whether Wi-Fi is activated.
    bool wifi_activated = false;
    wifi_is_activated(&wifi_activated);
    if (wifi_activated)
    {
       dlog_print(DLOG_INFO, LOG_TAG, "Success to get Wi-Fi device state.");
    }
    else 
    {
       dlog_print(DLOG_INFO, LOG_TAG, "Fail to get Wi-Fi device state.");
    }
    

Scanning for Access Points

To scan nearby access points, and after scanning, to print the result of the scan, such as the AP name and state:

  1. Scan nearby access points asynchronously. When the scan is finished, the __scan_request_cb() function is called.
    wifi_scan(__scan_request_cb, NULL);
    
  2. Define a callback function to be called when the scan is finished. In this tutorial, the callback is the __scan_request_cb() function. It invokes the wifi_foreach_found_aps() function for getting information on the found AP. The wifi_foreach_found_aps() function gets the result of the scan. The __wifi_found_ap_cb() function is called when you get the found access point repeatedly.
    void __scan_request_cb(wifi_error_e error_code, void *user_data)
    {
       error_code = wifi_foreach_found_aps(__wifi_found_ap_cb, NULL);
       if (error_code != WIFI_ERROR_NONE) 
          dlog_print(DLOG_INFO, LOG_TAG, "Fail to scan");
    }
    
  3. Show the result of the scan using the __wifi_found_ap_cb() callback function, which is invoked by the wifi_foreach_found_aps() function. In this tutorial, print the AP name and connection state.
    bool __wifi_found_ap_cb(wifi_ap_h ap, void *user_data) 
    {
       int error_code = 0;
       char *ap_name = NULL;
       wifi_connection_state_e state;
    
    
       error_code = wifi_ap_get_essid(ap, &ap_name);
       if (error_code != WIFI_ERROR_NONE) 
       {
          dlog_print(DLOG_INFO, LOG_TAG, "Fail to get AP name.");
    
          return false;
       }
       error_code = wifi_ap_get_connection_state(ap, &state);
       if (error_code != WIFI_ERROR_NONE) 
       {
          dlog_print(DLOG_INFO, LOG_TAG, "Fail to get state.");
    
          return false;
       }
       dlog_print(DLOG_INFO, LOG_TAG, "AP name : %s, state : %s", ap_name, print_state(state));
    
       return true;
    }
    
    static const char* print_state(wifi_connection_state_e state) 
    {
       switch (state) 
       {
       case WIFI_CONNECTION_STATE_DISCONNECTED:
          return "Disconnected";
       case WIFI_CONNECTION_STATE_ASSOCIATION:
          return "Association";
       case WIFI_CONNECTION_STATE_CONNECTED:
          return "Connected";
       case WIFI_CONNECTION_STATE_CONFIGURATION:
          return "Configuration";
       }
    }
    

    You can get other information, including frequency, IP address, and security type. For more information, see the Wi-Fi guide.

Connecting to a Specific Access Point

To make a connection using a specific access point:

  1. Select an access point.

    Check whether Wi-Fi is activated using the wifi_is_activated() function, and then receive the specific AP name from the user. Call the wifi_foreach_found_aps() function to compare the AP name with the result of the scan and connecting to the AP.

    char ap_name[33];
    bool state = false;
    
    wifi_is_activated(&state);
    if (state == false)
       return -1;
    dlog_print(DLOG_INFO, LOG_TAG, "Input a part of AP name to connect : ");
    error_code = scanf("%32s", ap_name);
    
    error_code = wifi_foreach_found_aps(__found_connect_ap_cb, ap_name);
    if (error_code != WIFI_ERROR_NONE) 
    {
       dlog_print(DLOG_INFO, LOG_TAG, "Fail to connect (can’t get AP list)");
    
       return -1;
    }
    
    dlog_print(DLOG_INFO, LOG_TAG, "Connection step finished");
    
  2. Make a connection with an access point.

    The wifi_foreach_found_aps() function called above invokes a callback function, which is __found_connect_ap_cb(). Define the __found_connection_ap_cb() callback. This function compares user_data (the AP name from the user input) with the name of the found AP. If it is correct, the function checks whether the AP requires a passphrase. Set the passphrase using the wifi_ap_set_passphrase() function.

    Finally, connect to a specific AP using the wifi_connect() function.

    static bool __found_connect_ap_cb(wifi_ap_h ap, void *user_data) 
    {
       int error_code = 0;
       char *ap_name = NULL;
       char *ap_name_part = (char*)user_data;
    
       error_code = wifi_ap_get_essid(ap, &ap_name);
       if (error_code != WIFI_ERROR_NONE) 
       {
          dlog_print(DLOG_INFO, LOG_TAG, "Fail to get AP name");
    
          return false;
       }
    
       if (strstr(ap_name, ap_name_part) != NULL) 
       {
          bool required = false;
    
          if (wifi_ap_is_passphrase_required(ap, &required) == WIFI_ERROR_NONE)
             dlog_print(DLOG_INFO, LOG_TAG, "Passphrase required: %s", required ? "True" : "False");
          else
             dlog_print(DLOG_INFO, LOG_TAG, "Fail to get Passphrase required");
    
          if (required) 
          {
             char passphrase[100];
             dlog_print(DLOG_INFO, LOG_TAG, "Input passphrase for %s :", passphrase);
             error_code = scanf("99%s", passphrase);
    
             error_code = wifi_ap_set_passphrase(ap, passphrase);
             if (error_code != WIFI_ERROR_NONE) 
             {
                dlog_print(DLOG_INFO, LOG_TAG, "Fail to set passphrase");
    
                return false;
             }
          }
    
          error_code = wifi_connect(ap, __connected_cb, NULL);
          if (error_code != WIFI_ERROR_NONE)
             dlog_print(DLOG_INFO, LOG_TAG, "Fail to connection request");
          else
             dlog_print(DLOG_INFO, LOG_TAG, "Success to connection request");
    
          free(ap_name);
    
          return false;
       }
    
       free(ap_name);
    
       return true;
    }
    
  3. Provide a notification about the result of the connection.

    The wifi_connect() function called earlier invokes the __connected_cb() function. With this function, you can provide the result of the connection.

    static void __connected_cb(wifi_error_e result, void* user_data) 
    {
       if (result == WIFI_ERROR_NONE)
          dlog_print(DLOG_INFO, LOG_TAG, "Wi-Fi Connection Succeeded");
       else
          dlog_print(DLOG_INFO, LOG_TAG, "Wi-Fi Connection Failed!");
    }
    

Deactivating the Wi-Fi Device

To power off the Wi-Fi device when Wi-Fi is no longer needed (or the application is exiting), use the wifi_deactivate() function:

wifi_deactivate(NULL, NULL);
Go to top