コード例 #1
0
 private static void DumpElement(System.IO.TextWriter writer, int depth, ServiceElement elem)
 {
     WritePrefix(writer, depth);
     if (elem.ElementType == ElementType.ElementSequence || elem.ElementType == ElementType.ElementAlternative)
     {
         writer.WriteLine("{0}", elem.ElementType);
         foreach (ServiceElement element in elem.GetValueAsElementList())
         {
             DumpElement(writer, depth + 1, element);
         }//for
     }
     else if (elem.ElementType == ElementType.Nil)
     {
         writer.WriteLine("Nil:");
     }
     else if (elem.ElementType == ElementType.TextString)
     {
         DumpString(writer, depth, elem, null);
     }
     else if (elem.ElementType == ElementType.Boolean ||
              elem.ElementType == ElementType.Url)
     {
         // Non-numeric types
         writer.WriteLine("{0}: {1}", elem.ElementType, elem.Value);
     }
     else
     {
         String name      = null;
         String valueText = null;
         if (elem.ElementTypeDescriptor == ElementTypeDescriptor.Uuid)
         {
             if (elem.ElementType == ElementType.Uuid16)
             {
                 name = BluetoothService.GetName((UInt16)elem.Value);
             }
             else if (elem.ElementType == ElementType.Uuid32)
             {
                 name = BluetoothService.GetName((UInt32)elem.Value);
             }
             else
             {
                 System.Diagnostics.Debug.Assert(elem.ElementType == ElementType.Uuid128);
                 name      = BluetoothService.GetName((Guid)elem.Value);
                 valueText = ((Guid)elem.Value).ToString();
             }
         }//if UUID
         if (valueText == null)
         {
             if (elem.ElementTypeDescriptor == ElementTypeDescriptor.Unknown)
             {
                 valueText = "unknown";
             }
             else if (elem.ElementType == ElementType.UInt128 ||
                      elem.ElementType == ElementType.Int128)
             {
                 valueText = BitConverter.ToString((byte[])elem.Value);
             }
             else
             {
                 valueText = String.Format(System.Globalization.CultureInfo.InvariantCulture, "0x{0:X}", elem.Value);
             }
         }
         if (name == null)
         {
             writer.WriteLine("{0}: {1}", elem.ElementType, valueText);
         }
         else
         {
             writer.WriteLine("{0}: {1} -- {2}", elem.ElementType, valueText, name);
         }
     }//else
 }
コード例 #2
0
        //--------
        /// <summary>
        /// Attempt to get the name of the protocol,
        /// and optionally it's enum id if we handle it specially.
        /// </summary>
        /// -
        /// <param name="protocolGuid">The input.
        /// </param>
        /// <param name="protoStr">The protocol's name if known, or its
        /// Guid.ToString if not.
        /// We handle some explicitly, and otherwise we see if there's a
        /// matching value in BluetoothService that has its name suffixed "Protocol".
        /// </param>
        /// -
        /// <returns>The id as a <see cref="T:InTheHand.Net.Bluetooth.ServiceRecordUtilities.HackProtocolId"/>.
        /// We handle some explicitly,
        /// otherwise we see if its a UUID16 and convert it automatically,
        /// finally if neither we return <c>zero</c>.
        /// </returns>
        private static HackProtocolId GuidToHackProtocolId(Guid protocolGuid, out string protoStr)
        {
            HackProtocolId?proto = null;

            if (protocolGuid == BluetoothService.BnepProtocol)
            {
                proto = HackProtocolId.Bnep;
                //else if (protocolGuid == BluetoothService.hid......)
                // Missing to test automatic uint16->HackProtocolId covnersion
                //    return HackProtocolId.Hidp;
            }
            else if (protocolGuid == BluetoothService.L2CapProtocol)
            {
                proto = HackProtocolId.L2Cap;
            }
            else if (protocolGuid == BluetoothService.ObexProtocol)
            {
                proto = HackProtocolId.Obex;
            }
            else if (protocolGuid == BluetoothService.RFCommProtocol)
            {
                proto = HackProtocolId.Rfcomm;
            }
            else if (protocolGuid == BluetoothService.SdpProtocol)
            {
                proto = HackProtocolId.Sdp;
            }
            if (proto.HasValue)
            {
                protoStr = proto.ToString();
                return(proto.Value);
            }
            //
            // Slower ways of picking up the rest.
            // Get HackProtocolId and string name separately.
            if (IsUuid16Value(protocolGuid))
            {
                proto = GetAsUuid16Value(protocolGuid);
            }
            else
            {
                proto = 0;
            }
            //
            if (Enum.IsDefined(typeof(HackProtocolId), proto))
            {
                protoStr = Enum_ToStringNameOrHex(proto);
            }
            else
            {
                string       nameAttempt    = BluetoothService.GetName(protocolGuid);
                const string ProtocolSuffix = "Protocol";
                if (nameAttempt != null && nameAttempt.EndsWith(ProtocolSuffix, StringComparison.Ordinal))
                {
                    // Has a known name
                    Debug.Assert(nameAttempt.Length > 0);
                    Debug.Assert(nameAttempt.Length > ProtocolSuffix.Length);
                    // (Two param version required for NETCF).
                    protoStr = nameAttempt.Remove(nameAttempt.Length - ProtocolSuffix.Length, ProtocolSuffix.Length);
                }
                else if (proto != 0)     // Conver the integer to hex
                {
                    protoStr = Enum_ToStringNameOrHex(proto);
                }
                else     // Not a standard Bluetooth value so dump its UUID
                {
                    protoStr = protocolGuid.ToString();
                }
            }
            return(proto.Value);
        }