.NET Components for Mobility

ClassOfDevice Enumeration

Last post 01-19-2006 11:07 AM by alanjmcf. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-12-2006 7:34 PM

    ClassOfDevice Enumeration

    Peter, or anyone else.

    Has anyone been successful in throwing the BluetoothDeviceInfo.ClassOfDevice into an switch?

    I've ripped all the bluetooth icons from my widcomm stack ( not installed ) so I can show an icon for headset (authenticated, connected and disconnected etc.) but using

    switch(device.ClassOfDevice)
    {
        case ClassOfDevice.PdaComputer :
        do something
        break;
    }

    Does not work, when I add another case into the switch I get errors.

    Anyone?

    Gav

  • 01-12-2006 11:27 PM In reply to

    Re: ClassOfDevice Enumeration

    If using a combobox like this:
    cmbDevices.DataSource = bdi;
                cmbDevices.DisplayMember = "DeviceName";
                cmbDevices.ValueMember = "ClassOfDevice";

    And doing a switch in the selectedIndexChanged function like:
    switch(cmbDevices.SelectedValue.ToString())
                {
                    case "PdaComputer" :
                        MessageBox.Show("PDA");
                    break;

                    case "LaptopComputer" :
                        MessageBox.Show("Laptop");
                        break;

                }

    It works for my laptop which only returns "LaptopComputer" however my pda which returns "PdaComputer, ObexServices" does not work.

    The ClassOfDevice returns all classes seperated by a comma so you'd have to split the returned string into an array and then do a switch on each element in the array.

    string[] Array = cmbDevices.SelectedValue.ToString().Split(new char[] {','});
                string tmpClass;
                foreach(string Class in Array)
                {
                    tmpClass = Class.TrimEnd(' ');
                    tmpClass = tmpClass.TrimStart(' ');

                    switch(tmpClass)
                    {
                        case "PdaComputer" :
                            MessageBox.Show("PDA");
                            break;

                        case "LaptopComputer" :
                            MessageBox.Show("Laptop");
                            break;

                    }
                }

    All works. Hope it helps mate!
    I accept no liability for my crappy code

    www.OurNetworkIsMadeOfString.co.uk
  • 01-19-2006 11:07 AM In reply to

    Re: ClassOfDevice Enumeration

    Hi both

    Using the ClassOfDevice value is not as simple as comparing it for equality against a single enum member. The Flags Attribute on the enum should be a hint that that's the case. :-)

    One really also needs to have read the Bluetooth documentation on this value, its quite involved, see the "Assigned Numbers" link in the help page. In summary the value contains four separate parts: Service classes, Major Device class, Minor Device class and a fixed Format Type. So one will nearly always need to use a set of bit masks to pick out the required piece. I suggest something like:

       MaskServiceClasses = 0xffe000,

       MaskMajorDeviceClass = 0x001f00,

       MaskMinorDeviceClass = 0x0000fc,

       MaskDeviceClass = MaskMajorDeviceClass | MaskMinorDeviceClass,

       MaskFormatType = 0x000003,

    and for completeness

       FormatType1=0x0, //last two bits (00), for "format #1"; the one described in this enum

    Note then, that: the Service Classes field is a bit mask; Major Device Class is a numerical value; the format of Minor Device class is different for each Major Device class, and can be a numerical or a flags value, or even have a number of sub fields. For instance the Peripherals minor device class has two part a Keyboard / Pointing Device pair of flags and a "sub-field" numerical value, again further masking is probably necessary there.

    Anyway, if wanting to check the value of the Major Device class, one could do something like the following (untested) code.

       class Abcdef

       {

          const ClassOfDevice MaskMajorDeviceClass = (ClassOfDevice)0x001f00;

          ... MyMethod(...)

          {

             //mask out the middle five bits

             switch(value & MaskMajorDeviceClass)

             {

                //use the enum members that have their last byte blank

                case ClassOfDevice.Computer: //0x100

                   ...

                case ClassOfDevice.Phone: //0x200

                case ClassOfDevice.AccessPointAvailable: //0x300

             }//switch

    As you probably guessed converting the enum value to a string (with ToString) and then grep-ing bits of the string from it is not nice. :-,(

    Or to check whether a device advertises a particular service use, for example:

       if(0 != (value & ClassOfDevice.ObexService))

    Alan

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
Page 1 of 1 (3 items)
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.