First we start The Appcelerator Studio and create a new module. After this we open CLI, goes to the android folder in it and modify build.properties.
In folder android/src/de-appwerft.wifimanager we see two files: Wifimanager.java and ExampleProxy.java
We open the module file and strip all unused parts. In header of class we put all constants from original api description. After this we rename ExampleProxy.java to WifiManagerProxy.java
After stripping of unsed stuff we see this code:
package de.appwerft.wifimanager;
@Kroll.proxy(creatableInModule = WifimanagerModule.class)
public class WifiManagerProxy extends KrollProxy {
// Standard Debugging variables
private static final String LCAT = WifimanagerModule.LCAT;
private WifiManager manager;
// Constructor
public WifiManagerProxy() {
super();
}
// Handle creation options
@Override
public void handleCreationDict(KrollDict options) {
super.handleCreationDict(options);
init();
}
}
The meaning of this proxy is to expose native classes. In our case it is WifiManager. Therefore we add a private variable manager
from type WiFiManager
If the Javascript layer calls Module.createWifiManager()
, then internally the class will instanciated by new
. The constructor parameters doesn't run in constructor, for this is a special method handleCreationDict
. After this we call a private method init()
.
##Init()
This "constructor" create a new instance of manager:
manager = (WifiManager) TiApplication.getInstance().getSystemService(Context.WIFI_SERVICE);
##The trick
The javascript call createWifiManager()
returns a proxy and a property of this proxy is the native object. The proxy is quasi a "container" for native objects.
##getConfiguredNetworks() For usage of this native methode we write a Kroll.method:
@Kroll.method
public List<WifiConfiguration> getConfiguredNetworks() {
List<WifiConfiguration> = manager.getConfiguredNetworks();
}
manager
is our native object and we can cann this native methode on it. But what is with result value List<WifiConfiguration>
? Because we need a javascript reference for it, we need a proxy WifiConfigurationProxy
too.
And we modify the method to:
@Kroll.method
public List<WifiConfigurationProxy> getConfiguredNetworks() {
List<WifiConfigurationProxy> proxylist = new ArrayList<WifiConfigurationProxy>();
for (WifiConfiguration conf : manager.getConfiguredNetworks()) {
proxylist.add(new WifiConfigurationProxy(conf));
}
return proxylist;
}
It is the same trick as before: we pack the WifiConfiguration
in a WifiConfigurationProxy
and returns to JS layer. Now we could call methods on native object WifiConfiguration. For this we need a
WifiConfigurationProxywith
WifiConfiguration` as constructor parameter:
public class WifiConfigurationProxy extends KrollProxy {
private static final String LCAT = WifimanagerModule.LCAT;
private WifiConfiguration conf;
public WifiConfigurationProxy(WifiConfiguration conf) {
super();
this.conf = conf;
}
}