问题
i tried to covert the wifimanager and wifiinfo (android) and i don't understand why i had segmentation fault, i saw that wifimanager worked well, but when i try to call some methods of wifiinfo i get "segmentation fault". my code:
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.
//-----------------------------------------------------------------------
and my test is :
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;
what can be wrong with this? (the methods of wifimanager works but not getconnectioninfo )
ORIGINAL JAVA POST: Detect wifi IP address on Android?
LOGCAT when i call this function:
and this is the project in delphi: PROJECT DELPHI
回答1:
What is primarily wrong with it is that lack of checking for nil values, which consequently leads to your segmentation fault (at least I'm expecting this to be the case - wm being nil, for example).
You can try referring to the code snippet in this SO answer as a general guide to working with the Android network classes. That should help for now, in lieu of a direct pointer to the error in your code. Try that code out and ensure you understand how it operates. It should then help you identify what is errant in your snippet.
One potential problem with the code is the attributes are using dot-separators, but the RTL Android class attributes, along with the aforementioned snippet, use / as a separator.
Judicious use of logcat (say from within the monitor app) would help identify if this was the case by seeing what the OS throws out as log messages for the period this app executes.
Oh, and one thing that should have been checked first - have you added the Access wifi state permission to your app? That will cause issues with, erm, accessing wifi state information, natch.
回答2:
why did you use "Winfo:=TJWifiInfo.Create;"?!...you do not need to create it, it is the getconnectioninfo function that returns a jwifiinfo instance. Why not simply using "Winfo:=wm.getconnectioninfo;"
来源:https://stackoverflow.com/questions/19794024/delphi-xe5-wifimanager-and-wifiinfo-coversion