i have experience perl's data structures, , beginning java (on android). need implement these perl data structures:
$beacons = ( { ssid => "north", bssid => "01:02:03:04:05:06", }, { ssid => "east", bssid => "02:03:04:05:06:07", }, { ssid => "south", bssid => "03:04:05:06:07:08", }, { ssid => "west", bssid => "04:05:06:07:08:09", }, ); $points = ( { id => "la gioconda", rssi => { north => -55, east => -76, south => -64, west => -92, }, }, { id => "la pietà ", rssi => { north => -51, east => -60, south => -88, west => -59, }, }, ... );
these static structures (eventually read xml file on disk).
if can of use, i'm implementing local (indoor) positioning system using change in rssi (signal strength) values 4 wifi beacons positioned in 4 corners of each room of museum, find smartphone position. @ runtime, i'll have compare 4 rssi values (each beacon specific ssid , bssid) of points structure, , find 1 closest values.
which best construct available in java? should use hashmaps? can make example?
approach 1:
can use hashmaps acheive goal.
map<string,string> beacons = new hashmap<string,string>(); beacons.put("ssid","north"); beacons.put("bssid","01:02:03:04:05:06"); system.out.println(beacons.get("ssid")); // outputs "north"
approach 2:
if data comes xml file, can use jaxb api (java xml binding) building objects.
let's have following xml file : beacons.xml
<beacons> <beacon ssid="north"> <bssid>01:02:03:04:05:06</bssid> </beacon> <beacon ssid="north"> <bssid>02:03:04:05:06:07</bssid> </beacon> </beacons>
the classes may defined :
beacons.java
package com.mycompany; import java.util.arraylist; import java.util.list; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement public class beacons { @xmlelement(name = "beacon") public list<beacon> beaconslist = new arraylist<beacon>(); }
beacon.java
package com.mycompany; import javax.xml.bind.annotation.xmlattribute; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement public class beacon { @xmlattribute private string ssid; @xmlelement private string bssid; public string getssid() { return ssid; } public string getbssid() { return bssid; } }
sample code loading classes :
package com.mycompany; import java.io.file; import java.util.list; import javax.xml.bind.jaxbcontext; import javax.xml.bind.unmarshaller; public class main { public static void main(string args[]) { try { jaxbcontext jc = jaxbcontext.newinstance(beacons.class); unmarshaller unmarshaller = jc.createunmarshaller(); beacons mybeacons = (beacons) unmarshaller.unmarshal(new file("beacons.xml")); list<beacon> beaconslist = mybeacons.beaconslist; (int = 0; < beaconslist.size(); i++) { beacon a_beacon = beaconslist.get(i); system.out.println("beacon " + (i+1)); system.out.println("ssid : " + a_beacon.getssid()); system.out.println("bssid : " + a_beacon.getbssid()); system.out.println(); } } catch (exception e) { e.printstacktrace(); } } }
--
here output :
beacon 1 ssid : north bssid : 01:02:03:04:05:06 beacon 2 ssid : north bssid : 02:03:04:05:06:07
Comments
Post a Comment