No Gps

of 32
8 views
PDF
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Document Description
Java Prog. Techniques for Games. Chapter 35. No GPS Draft #1 (27th Nov. 10) Chapter 35. Geolocation without GPS I made things easy for myself in the last chapter by relying on a GPS receiver to generate latitude and longitude information. In this chapter, I throw away the GPS device (figuratively, of course), and determine my position and geographical address (my geolocation) without its help. Although this seems a daunting task, I'm encouraged by the availability of geolocation in HTML5. Even
Document Share
Document Tags
Document Transcript
   Java Prog. Techniques for Games. Chapter 35. No GPS   Draft #1 (27th Nov. 10) 1 © Andrew Davison 2010 Chapter 35. Geolocation without GPS I made things easy for myself in the last chapter by relying on a GPS receiver togenerate latitude and longitude information. In this chapter, I throw away the GPSdevice (figuratively, of course), and determine my position and geographical address(my geolocation ) without its help.Although this seems a daunting task, I'm encouraged by the availability of geolocationin HTML5. Even when there's no GPS receiver attached to the computer, a tiny pieceof JavaScript executed by the browser can return your latitude and longitude. Amapping example using HTML5's geolocation API can be found athttp://html5demos.com/geo. The Google map it displays for my position is shown inFigure 1.Figure 1. An HTML5-generated Map.This map isn't such an impressive display of HTML5's magic, since it places me inthe wrong city – Songkhla, about 25 km east of my true location, at Prince of SongklaUniversity (PSU) in Hat Yai. To be fair, US and European users have reported moreimpressive results, and the API makes no guarantees about returning an accuratelocation.How does HTML5 work? At the programming level, the Web page author employsthe navigator.geolocation JavaScript property, and a callback function that's executedafter the user agrees to share information over the Web. A good introduction to thecoding details can be found in Chapter 6 of the online book Dive into HTML5 byMark Pilgrim (http://diveintohtml5.org/geolocation.html).The W3C Geolocation API Specification (http://www.w3.org/TR/geolocation-API),states that an implementation can use several alternative location sources, dependingon the capabilities of the device. Possibilities include GPS, triangulation via cellular   Java Prog. Techniques for Games. Chapter 35. No GPS   Draft #1 (27th Nov. 10) 2 © Andrew Davison 2010 tower IDs, RFID and Bluetooth IDs, nearby WiFi access points, and the computer’sIP address. Firefox utilizes this information by sending it to Google's LocationServices which returns a location estimate (http://www.mozilla.com/en-US/firefox/geolocation/).I want to emulate HTML5's geolocation API in Java without having to fire-up abrowser. Also, I'm going to restrict my hardware to a netbook without a GPS receiver,RFID or Bluetooth; the machine's only connection to the wider world will be throughWiFi. This leaves two starting points for finding my location: IP and MAC addresses. What are IP and MAC Addresses? As you probably know, an IP address is a unique ID assigned to any device connectedto a TCP/IP network (such as the Internet). By assuming that computers are fairlyimmobile, databases have been compiled that map IP addresses to locations.Unfortunately, an increasing number of machines aren't fixed in one place, and/orutilize non-unique, dynamic IP addresses.A Media Access Control (MAC) address is meant to be a permanent, unique IDassigned to a network interface card (NIC) or LAN card on your machine. But MACaddresses aren't as permanent as we might hope; often the address is stored in a card'sfirmware, which makes it possible to modify (called  MAC spoofing ). The addressdoesn't contain a latitude or longitude, but details on the card's manufacturer andmodel. The IEEE maintains a database of vendors, called the Organizationally UniqueIdentifiers (OUI), which can be searched using the first three bytes of a MAC addressat http://standards.ieee.org/develop/regauth/oui/public.html.How can a MAC address be converted into a latitude and longitude? For that we mustthank Google and Skyhook Wireless (http://www.skyhookwireless.com/). They'vebeen busy driving around cities and towns in North America, Europe, Asia, andAustralia, plotting the location of WiFi access points (APs) and cell towers. Skyhook has a map online showing its coverage(http://www.skyhookwireless.com/howitworks/coverage.php), and claims theirdatabase has over 250 million entries.This occupation is often called wardriving , and has been getting Google into troublefor 'snooping'. It's probably okay to record a wireless' name (SSID) and MAC address,along with an associated latitude and longitude. The problems begin if data being sentover those networks is recorded.Although IP and MAC addresses are quite different, TCP/IP includes protocols forconverting from IP to MAC addresses -- the Address Resolution Protocol (ARP) forIPv4 and the Neighbor Discovery Protocol (NDP) for IPv6. Using IP and MAC addresses Figure 2 shows an overview of the various ways I'll be using IP and MAC addressesin this chapter.   Java Prog. Techniques for Games. Chapter 35. No GPS   Draft #1 (27th Nov. 10) 3 © Andrew Davison 2010 Figure 2. From IP/MAC addresses to Locations.The IP and MAC addresses of my netbook aren't much use, because they're for amobile device. Instead I have to reach out across the wireless network for theaddresses of WiFi Access Points (APs). These APs are probably in fixed positions,and so likely to be listed in location databases.The dotted lines and arrows in Figure 2 have Java program names next to them. Forexample, I'll describe two programs, GoogleMAC.java and SkyhookMAC.java, forconverting a MAC address into a latitude and longitude. As their names suggest, oneuses Google, the other Skyhook Wireless.I'll explore several approaches since they tend to give slightly different answers, withthe quality depending on the AP's location and the coverage of the databasesinvolved. For example, SkyhookMAC.java fails to return any information for mynetbook, probably because of Skyhook's sketchy coverage of the south of Thailand.Most of the programs are similar – they utilize Web services, and parse the JSON,XML, or HTML responses. For that reason, it's a good idea if you read Chapter 33 onWeb Service APIs first (http://fivedots.coe.psu.ac.th/~ad/jg/ch33/), because I won't beexplaining those details again. I also won't be describing latitude and longitudemapping, because I just talked about that in Chapter 34(http://fivedots.coe.psu.ac.th/~ad/jg/ch34/).   Java Prog. Techniques for Games. Chapter 35. No GPS   Draft #1 (27th Nov. 10) 4 © Andrew Davison 2010 1. Examining my Netbook Two JavaSE network classes, InetAddress and NetworkInterface, provide all I need tolist my netbook's IP and MAC addresses, although NetworkInterface.  getHardwareAddress() for reading the MAC address was only added in JavaSE 6.More detailed information can be displayed for each NIC via the InterfaceAddressclass (also new to JavaSE 6). A good example of its capabilities can be found athttp://stackoverflow.com/questions/494465/how-to-enumerate-ip-addresses-of-all-enabled-nic-cards-from-javaThe following code comes from my AddrViewer.java. showIP() prints the machine'sIP address: private static void showIP(){try {InetAddress localHost = InetAddress.getLocalHost();System.out.println( LocalHost IP address: +localHost.getCanonicalHostName() + \n );}catch (UnknownHostException e){ System.out.println( No LocalHost Info found\n ); }} // end of showIP() showMacNICs() iterates through the NICs on a device, printing their MAC addresses: private static void showMacNICs()// list the NICs that have MAC addresses{try {Enumeration<NetworkInterface> intfs =NetworkInterface.getNetworkInterfaces();while(intfs.hasMoreElements()) {NetworkInterface intf = intfs.nextElement();byte[] macAddr = intf.getHardwareAddress();if (macAddr != null) {System.out.println(intf.getDisplayName());System.out.println( MAC: +  macToString (macAddr));System.out.println( Operational? + intf.isUp());System.out.println();}}}catch (SocketException e){ e.printStackTrace(); }} // end of showMacNICs() NetworkInterface.getDisplayName() supplies the NIC's SSID (its name).NetworkInterface.getHardwareAddress() unhelpfully returns a MAC address as a bytearray, so macToString() converts it to a string containing two-digit hexadecimalsseparated by : s.Information for my netbook is shown in Figure 3.
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x