/// <summary> /// Sets the RFCOMM Channel Number value in the service record. /// </summary> /// - /// <param name="record">The <see cref="T:InTheHand.Net.Bluetooth.ServiceRecord"/> /// in which to set the RFCOMM Channel number. /// </param> /// <param name="channelNumber">The Channel number to set in the record. /// </param> /// - /// <exception cref="T:System.InvalidOperationException">The /// <see cref="F:InTheHand.Net.Bluetooth.AttributeIds.UniversalAttributeId.ProtocolDescriptorList"/> /// attribute is missing or contains invalid elements. /// </exception> public static void SetRfcommChannelNumber(ServiceRecord record, byte channelNumber) { ServiceElement channelElement = GetRfcommChannelElement(record); if (channelElement == null) { throw new InvalidOperationException("ProtocolDescriptorList element does not exist or is not in the RFCOMM format."); } System.Diagnostics.Debug.Assert(channelElement.ElementType == ElementType.UInt8); channelElement.SetValue(channelNumber); }
/// <summary> /// Sets the RFCOMM Channel Number value in the service record. /// </summary> /// - /// <remarks> /// <para>Note: We use an <see cref="T:System.Int32"/> for the /// <paramref name="psm"/> parameter as its natural type <see cref="T:System.UInt16"/> /// in not usable in CLS Compliant interfaces. /// </para> /// </remarks> /// - /// <param name="record">The <see cref="T:InTheHand.Net.Bluetooth.ServiceRecord"/> /// in which to set the L2CAP PSM value. /// </param> /// <param name="psm">The PSM value to set in the record. /// Note that although the parameter is of type <see cref="T:System.Int32"/> /// the value must actually be in the range of a <see cref="T:System.UInt16"/>, /// see the remarks for more information. /// </param> /// - /// <exception cref="T:System.InvalidOperationException">The /// <see cref="F:InTheHand.Net.Bluetooth.AttributeIds.UniversalAttributeId.ProtocolDescriptorList"/> /// attribute is missing or contains invalid elements. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// The PSM must fit in a 16-bit unsigned integer. /// </exception> public static void SetL2CapPsmNumber(ServiceRecord record, int psm) { if (psm < 0 || psm > UInt16.MaxValue) { throw new ArgumentOutOfRangeException("psm", "A PSM is a UInt16 value."); } var psm16 = checked ((UInt16)psm); ServiceElement rfcommElement = GetRfcommChannelElement(record); if (rfcommElement != null) { Debug.WriteLine("Setting L2CAP PSM for a PDL that includes RFCOMM."); } ServiceElement channelElement = GetChannelElement(record, BluetoothProtocolDescriptorType.L2Cap); if (channelElement == null || channelElement.ElementType != ElementType.UInt16) { throw new InvalidOperationException("ProtocolDescriptorList element does not exist, is not in the L2CAP format, or it the L2CAP layer has no PSM element."); } channelElement.SetValue(psm16); }