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



我想创建一个双向蓝牙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的 UWP 示例中找到了初始化 SDP 属性的示例。

相关内容

  • 没有找到相关文章