【问题标题】:Unable to set SDP Record for Rfcomm Service Provider (InTheHand, uwp)无法为 Rfcomm 服务提供商(InTheHand、uwp)设置 SDP 记录
【发布时间】:2019-12-21 22:20:21
【问题描述】:

我想创建双向蓝牙 Rfcomm 连接。服务器端在 UWP 中实现。使用 InTheHand.Devices.Bluetooth (NuGet v4.0.1803.22-preview) 我找不到将 SDP 记录附加到服务提供商的方法。这会导致在尝试宣传服务时出错。

我想可以将“InTheHand”服务提供商转换为 Windows.Devices.Bluetooth.Rfcomm 种类,但如果可能的话,我更喜欢 InTheHand 库中的解决方案。我错过了什么吗?

private async void InitializeService(){
    var localRfcommServiceID = RfcommServiceId.FromUuid(uuid);
    var localRfcommService = await RfcommServiceProvider.CreateAsync(localRfcommServiceID);

//This is where I would expect to add SDP records to the service provider

    localRfcommService.StartAdvertising();
    localRfcommService.ConnectionReceived += LocalRfcommService_ConnectionReceived;
}

我尝试开始投放广告时遇到异常。 (对不起德语错误消息)

Exception thrown: 'System.IO.FileNotFoundException' in InTheHand.Devices.Bluetooth.dll
WinRT information: Der StreamSocketListener muss gebunden werden, bevor Sie mit der Ankündigung beginnen können.
Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.ni.dll
WinRT information: Der StreamSocketListener muss gebunden werden, bevor Sie mit der Ankündigung beginnen können.

翻译:StreamSocketListener 需要在广播开始之前绑定。

【问题讨论】:

  • 嗨,您是否按照错误消息的指示添加了StreamSocketListener
  • 到目前为止,我已经将提供程序转换为 Windows 种类并将其绑定到 StreamSocketLiestener,因为我在 InTheHand 库中找不到明显的绑定方法。似乎正在工作;仍在尝试在客户端发现它。

标签: c# uwp bluetooth rfcomm 32feet


【解决方案1】:

我最终将“InTheHand”RfcommServiceProvider 转换为“Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider”,然后将服务名称绑定到它并初始化 SDP 属性。

async void InitializeService()
{
    var localRfcommProvider = await RfcommServiceProvider.CreateAsync(Constants.RfcommServiceUuid);
    var rfcommServiceID = RfcommServiceId.FromUuid(Constants.RfcommServiceUuid);
    socketListener = new StreamSocketListener();
    socketListener.ConnectionReceived += SocketListener_ConnectionReceived;      
    await socketListener.BindServiceNameAsync(rfcommServiceID.AsString(),SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
    InitializeServiceSdpAttributes(localRfcommProvider);

    try
    {
        ((Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider)localRfcommProvider).StartAdvertising(socketListener);  
    }
    catch (Exception e)
    {
            Debug.WriteLine(e.Message);
    }
}


void InitializeServiceSdpAttributes(Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider provider)
    {
        var sdpWriter = new DataWriter();

        //Write the service name attribute
        sdpWriter.WriteByte(Constants.SdpServiceNameAttributeType);

        // The length of the UTF-8 encoded Service Name SDP Attribute.
        sdpWriter.WriteByte((byte)Constants.SdpServiceName.Length);

        // The UTF-8 encoded Service Name value.
        sdpWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
        sdpWriter.WriteString(Constants.SdpServiceName);

        // Set the SDP Attribute on the RFCOMM Service Provider.
        provider.SdpRawAttributes.Add(Constants.SdpServiceNameAttributeId, sdpWriter.DetachBuffer());
    }

Microsoft's UWP samples 中找到了初始化 SDP 属性的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 2017-05-01
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多