我试图隐藏 wifimanager 和 wifiinfo(android),但我不明白为什么我有分段错误,我看到 wifimanager 运行良好,但是当我尝试调用 wifiinfo 的某些方法时,我得到“分段错误”。我的代码:
http://developer.android.com/reference/android/net/wifi/WifiInfo.html http://developer.android.com/reference/android/net/wifi/WifiManager.html
unit wifi1;
interface
uses
System.SysUtils,
Androidapi.JNIBridge,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
FMX.Helpers.Android;
//-------------------------------- wifi manager ---------------------------------------//
type
JWifiManager = interface;
JWifiInfo = interface;
JWifiManagerClass = interface(JObjectClass)
['{69F35EA7-3EB9-48AA-B7FC-4FFD0E7D712F}']
function _GetACTION_PICK_WIFI_NETWORK: JString;
function _GetEXTRA_WIFI_INFO: JString;
function _GetWIFI_STATE_CHANGED_ACTION: JString;
property ACTION_PICK_WIFI_NETWORK: JString read _GetACTION_PICK_WIFI_NETWORK;
property EXTRA_WIFI_INFO: JString read _GetEXTRA_WIFI_INFO;
property WIFI_STATE_CHANGED_ACTION: JString read _GetWIFI_STATE_CHANGED_ACTION;
end;
[JavaSignature('android.net.wifi.WifiManager')]
JWifiManager = interface(JObject)
['{DA7107B9-1FAD-4A9E-AA09-8D5B84614E60}']
function isWifiEnabled:Boolean;cdecl;
function setWifiEnabled(enabled:Boolean):Boolean; cdecl;
//function getConfiguredNetworks : JList;cdecl;
function getConnectionInfo :JWifiInfo; cdecl;
end;
TJWifiManager = class(TJavaGenericImport<JWifiManagerClass, JWifiManager>) end;
//-------------------------------- wifi info ---------------------------------------//
JWifiInfoClass = interface(JObjectClass)
['{2B1CE79F-DE4A-40D9-BB2E-7F9F118D8C08}']
function _GetLINK_SPEED_UNITS:JString;
property LINK_SPEED_UNITS: JString read _GetLINK_SPEED_UNITS;
end;
[JavaSignature('android.net.wifi.WifiInfo')]
JWifiInfo = interface(JObject)
['{4F09E865-DB04-4E64-8C81-AEFB36DABC45}']
function getBSSID:jString; cdecl;
function getHiddenSSID:Boolean; cdecl;
function getIpAddress:integer; cdecl;
function getLinkSpeed:integer; cdecl;
function getMacAddress:JString; cdecl;
function getNetworkId:integer; cdecl;
function getRssi:integer; cdecl;
function GetSSID:jString; cdecl;
end;
TJWifiInfo= class(TJavaGenericImport<JWifiInfoClass, JWifiInfo>)
end;
implementation
end.
//-----------------------------------------------------------------------
我的测试是:
var obj:jobject;
wm:jwifimanager;
Winfo:jwifiinfo;
ip:integer;
mac:string;
jmac:JString;
begin
obj:=SharedActivity.getSystemService(TJActivity.JavaClass.WIFI_SERVICE);
wm:= Tjwifimanager.Wrap((obj as ILocalObject).GetObjectID);
Winfo:=TJWifiInfo.Create;
winfo:=wm.getconnectioninfo; <- segmentation fault
ip:=winfo.getIpAddress;
jmac:=winfo.getMacAddress;
mac:=JStringToString(jmac);
end;
这有什么问题?( wifimanager 的方法有效,但 getconnectioninfo 无效)
ORIGINAL JAVA POST:在 Android 上检测 wifi IP 地址?
LOGCAT 当我调用这个函数时:
这是德尔福的项目:PROJECT DELPHI