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 initialize Wi-Fi:

  1. 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>
    
  2. 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;
    
  3. 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:
    error_code = wifi_activate(__wifi_activated_cb, NULL);
    

    Define the __wifi_activated_cb() callback, which is invoked when the Wi-Fi activation is completed.

    In the following example, the callback prints an information message using the dlogutil tool:

    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.");
    }
    
  3. To deactivate 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);
    

Scanning for Access Points

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

  1. Scan nearby access points asynchronously:
    wifi_scan(__scan_request_cb, NULL);
    
  2. Define a callback, which is invoked when the scan is finished.

    In the following example, the callback calls 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, and the __wifi_found_ap_cb() callback is called for each found access point.

    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.

    In the following example, the callback prints 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 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:

    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.

    Define the __found_connect_ap_cb() callback invoked by the wifi_foreach_found_aps() function.

    The callback 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 connection result.

    The wifi_connect() function called within the __found_connect_ap_cb() callback invokes the __connected_cb() function, which you can use to notify the user of the connection result:

    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!");
    }
    
Go to top