无法为 Rfcomm 服务提供商(InTheHand、uwp)设置 SDP 记录

Unable to set SDP Record for Rfcomm Service Provider (InTheHand, uwp)

我想创建双向蓝牙 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 需要在广告开始前绑定。

我最终将 '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 属性的示例。