BY Annie Abraham16 Oct 2017Tizen .NET Application, Tizen .NET
This blog explains how to create a simple Wi-Fi demo application using the Tizen Wi-Fi APIs, and also to design the application UI using XAML.
This simple Wi-Fi demo application demonstrates how to enable and disable Wi-Fi, also how to scan and connect to an open network Access Point (AP). The Wi-Fi APIs used in this app are part of the Tizen C# Native APIs.
Prerequisites
The content presented in this blog is based on the assumption that you understand the structure of Tizen .NET application, and how to design the UI using XAML file. If not, refer to the Drumpad application.
Steps
The following are the steps involved in creating the Wi-Fi demo application:
Using the above code snippet, you can create the following UI elements:
Button “Enable” to enable Wi-Fi
Button “Disable” to disable Wi-Fi
Label “Scan Access Points (AP)” and a button “Scan” to scan for access points.
Label with text “Tap on the box below to select AP”
Picker to select the required access point.
Button “Connect” to connect to a specific access point.
MainPage.xaml has a corresponding MainPage.xaml.cs file where the events can be handled when the user clicks the Enable, Disable, Scan, and Connect buttons. Set MainPage as the Main Page of the application in WiFiDemo.cs by setting MainPage as “MainPage = new MainPage()” in the App constructor.
The following is the screenshot of the application:
Binding Button Click Events
Now that the UI is created, you must implement the Wi-Fi functionality. In the MainPage.xaml.cs file, after the InitializeComponent() function, add event handlers for the Picker, Enable,Disable, Scan, and Connect buttons as shown in the following code example:
Implement the Connect button click event and also to get the selected AP from the picker list.
void OnAPSelect(object sender, EventArgs e)
{
Log.Debug("WIFITESTLOG", "onapselect, ap info");
if (picker.SelectedIndex > 0)
{
if (_device != null)
_device.Dispose();
_device = new WiFiAP(picker.Items[picker.SelectedIndex]);
cell.Text = "Device " + _device.NetworkInformation.Essid + " is selected";
Log.Debug("WIFITESTLOG", "onapselect, ap info =" + _device.NetworkInformation.Essid + ", " + _device.NetworkInformation.ConnectionState);
}
else
{
cell.Text = "Device is not selected";
}
}
async void OnConnectClicked(object sender, EventArgs e)
{
WiFiManager.BackgroundScanFinished -= ScanFinished;
if (!WiFiManager.IsActive)
{
cell.Text = "Enable Wi-Fi";
return;
}
else
{
if (_device != null)
{
cell.Text = "Connecting to " + _device.NetworkInformation.Essid;
WiFiManager.ConnectionStateChanged += StateChanged;
await _device.ConnectAsync();
await Task.Delay(4000);
}
else
{
cell.Text = "Device not found cannot connect";
return;
}
}
}
The Wi-Fi connection status can be checked using the following code:
void StateChanged(Object sender, ConnectionStateChangedEventArgs e)
{
Log.Debug("WIFITESTLOG", "StateChanged");
if (e.State == WiFiConnectionState.Connected)
cell.Text = "Device connected " + e.AP.NetworkInformation.Essid.ToString();
if (e.State == WiFiConnectionState.Disconnected)
cell.Text = "Deivce is not connected";
}
Adding the Privilege
The Wi-Fi APIs require few privileges. This Tizen privilege enables the application to use the Wi-Fi APIs. You can add this privilege to the tizen-manifest.xml file. To do so, perform the following steps:
In the Solution Explorer pane, go to WiFiDemo.Tizen project > tizen-manifest.xml.
Right-click the tizen-manifest.xml file and click Open.
Click Privileges > Add and select Custom Privileges.
Developing Tizen .NET Application with Wi-Fi
This blog explains how to create a simple Wi-Fi demo application using the Tizen Wi-Fi APIs, and also to design the application UI using XAML.
This simple Wi-Fi demo application demonstrates how to enable and disable Wi-Fi, also how to scan and connect to an open network Access Point (AP). The Wi-Fi APIs used in this app are part of the Tizen C# Native APIs.
Prerequisites
The content presented in this blog is based on the assumption that you understand the structure of Tizen .NET application, and how to design the UI using XAML file. If not, refer to the Drumpad application.
Steps
The following are the steps involved in creating the Wi-Fi demo application:
Creating the Project
First, you need to create a Tizen Xamarin.Forms Single application. To do so, perform the following steps:
The New Project screen is displayed.
Creating UI for the Application
Now that you have created the project, use the XAML to create the UI. For information on XAML, refer to eXtensible Application Markup Language (XAML).
To create the UI, perform the following:
Using the above code snippet, you can create the following UI elements:
MainPage.xaml has a corresponding MainPage.xaml.cs file where the events can be handled when the user clicks the Enable, Disable, Scan, and Connect buttons. Set MainPage as the Main Page of the application in WiFiDemo.cs by setting MainPage as “MainPage = new MainPage()” in the App constructor.
The following is the screenshot of the application:
Binding Button Click Events
Now that the UI is created, you must implement the Wi-Fi functionality. In the MainPage.xaml.cs file, after the InitializeComponent() function, add event handlers for the Picker, Enable, Disable, Scan, and Connect buttons as shown in the following code example:
Implement the Enable and Disable button click events as shown:
Implement the Scan button click event to scan for the Wi-Fi access points:
Implement the Connect button click event and also to get the selected AP from the picker list.
The Wi-Fi connection status can be checked using the following code:
Adding the Privilege
The Wi-Fi APIs require few privileges. This Tizen privilege enables the application to use the Wi-Fi APIs. You can add this privilege to the tizen-manifest.xml file. To do so, perform the following steps:
Running the Application
Press CTRL+F5 to launch the application on the device/emulator.
To connect to an access point, follow the steps below:
Note: This demo app allows you to connect only with an open network access point. To disable the Wi-Fi, tap “Disable”.
For more details, refer to the attached source.
BY Annie Abraham
24 Sep 2021
Tizen Studio, Tizen .NET Application
BY Rishu Mehra
30 Jun 2020
Tizen .NET, Tizen Studio, Tizen
BY Reni Mathew
25 Jun 2020
Tizen .NET Application, Tizen .NET