UWP RfComm - StreamSocketListener.BindServiceNameAsync 抛出异常
UWP RfComm - StreamSocketListener.BindServiceNameAsync throws exception
我正在尝试创建一个侦听蓝牙串行端口连接的 UWP 应用程序。当我在应用程序中设置侦听器时(通过单击按钮),我在以下行遇到异常:
await socketListener.BindServiceNameAsync(
rfcommProvider.ServiceId.ToString(),
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);`
异常信息是:
System.ArgumentException: '参数不正确。
'protectionLevel': IP StreamSocketListeners 只允许普通套接字。'
代码如下:
private async void btnBluetoothServerClick(object sender, RoutedEventArgs e)
{
var rfcommProvider =
await RfcommServiceProvider.CreateAsync(RfcommServiceId.SerialPort);
var socketListener = new StreamSocketListener();
socketListener.ConnectionReceived += OnConnectionReceived;
await socketListener.BindServiceNameAsync(
rfcommProvider.ServiceId.ToString(),
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
rfcommProvider.StartAdvertising(socketListener);
}
当 protectionLevel
参数 SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication
更改为 SocketProtectionLevel.PlainSocket
作为异常指示时,新的异常消息为:System.Exception: 'The specified class was not found. (Exception from HRESULT: 0x8007277D)'.
应用清单文件包括:
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="bluetooth" />
<DeviceCapability Name="proximity" />
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
<DeviceCapability Name="wifiControl" />
</Capabilities>
我做错了什么?
只需更改 BindServiceNameAsync
方法的参数,
rfcommProvider.ServiceId.ToString()
至
rfcommProvider.ServiceId.AsString()
那么你的代码片段就可以工作了。
RfcommServiceId
的 AsString()
方法会将 RfcommServiceId
转换为字符串。ToString
会将 return 类型转换为不正确的参数BindServiceNameAsync
.
更多详情请参考official sample。
我正在尝试创建一个侦听蓝牙串行端口连接的 UWP 应用程序。当我在应用程序中设置侦听器时(通过单击按钮),我在以下行遇到异常:
await socketListener.BindServiceNameAsync(
rfcommProvider.ServiceId.ToString(),
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);`
异常信息是: System.ArgumentException: '参数不正确。 'protectionLevel': IP StreamSocketListeners 只允许普通套接字。'
代码如下:
private async void btnBluetoothServerClick(object sender, RoutedEventArgs e)
{
var rfcommProvider =
await RfcommServiceProvider.CreateAsync(RfcommServiceId.SerialPort);
var socketListener = new StreamSocketListener();
socketListener.ConnectionReceived += OnConnectionReceived;
await socketListener.BindServiceNameAsync(
rfcommProvider.ServiceId.ToString(),
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
rfcommProvider.StartAdvertising(socketListener);
}
当 protectionLevel
参数 SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication
更改为 SocketProtectionLevel.PlainSocket
作为异常指示时,新的异常消息为:System.Exception: 'The specified class was not found. (Exception from HRESULT: 0x8007277D)'.
应用清单文件包括:
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="bluetooth" />
<DeviceCapability Name="proximity" />
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
<DeviceCapability Name="wifiControl" />
</Capabilities>
我做错了什么?
只需更改 BindServiceNameAsync
方法的参数,
rfcommProvider.ServiceId.ToString()
至
rfcommProvider.ServiceId.AsString()
那么你的代码片段就可以工作了。
RfcommServiceId
的 AsString()
方法会将 RfcommServiceId
转换为字符串。ToString
会将 return 类型转换为不正确的参数BindServiceNameAsync
.
更多详情请参考official sample。