Click here to Skip to main content
11,500,844 members (79,961 online)
Click here to Skip to main content
Add your own
alternative version

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

, 10 Feb 2009 CPOL 493.4K 7.2K 378
A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
source.zip
DeepCast
DeepCast.suo
DeepCast
bin
Debug
DeepCast.exe
FreshTracks.exe
Microsoft.WindowsMobile.dll
Microsoft.WindowsMobile.PocketOutlook.dll
Microsoft.WindowsMobile.Samples.Location.dll
bNb.Sec.dll
DeepCast.csproj.user
Location
Microsoft.WindowsMobile.Samples.Location.dll
OpenNETCF.dll
Properties
Services
FireEagle
InternalServices
Twitter
YOUR SERVICE HERE
UI
Utility
DeepCastCab
Debug
CabWiz.log
DeepCastCab.CAB
DeepCastCab.inf
DeepCastCab.vddproj
Microsoft.Wireless
bin
Debug
Properties
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)

Share

About the Author

TylerBrinks
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
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 10 Feb 2009
Article Copyright 2008 by TylerBrinks
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid