1
Vote

Bluetooth

description

when we have a system without bluetooth application (by example, I'm using windows embedded xp and I remove the bluetooth), the application make an error and crash.
 
for fixing this, I put a try catch on the both function for detecting bluetooth...
 
OLD:
    public static bool IsBluetoothEnabled
    {
        get
        {
            /* We can get the state of the radio if it's a Microsoft stack.
             * Thankfully, Microsoft BT stacks are part of Wista, Wnidows 7, and
             * Windows Mobile 5+, making it very common.  However, it's still in 2nd
             * place behind Broadcom (Widcomm).  Though, I doubt this will last long.
             * So, screw Broadcom.
             */
 

if PocketPC

            GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode mode =
                GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.PowerOff;
            int errorCode = GeoFramework.Gps.IO.NativeMethods.BthGetMode(out mode);
 
 
            if (errorCode != 0)
            {
                /* I get error "1359" (Internal error) on my HP iPaq 2945, which does NOT have a Microsoft Bluetooth stack.
                 * I'm guessing that this API just isn't supported on the Widcomm stack.  Rather
                 * than throw a fit, just gracefully indicate an Off radio.  This will prevent
                 * BT functions in GPS.NET.
                 */
                return false;
            }
 
            /* Connectable and Discoverable both mean "ON".  The only difference is that a
             * "discoverable" Bluetooth radio can be seen by other devices.
             */
            return mode != GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.PowerOff;

else

            return BluetoothRadio.Current != null 
                && BluetoothRadio.Current.IsConnectable;

endif

        }
        set
        {

if PocketPC

            // Convert the boolean to a numeric mode
            GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode mode =
                value ? GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.Connectable
                      : GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.PowerOff;
 
            // Set the new mode
            int result = GeoFramework.Gps.IO.NativeMethods.BthSetMode(mode);
            if (result != 0)
            {
                //    throw new Win32Exception(result);
            }

else

            // TODO: Add support for turning the Bluetooth radio on or off on the desktop.

endif

        }
    }
 
 
New:
    public static bool IsBluetoothEnabled
    {
        get
        {
            /* We can get the state of the radio if it's a Microsoft stack.
             * Thankfully, Microsoft BT stacks are part of Wista, Wnidows 7, and
             * Windows Mobile 5+, making it very common.  However, it's still in 2nd
             * place behind Broadcom (Widcomm).  Though, I doubt this will last long.
             * So, screw Broadcom.
             */
 

if PocketPC

            GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode mode =
                GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.PowerOff;
            int errorCode = GeoFramework.Gps.IO.NativeMethods.BthGetMode(out mode);
 
 
            if (errorCode != 0)
            {
                /* I get error "1359" (Internal error) on my HP iPaq 2945, which does NOT have a Microsoft Bluetooth stack.
                 * I'm guessing that this API just isn't supported on the Widcomm stack.  Rather
                 * than throw a fit, just gracefully indicate an Off radio.  This will prevent
                 * BT functions in GPS.NET.
                 */
                return false;
            }
 
            /* Connectable and Discoverable both mean "ON".  The only difference is that a
             * "discoverable" Bluetooth radio can be seen by other devices.
             */
            return mode != GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.PowerOff;

else

            try
            {
 
                return BluetoothRadio.Current != null
                    && BluetoothRadio.Current.IsConnectable;
            } 
            catch
            {
                // Nope, not supported
                return false;
            }

endif

        }
        set
        {

if PocketPC

            // Convert the boolean to a numeric mode
            GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode mode =
                value ? GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.Connectable
                      : GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.PowerOff;
 
            // Set the new mode
            int result = GeoFramework.Gps.IO.NativeMethods.BthSetMode(mode);
            if (result != 0)
            {
                //    throw new Win32Exception(result);
            }

else

            // TODO: Add support for turning the Bluetooth radio on or off on the desktop.

endif

        }
    }
 
 
 
 
OLD:
    public static bool IsBluetoothSupported
    {
        get
        {

if PocketPC

            try
            {
                // TODO: Is there any more specific way to detect MICROSOFT bluetooth without falsely detecting a stack which doesn't support sockets?
 
                // Try and get the Bluetooth Radio status
                GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode mode =
                    GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.PowerOff;
                int errorCode = GeoFramework.Gps.IO.NativeMethods.BthGetMode(out mode);
 
                // If there's no error, we can proceed.
                return errorCode == 0;
            }
            catch
            {
                // Nope, not supported
                return false;
            }

else

            /* The Microsoft Bluetooth stack provides an API used to enumerate all of the 
             * "radios" on the local machine.  A "radio" is just a Bluetooth transmitter.
             * A vast majority of people will only have one radio; multiple radios would happen
             * if, say, somebody had two USB Bluetooth dongles plugged in.
             * 
             * We can confirm that Bluetooth is supported by looking for a local radio.
             * This method will return immediately with a non-zero handle if one exists.
             */
 
            return BluetoothRadio.Current != null;

endif

        }
    }
 
 
NEW:
    public static bool IsBluetoothSupported
    {
        get
        {

if PocketPC

            try
            {
                // TODO: Is there any more specific way to detect MICROSOFT bluetooth without falsely detecting a stack which doesn't support sockets?
 
                // Try and get the Bluetooth Radio status
                GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode mode =
                    GeoFramework.Gps.IO.NativeMethods.BluetoothRadioMode.PowerOff;
                int errorCode = GeoFramework.Gps.IO.NativeMethods.BthGetMode(out mode);
 
                // If there's no error, we can proceed.
                return errorCode == 0;
            }
            catch
            {
                // Nope, not supported
                return false;
            }

else

            /* The Microsoft Bluetooth stack provides an API used to enumerate all of the 
             * "radios" on the local machine.  A "radio" is just a Bluetooth transmitter.
             * A vast majority of people will only have one radio; multiple radios would happen
             * if, say, somebody had two USB Bluetooth dongles plugged in.
             * 
             * We can confirm that Bluetooth is supported by looking for a local radio.
             * This method will return immediately with a non-zero handle if one exists.
             */
            try
            {
 
                return BluetoothRadio.Current != null;
            }
            catch
            {
                // Nope, not supported
                return false;
            }

endif

        }
    }

comments

JFDionne wrote Aug 4, 2010 at 11:53 PM

BigstickCarpet, please, can you put a eye on that? each time I want to upgade, I need to not forget to put the try catch, or my application crash.

wrote Feb 21, 2013 at 10:51 PM