Author: h.mohri
Table of Contents
Using ConnectivityManager with C++Builder
With C++Builder 10.2 Tokyo Android
ConnectivityManager class is available.
Use the ConnectivityManager, you can get NetworkInfo.
NetworkInfo see various network information.
inside that, The getType()
function is Reports the type of network.
The getType()
function return is int.
int | type |
---|---|
0x00000000 | TYPE_MOBILE |
0x00000001 | TYPE_WIFI |
0x00000006 | TYPE_WIMAX |
0x00000009 | TYPE_ETHERNET |
0x00000007 | TYPE_BLUETOOTH |
I tried ways to get NetworkInfo.
Declare _di_JConnectivityManager
variable.
_di_JConnectivityManager
is the ConnectivityManager class interface.
1 |
_di_JConnectivityManager f_ConnectivityManager; |
It creates to use ‘_di_JConnectivityManager’.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="synType" style="color: #3ec63e;">template</span> <<span class="synType" style="color: #3ec63e;">typename</span> T1, <span class="synType" style="color: #3ec63e;">typename</span> T2> <span class="synType" style="color: #3ec63e;">void</span> __fastcall Tfm_main_wifistatus::getService(_di_JObject obj, T1& iobj) { <span class="synStatement" style="color: #d88a17;">if</span> (obj != <span class="synConstant" style="color: #ff6666;">nullptr</span>) { iobj = T2::Wrap(_di_ILocalObject(obj)->GetObjectID()); } } <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 CONNECTIVITY_SERVICE.</span> getService<_di_JConnectivityManager, TJConnectivityManager>( TAndroidHelper::Context->getSystemService(TJContext::JavaClass->CONNECTIVITY_SERVICE), f_ConnectivityManager); } |
Get the _di_JNetworkInfo
using the getActiveNetworkInfo()
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<span class="synComment" style="color: #4f80e5;">//TJConnectivityManager::JavaClass->TYPE_WIFI;</span> <span class="synPreProc" style="color: #9355e6;">#define TYPE_WIFI </span><span class="synConstant" style="color: #ff6666;">0x00000001</span> <span class="synComment" style="color: #4f80e5;">//TJConnectivityManager::JavaClass->TYPE_MOBILE;</span> <span class="synPreProc" style="color: #9355e6;">#define TYPE_MOBILE </span><span class="synConstant" style="color: #ff6666;">0x00000000</span> <span class="synComment" style="color: #4f80e5;">//TJConnectivityManager::JavaClass->TYPE_VPN;</span> <span class="synPreProc" style="color: #9355e6;">#define TYPE_VPN </span><span class="synConstant" style="color: #ff6666;">0x00000011</span> <span class="synComment" style="color: #4f80e5;">//TJConnectivityManager::JavaClass->TYPE_WIMAX;</span> <span class="synPreProc" style="color: #9355e6;">#define TYPE_WIMAX </span><span class="synConstant" style="color: #ff6666;">0x00000006</span> <span class="synType" style="color: #3ec63e;">void</span> __fastcall Tfm_main_wifistatus::Timer1Timer(TObject *Sender) { UnicodeString net_str; _di_JNetworkInfo n_info = f_ConnectivityManager->getActiveNetworkInfo(); <span class="synStatement" style="color: #d88a17;">if</span> (n_info != <span class="synConstant" style="color: #ff6666;">nullptr</span>) { <span class="synStatement" style="color: #d88a17;">switch</span> (n_info->getType()) { <span class="synStatement" style="color: #d88a17;">case</span> TYPE_WIFI: net_str = <span class="synConstant" style="color: #ff6666;">L"(TYPE_WIFI)"</span>; <span class="synStatement" style="color: #d88a17;">break</span>; <span class="synStatement" style="color: #d88a17;">case</span> TYPE_MOBILE: net_str = <span class="synConstant" style="color: #ff6666;">L"(TYPE_MOBILE)"</span>; <span class="synStatement" style="color: #d88a17;">break</span>; <span class="synStatement" style="color: #d88a17;">case</span> TYPE_VPN: net_str = <span class="synConstant" style="color: #ff6666;">L"(TYPE_VPN)"</span>; <span class="synStatement" style="color: #d88a17;">break</span>; } } <span class="synStatement" style="color: #d88a17;">else</span> net_str = <span class="synConstant" style="color: #ff6666;">L"(nullptr)"</span>; } |
You can get the status of a network interface.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition