C++ Builder XE8 安卓开发之获取WIFI状态

一、首先在工程选项那里设置获取WIFI的权限,如图:

C++ Builder XE8 安卓开发之获取WIFI状态_第1张图片


不然会出现信息为

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++的接口。

你可能感兴趣的:(C++,Wi-Fi,BCB)