ratishkool.blogspot.in/ Heart Hacking...: April 2011

Pages

Ads 468x60px

Monday, April 11, 2011

Cell location info

Few years back a phone was launched called Nokia N95, it used to have an App which was providing 
the details of the caller, area even it used to show the google map address on the phone screen, 
the same feature was also launched innokia E90. This is the source code for that App, which is
written in python, hope you will like it.
 
 
import location
 
 
mcc, mnc, lac, cellid = location.gsm_location()
 
print "Mobile Country Code:", mcc
print "Mobile Network Code:", mnc
print "Location Area Code:", lac
print "Cell ID:", cellid

Source code:GoogleMaps class

import java.io.ByteArrayOutputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Vector; 
import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 
import javax.microedition.lcdui.Image; 
 
public class GoogleMaps { 
    private static final String URL_UNRESERVED =  
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  
        "abcdefghijklmnopqrstuvwxyz" + 
        "0123456789-_.~"; 
    private static final char[] HEX = "0123456789ABCDEF".toCharArray(); 
 
    // these 2 properties will be used with map scrolling methods. You can remove them if not needed 
    public static final int offset = 268435456; 
    public static final double radius = offset / Math.PI; 
 
    private String apiKey = null; 
 
    public GoogleMaps(String key) { 
        apiKey = key; 
    } 
 
    public double[] geocodeAddress(String address) throws Exception { 
        byte[] res = loadHttpFile(getGeocodeUrl(address)); 
        String[] data = split(new String(res), ','); 
 
        if (!data[0].equals("200")) { 
            int errorCode = Integer.parseInt(data[0]); 
            throw new Exception("Google Maps Exception: " + getGeocodeError(errorCode)); 
        } 
 
        return new double[] { 
                Double.parseDouble(data[2]), Double.parseDouble(data[3]) 
        }; 
    } 
 
    public Image retrieveStaticImage(int width, int height, double lat, double lng, int zoom, 
            String format) throws IOException { 
        byte[] imageData = loadHttpFile(getMapUrl(width, height, lng, lat, zoom, format)); 
 
        return Image.createImage(imageData, 0, imageData.length); 
    } 
 
    private static String getGeocodeError(int errorCode) { 
        switch (errorCode) { 
        case 400: 
            return "Bad request"; 
        case 500: 
            return "Server error"; 
        case 601: 
            return "Missing query"; 
        case 602: 
            return "Unknown address"; 
        case 603: 
            return "Unavailable address"; 
        case 604: 
            return "Unknown directions"; 
        case 610: 
            return "Bad API key"; 
        case 620: 
            return "Too many queries"; 
        default: 
            return "Generic error"; 
        } 
    } 
 
    private String getGeocodeUrl(String address) { 
        return "http://maps.google.com/maps/geo?q=" + urlEncode(address) + "&output=csv&key=" 
                + apiKey; 
    } 
 
    private String getMapUrl(int width, int height, double lng, double lat, int zoom, String format) { 
        return "http://maps.google.com/staticmap?center=" + lat + "," + lng + "&format=" 
                + format + "&zoom=" + zoom + "&size=" + width + "x" + height + "&key=" + apiKey; 
    } 
 
    private static String urlEncode(String str) { 
        StringBuffer buf = new StringBuffer(); 
        byte[] bytes = null; 
        try { 
            ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
            DataOutputStream dos = new DataOutputStream(bos); 
            dos.writeUTF(str); 
            bytes = bos.toByteArray(); 
        } catch (IOException e) { 
            // ignore 
        } 
        for (int i = 2; i < bytes.length; i++) { 
            byte b = bytes[i]; 
            if (URL_UNRESERVED.indexOf(b) >= 0) { 
                buf.append((char) b); 
            } else { 
                buf.append('%').append(HEX[(b >> 4) & 0x0f]).append(HEX[b & 0x0f]); 
            } 
        } 
        return buf.toString(); 
    } 
 
    private static byte[] loadHttpFile(String url) throws IOException { 
        byte[] byteBuffer; 
 
        HttpConnection hc = (HttpConnection) Connector.open(url); 
        try { 
            hc.setRequestMethod(HttpConnection.GET); 
            InputStream is = hc.openInputStream(); 
            try { 
                int len = (int) hc.getLength(); 
                if (len > 0) { 
                    byteBuffer = new byte[len]; 
                    int done = 0; 
                    while (done < len) { 
                        done += is.read(byteBuffer, done, len - done); 
                    } 
                } else { 
                    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
                    byte[] buffer = new byte[512]; 
                    int count; 
                    while ( (count = is.read(buffer)) >= 0 ) { 
                        bos.write(buffer, 0, count); 
                    } 
                    byteBuffer = bos.toByteArray(); 
                } 
            } finally { 
                is.close(); 
            } 
        } finally { 
            hc.close(); 
        } 
 
        return byteBuffer; 
    } 
 
    private static String[] split(String s, int chr) { 
        Vector res = new Vector(); 
 
        int curr; 
        int prev = 0; 
 
        while ( (curr = s.indexOf(chr, prev)) >= 0 ) { 
            res.addElement(s.substring(prev, curr)); 
            prev = curr + 1; 
        } 
        res.addElement(s.substring(prev)); 
 
        String[] splitted = new String[res.size()]; 
        res.copyInto(splitted); 
 
        return splitted; 
    } 
}

GetWLANAPIInfol

/**
 * This function is used to list WLAN Info with WLAN Info API
 * 
 * @infoBuff - when function returns it holds the WLAN Information
 */
void GetWLANAPIInfoL(TDes &infoBuff)
{
        CWlanScanInfo iScanInfo;
        CWlanMgmtClient iWLANMgmtClient;
 
        iWLANMgmtClient.GetScanResults(iScanInfo);
 
 TInt i=1, k;
 TBuf<36> bss;
 
 _LIT(KNetwork, "Network ");
 _LIT(KNEWLine, "\n");
 _LIT(KSeparator, " - ");
 _LIT(KBSSIDFormat, "%02X:");
 _LIT(KSSIDUnknown, "SSID Unknown");
 
 infoBuff.Zero();
 for(iScanInfo.First(); !iScanInfo.IsDone(); iScanInfo.Next() )
 {
  infoBuff.Append(KNetwork);
  infoBuff.AppendNum(i);
  infoBuff.Append(KSeparator);
  i++;
 
  //Retrieve BSSID
  TWlanBssid bssid;
  iScanInfo.Bssid( bssid );
  bss.Zero();  
  for(k = 0; k < bssid.Length(); k++)
   bss.AppendFormat(KBSSIDFormat, bssid[k]);
  //remove last :
  bss.Delete(bss.Length()-1, 1);
  infoBuff.Append(bss);
  infoBuff.Append(KSeparator);
 
  //Get transmision level
  TInt8 rxLevel = iScanInfo->RXLevel();
  infoBuff.AppendNum(rxLevel);
  infoBuff.Append(KSeparator);
 
  //Get SSID
  TBuf8<36> ssid;
  TInt err;
  err = GetSSID(&iScanInfo, ssid);
  if(err== KErrNone)
  {
   bss.Zero();
   bss.Copy(ssid);
   infoBuff.Append(bss);
  }
  else
   infoBuff.Append(KSSIDUnknown);
 
  infoBuff.Append(KNEWLine);
 }
}

GetSSID

/**
 * Function that retrieves SSID of current scaned network
 * 
 * The SSID is the Information Element with ID = 0
 * The SSID can be at most 34 bytes, so the aSSID should have the capacity for 34 bytes
 * 
 * Returns KErrNone if successful or the type of error ocurred
 */
TInt GetSSID(CWlanScanInfo *scanInfo, TDes8 &aSSID)
{
 TInt error;
 TUint8 ie;
 TUint8 length;
 TUint8 vdata[40];
 const TUint8 *data = &vdata[0];
 
 if(scanInfo == NULL)
  return KErrNullPointerPassed;
 
 if(aSSID.MaxLength() < 34)
  return KErrNoMemory;
 
 
 if((error = scanInfo->FirstIE(ie, length, &data)) == KErrNone)
 {
  if(ie == 0)
  {
   aSSID.Copy(data, length);
   return KErrNone;
  }
 }
 else
  return error;
 
 while((error = scanInfo->NextIE(ie, length, &data)) == KErrNone)
 {
  if(ie == 0)
  {
   aSSID.Copy(data, length);
   return KErrNone;
  }
 }
 
 return error;
}
 

Heart Hacking

Heart Hacking

Heart Hacking

 
url submit Ping your blog, website, or RSS feed for Free Text Back Link Exchange Way2Wap.Com
eXTReMe Tracker
Computers Blogs
Top Blogs