Click here to Skip to main content
15,892,005 members
Articles / Mobile Apps / Windows Mobile

Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant

Rate me:
Please Sign up or sign in to vote.
4.94/5 (126 votes)
10 Feb 2009CPOL10 min read 775.1K   11.1K   381  
A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Microsoft.Wireless
{
    /// <summary>
    /// Identifies the phone number type specified.
    /// </summary>
    public enum AddressType
    {
        /// <summary>Unknown phone number type.</summary>
        Unknown,
        /// <summary>International phone number.</summary>
        International,
        /// <summary>National phone number.</summary>
        National,
        /// <summary>Network-specific phone number.</summary>
        NetworkSpecific,
        /// <summary>Subscriber phone number.</summary>
        Subscriber,
        /// <summary>Alphanumeric phone number.</summary>
        Alphanumeric,
        /// <summary>Abbreviated phone number.</summary>
        Abbreviated
    }

    /// <summary>
    /// Information about the phone number.
    /// </summary>
    public struct PhoneAddress
    {
        /// <summary>The address type.</summary>
        public AddressType AddressType;
        /// <summary>The phone number in string format.</summary>
        public String Address;
    }

    /// <summary>
    /// Short Message Service.
    /// </summary>
    public class SMS
    {
        private static string SMS_MSGTYPE_TEXT = "Microsoft Text SMS Protocol";
        private static long SMS_MODE_SEND = 0x00000002;
        private static long SMS_OPTION_DELIVERY_NONE = 0x00000000;
        private static long SMS_OPTION_DELIVERY_NO_RETRY = 0x00000001;
        private static long PS_MESSAGE_OPTION_NONE = 0x00000000;

        private enum SMS_DATA_ENCODING
        {
            SMSDE_OPTIMAL = 0,
            SMSDE_GSM,
            SMSDE_UCS2,
        }

        private enum PROVIDER_SPECIFIC_MESSAGE_CLASS
        {
            PS_MESSAGE_CLASS0 = 0,
            PS_MESSAGE_CLASS1,
            PS_MESSAGE_CLASS2,
            PS_MESSAGE_CLASS3,
        }

        private enum PROVIDER_SPECIFIC_REPLACE_OPTION
        {
            PSRO_NONE = 0,
            PSRO_REPLACE_TYPE1,
            PSRO_REPLACE_TYPE2,
            PSRO_REPLACE_TYPE3,
            PSRO_REPLACE_TYPE4,
            PSRO_REPLACE_TYPE5,
            PSRO_REPLACE_TYPE6,
            PSRO_REPLACE_TYPE7,
            PSRO_RETURN_CALL,
            PSRO_DEPERSONALIZATION,
        }

        private struct TEXT_PROVIDER_SPECIFIC_DATA
        {
            public IntPtr dwMessageOptions;
            public PROVIDER_SPECIFIC_MESSAGE_CLASS psMessageClass;
            public PROVIDER_SPECIFIC_REPLACE_OPTION psReplaceOption;
        }

        [DllImport("sms.dll")]
        private static extern IntPtr SmsOpen(String ptsMessageProtocol, IntPtr dwMessageModes, ref IntPtr psmshHandle, IntPtr phMessageAvailableEvent);

        [DllImport("sms.dll")]
        private static extern IntPtr SmsSendMessage(IntPtr smshHandle, IntPtr psmsaSMSCAddress, IntPtr psmsaDestinationAddress, IntPtr pstValidityPeriod, byte[] pbData, IntPtr dwDataSize,
            byte[] pbProviderSpecificData, IntPtr dwProviderSpecificDataSize, SMS_DATA_ENCODING smsdeDataEncoding,
            IntPtr dwOptions, IntPtr psmsmidMessageID);

        [DllImport("sms.dll")]
        private static extern IntPtr SmsClose(IntPtr smshHandle);

        /// <summary>
        /// Sends a SMS message to the phone number specified.
        /// </summary>
        /// <param name="sPhoneNumber">The phone number of the recipent</param>
        /// <param name="sMessage">The message to send.</param>
        unsafe public static void SendMessage(string sPhoneNumber, string sMessage)
        {
            IntPtr hSms = IntPtr.Zero;

            try
            {
                IntPtr res = SmsOpen(SMS_MSGTYPE_TEXT, (IntPtr)SMS_MODE_SEND, ref hSms, IntPtr.Zero);
                if (res != IntPtr.Zero)
                    throw new Exception("Could not open SMS.");

                Byte[] bDest = new Byte[516];
                fixed (byte* pAddr = bDest)
                {
                    byte* pCurrent = pAddr;
                    Marshal.WriteInt32((IntPtr)pCurrent, (int)AddressType.Unknown);
                    pCurrent += 4;

                    foreach (byte b in Encoding.Unicode.GetBytes(sPhoneNumber))
                    {
                        Marshal.WriteByte((IntPtr)pCurrent, b);
                        pCurrent++;
                    }

                    // The data for the TEXT_PROVIDER_SPECIFIC_DATA
                    Byte[] ProvData = new Byte[12];

                    byte[] bMessage = Encoding.Unicode.GetBytes(sMessage);
                    int nMsgSize = bMessage.Length;

                    res = SmsSendMessage(hSms, IntPtr.Zero, (IntPtr)pAddr, IntPtr.Zero, bMessage, (IntPtr)nMsgSize,
                        ProvData, (IntPtr)ProvData.Length, SMS_DATA_ENCODING.SMSDE_OPTIMAL, (IntPtr)SMS_OPTION_DELIVERY_NONE, IntPtr.Zero);

                    if (res != IntPtr.Zero)
                        throw new Exception("SMS send failed.");
                }
            }
            finally
            {
                if (hSms != IntPtr.Zero)
                    SmsClose(hSms);
            }
        }
    }
}

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
Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!

http://www.pagelabs.com

Comments and Discussions