Author: h.mohri
Using C++Builder 10.2 Tokyo, explain how to switch Wi-Fi on Android.
Table of Contents
Manage Wi-Fi switching using JWifiManager
I use di_JWifiManager which wrapped WifiManager.
di_JWifiManager is the Delphi interface.
Set the variable. _di_JWifiManager f_WifiManager;
use it like this.
Required headers
Header file for using WifiManager.
1 2 3 4 |
<span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"><Androidapi.JNI.Net.hpp></span> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"><Androidapi.Helpers.hpp></span> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"><Androidapi.JNI.GraphicsContentViewText.hpp></span> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"><Androidapi.JNIBridge.hpp></span> |
Display design
It is a design for displaying on Android.
TEdit, TSwitch, TTimer, TLabel, was placed.
When the main form creation
Create _di_JWifiManager using TJWifiManager.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="synComment" style="color:#4f80e5;">//---------------------------------------------------------------------------</span> <span class="synType" style="color:#3ec63e;">void</span> __fastcall Tfm_main_wifistatus::FormCreate(TObject *Sender) { <span class="synComment" style="color:#4f80e5;">//Form creation.Using TAndroidHelper, get the WIFI_SERVICE.</span> _di_JObject obj = TAndroidHelper::Activity->getSystemService(TJContext::JavaClass->WIFI_SERVICE); <span class="synStatement" style="color:#d88a17;">if</span> (obj != <span class="synConstant" style="color:#ff6666;">nullptr</span>) { <span class="synComment" style="color:#4f80e5;">//Wrap to TJWifiManager.</span> f_WifiManager = TJWifiManager::Wrap(_di_ILocalObject(obj)->GetObjectID()); <span class="synStatement" style="color:#d88a17;">if</span> (f_WifiManager != <span class="synConstant" style="color:#ff6666;">nullptr</span>) { Timer1->Enabled = <span class="synConstant" style="color:#ff6666;">true</span>; } } } |
Monitor Wi-Fi status at Timer1 event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="synType" style="color:#3ec63e;">void</span> __fastcall Tfm_main_wifistatus::Timer1Timer(TObject *Sender) { <span class="synComment" style="color:#4f80e5;">//Use a timer event to monitor the Wi-Fi state.</span> <span class="synComment" style="color:#4f80e5;">//Write state change to Edit1->Text. and It also reflects Switch1->IsChecked.</span> Timer1->Enabled = <span class="synConstant" style="color:#ff6666;">false</span>; <span class="synStatement" style="color:#d88a17;">if</span> (f_WifiManager != <span class="synConstant" style="color:#ff6666;">nullptr</span>) { Switch1->IsChecked = f_WifiManager->isWifiEnabled(); UnicodeString wifistr; (Switch1->IsChecked)?wifistr = <span class="synConstant" style="color:#ff6666;">"true"</span>: wifistr = <span class="synConstant" style="color:#ff6666;">"false"</span>; Edit1->Text = <span class="synConstant" style="color:#ff6666;">"f_WifiManager->isWifiEnabled() = "</span> + wifistr; } Timer1->Enabled = <span class="synConstant" style="color:#ff6666;">true</span>; } |
When you tap the Switch1.
Change the Wi-Fi status when tapping the Switch1.
This uses the setWifiEnabled() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="synType" style="color:#3ec63e;">void</span> __fastcall Tfm_main_wifistatus::Switch1Switch(TObject *Sender) { <span class="synComment" style="color:#4f80e5;">//This is a Switch1 change event.</span> Timer1->Enabled = <span class="synConstant" style="color:#ff6666;">false</span>; <span class="synComment" style="color:#4f80e5;">//Set the value of Switch1->IsChecked to setWifiEnabled()</span> f_WifiManager->setWifiEnabled(Switch1->IsChecked); TThread::CreateAnonymousThread([<span class="synStatement" style="color: #d88a17;">this</span>]() { sleep(<span class="synConstant" style="color:#ff6666;">1</span>); TThread::Synchronize(TThread::CurrentThread, [<span class="synStatement" style="color: #d88a17;">this</span>]() { Timer1->Enabled = <span class="synConstant" style="color:#ff6666;">true</span>; }); })->Start(); } |
https://github.com/mojeld/cpp_builder_firemonkey_wifi
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition