Click here to Skip to main content
15,896,118 members
Articles / Mobile Apps

Using the Skyhook Wireless XPS Positioning Service in Managed Code

Rate me:
Please Sign up or sign in to vote.
4.95/5 (15 votes)
20 Jan 2009CPOL28 min read 89.2K   1.4K   57  
Wrapper and sample programs demonstrating the use of the Skyhook Wireless XPS SDK (hybrid position system using GPS, WiFi Positioning, and Celltower positioning)
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Xml.XPath;


namespace J2i.Net.OpenCellID
{

    
    public class OpenCellClient : IDisposable
    {
        string _baseUrl = "http://www.opencellid.org/cell";
        string _appID = String.Empty;
        bool _supressIOErrors;
        RILCELLTOWERINFO lastTower = null;
        CellTowerLocation lastLocation = null;
        public OpenCellClient()
        {

        }
        public OpenCellClient(string appID):this()
        {
            _appID = appID;
        }

        public CellTowerLocation GetCellTowerLocation (uint mobileCountryCode, uint  mobileNetworkCode, uint  localeAreaCode, uint   cellID)
        {

            CellTowerLocation retVal = null;
            string requestAddress = String.Format("{0}/get?mnc={1}&mcc={2}&lac={3}&cellid={4}",
                _baseUrl,
                mobileNetworkCode, mobileCountryCode, localeAreaCode, cellID);
            try
            {
                WebRequest webRequest = HttpWebRequest.Create(requestAddress);
                WebResponse response = webRequest.GetResponse();
                using (Stream xmlSource = response.GetResponseStream())
                {
                    XmlDocument xd = new XmlDocument();
                    xd.Load(xmlSource);

                    if (xd.GetElementsByTagName("rsp")[0].Attributes["stat"].Value.Equals("ok"))
                    {
                        XmlNode cellNode = xd.GetElementsByTagName("cell")[0];
                        retVal = new CellTowerLocation();
                        retVal.cellid = cellID;
                        retVal.mcc = mobileCountryCode;
                        retVal.mnc = mobileNetworkCode;

                        retVal.lac = localeAreaCode;
                        retVal.lat = Double.Parse(cellNode.Attributes["lat"].Value);
                        retVal.lon = Double.Parse(cellNode.Attributes["lon"].Value);
                        retVal.nbSamples = Int32.Parse(cellNode.Attributes["nbSamples"].Value);
                        retVal.range = Int32.Parse(cellNode.Attributes["range"].Value);
                    }
                }
            }
            catch (Exception exc)
            {
                if (!_supressIOErrors)
                    throw;
            }
            return retVal;

        }

        public CellTowerLocation GetCellTowerLocation()
        {

            RILCELLTOWERINFO towerInfo = RIL.GetCellTowerInfo();
            if ((lastTower != null) && ((lastTower.dwMobileCountryCode == towerInfo.dwMobileCountryCode) && (lastTower.dwLocationAreaCode == towerInfo.dwLocationAreaCode) && (lastTower.dwMobileNetworkCode == towerInfo.dwMobileNetworkCode) && (lastTower.dwCellID == towerInfo.dwCellID)))
            {
                return lastLocation;
            }
            return (lastLocation = GetCellTowerLocation(towerInfo.dwMobileCountryCode, towerInfo.dwMobileNetworkCode, towerInfo.dwLocationAreaCode, towerInfo.dwCellID));

        }

        public int AddMeasure(uint mobileCountryCode, uint mobileNetworkCode, uint localeAreaCode, uint cellID, double latitude, double longitude)
        {
            TestAppID();
            int retVal = 0;
            string requestAddress = String.Empty;

            requestAddress = string.Format("{0}/measure/add?key={1}?&mnc={2}&mcc={3}&lac={4}&cellid={5}&lat={6}&long={7}",
                _baseUrl,
                _appID, mobileNetworkCode,
                mobileNetworkCode,
                mobileCountryCode,
                localeAreaCode,
                cellID,
                latitude,
                longitude);
            try
            {
                WebRequest webRequest = HttpWebRequest.Create(requestAddress);
                WebResponse response = webRequest.GetResponse();
                using (Stream xmlSource = response.GetResponseStream())
                {
                    XmlDocument xd = new XmlDocument();
                    xd.Load(xmlSource);

                    if (xd.GetElementsByTagName("rsp")[0].Attributes["stat"].Value.Equals("ok"))
                    {
                        retVal = int.Parse(xd.DocumentElement.Attributes["id"].Value);
                    }
                }
            }
            catch (Exception)
            {
                if (!_supressIOErrors)
                    throw;
            }
            return retVal;
        }

        public bool DeleteMeasure(int id)
        {
            TestAppID();
            bool retVal = false;
            string requestAddress = String.Format("{0}/measure/delete/{1}?key={2}", _baseUrl, id, _appID);
            try
            {
                WebRequest webRequest = HttpWebRequest.Create(requestAddress);
                WebResponse response = webRequest.GetResponse();
                using (Stream xmlSource = response.GetResponseStream())
                {
                    XmlDocument xd = new XmlDocument();
                    xd.Load(xmlSource);

                    if (xd.GetElementsByTagName("rsp")[0].Attributes["stat"].Value.Equals("ok"))
                    {
                        retVal = true;
                    }
                }
            }
            catch (Exception)
            {
                if (!_supressIOErrors)
                    throw;
            }
            return retVal;
        }


        private void TestAppID()
        {
            if ((_appID == null) || (_appID.Equals(String.Empty)))
                throw new ArgumentException("An Application ID has not been set on the OpenCellClient object.  A key can be requested at http://opencellid.org/users/signup", "AppID");
        }

        #region IDisposable Members

        public void Dispose()
        {

        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions