一、首先在工程选项那里设置获取WIFI的权限,如图:
不然会出现信息为
java.lang.SecurityException:
WifiService:Neither user 10065
nor current process has
android.permission.
ACCESS_WIFI_STATE.
的对话框。
二、包含以下头文件:
#include <Androidapi.JNI.Net.hpp>
#include <Androidapi.Helpers.hpp>
#include <Androidapi.JNI.JavaTypes.hpp>
#include <Androidapi.JNI.GraphicsContentViewText.hpp>
#include <Androidapi.JNIBridge.hpp>
三、上例程
//--------------------------------------------------------------------------- #include <fmx.h> #pragma hdrstop #include "Unit2.h" #include <Androidapi.JNI.Net.hpp> #include <Androidapi.Helpers.hpp> #include <Androidapi.JNI.JavaTypes.hpp> #include <Androidapi.JNI.GraphicsContentViewText.hpp> #include <Androidapi.JNIBridge.hpp> //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm2 *Form2; //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { _di_JObject WifiManagerObj; _di_JWifiManager WifiManager; _di_JWifiInfo WifiInfo; WifiManagerObj = SharedActivityContext()->getSystemService(TJContext::JavaClass->WIFI_SERVICE); if( WifiManagerObj !=NULL ) { WifiManager = TJWifiManager::Wrap( static_cast<_di_ILocalObject>(WifiManagerObj)->GetObjectID()); WifiInfo = WifiManager->getConnectionInfo(); Memo1->Lines->Add(JStringToString(WifiInfo->getSSID())); //显示SSID Memo1->Lines->Add(JStringToString(WifiInfo->getMacAddress())); //显示手机MAC地址 } } //---------------------------------------------------------------------------
四、_di_是什么意思?
JObject / JWifiManager / JWifiInfo 是 Delphi 的接口 。
_di_JObject / _di_JWifiManager / _di_JWifiInfo 是C++的接口。