更新:解决方案
我设法通过在计算机(要发送数据的设备)上打开/关闭蓝牙来解决此问题。
我正在尝试32feet.net蓝牙库,但遇到了一些奇怪的问题。当我按下扫描按钮时,我将打开SelectBluetoothDeviceDialog
,并且当BluetoothClient
与BluetoothEndPoint
连接时,将会发生新的事情。
昨天一切正常,但是现在我的代码将在client.Connect(endPoint)
上中断,并且Bluetooth.Connect(device)
由于某种原因返回false。
client.Connect(endPoint)
ErrorCode:10022
System.Net.Sockets.SocketException:'提供了无效的参数C0EEFBED5AAD:0000111e00001000800000805f9b34fb'
private void btnScanBeta_Click(object sender, EventArgs e)
{
var device = Bluetooth.DeviceDialog();
if (!Bluetooth.Connect(device))
{
throw new Exception("Can't connect to device.");
}
BluetoothEndPoint endPoint = new BluetoothEndPoint(device.DeviceAddress, BluetoothService.Handsfree);
using (BluetoothClient client = new BluetoothClient())
{
try
{
client.Connect(endPoint);
}
catch (Exception)
{
throw;
}
}
}
Bluetooth.DeviceDialog()
public static BluetoothDeviceInfo DeviceDialog(bool showAuthenticated = true, bool showRemembered = true, bool showUnknown = true)
{
using (SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog())
{
dialog.ShowAuthenticated = showAuthenticated;
dialog.ShowRemembered = showRemembered;
dialog.ShowUnknown = showUnknown;
if (dialog.ShowDialog() == DialogResult.OK)
{
if (dialog.SelectedDevice != null)
{
return dialog.SelectedDevice;
}
else return null;
}
else return null;
}
}
Bluetooth.Connect()
public static bool Connect(BluetoothDeviceInfo device)
{
if (device.Authenticated)
{
return true;
}
return BluetoothSecurity.PairRequest(device.DeviceAddress, null);
}