我有一个数字人机接口设备,我正在尝试使用 32feet.net 连接到它,这样我就可以从中读取输入数据并在我的应用程序中处理输出。我以前从未使用蓝牙编程过,并且正在边学习边编程。
我可以使用 microsoft/broadcom 堆栈将我的设备连接到 Windows 7,没有问题。我还可以使用 32feet.net 发现该设备,但当我尝试连接它时出现错误。我使用 BluetoothRadio.IsSupported 确保我的堆栈受支持,这是一个代码片段:
var client = new BluetoothClient();
var dlg = new SelectBluetoothDeviceDialog();
DialogResult result = dlg.ShowDialog(this);
if (result != DialogResult.OK)
{
return;
}
BluetoothDeviceInfo device = dlg.SelectedDevice;
BluetoothAddress addr = device.DeviceAddress;
Console.WriteLine(device.DeviceName);
Guid serviceClass = BluetoothService.HumanInterfaceDevice;
client.Connect(new BluetoothEndPoint(addr, serviceClass));
最后一行会导致以下错误:
System.dll 中发生“System.Net.Sockets.SocketException”类型的第一次机会异常
我还尝试了一些其他方法与 client.Connect 。我尝试使用 DiscoverDevices 获取 DeviceInfo 数组,并手动从该数组中选择设备并连接到它。我尝试将 serviceClass 设置为 Empty,因为这就是我使用 DeviceInfo.ClassOfDevice.Service 时显示的内容。我已经能够使用 DeviceInfo.SetServiceState(BluetoothService.HumanInterfaceDevice, true) 将设备连接到 Windows,但无法从中获取流。
即使在 Windows 中连接后我可以从设备获取数据流,那也没关系。我的目标很简单,就是能够像按下按钮一样从设备读取输入。
尝试这个:
var client = new BluetoothClient();
var dlg = new SelectBluetoothDeviceDialog();
DialogResult result = dlg.ShowDialog(this);
if (result != DialogResult.OK)
{
return;
}
BluetoothDeviceInfo device = dlg.SelectedDevice;
BluetoothAddress addr = device.DeviceAddress;
Console.WriteLine(device.DeviceName);
BluetoothSecurity.PairRequest(addr, "Whatever pin");
device.SetServiceState(BluetoothService.HumanInterfaceDevice, true);
Thread.Sleep(100); // Just in case
if (device.InstalledServices.Length == 0)
{
// I wouldn't know why it doesn't install the service
}
client.Connect(addr, BluetoothService.HumanInterfaceDevice);
无论如何,我都不是专家,但对我来说:
- 我需要配对设备如果该设备未显示在蓝牙设备中。您只需要对通过蓝牙无线电发现的每个新设备执行此操作一次,或者如果您卸载它,我每次都会执行此操作,以防万一选择了新设备。
-
启动服务,一旦我执行此操作,设备将显示设备属性中的“服务”下有一个串行端口(在您的情况下为 HID)可用。
- Connect
在每个阶段都需要发生一些事情:
- 确保该设备出现在“控制面板”的“蓝牙设备”中。
- 在设备“属性”内的“服务”选项卡下,应该显示“人机界面”或其他内容。
- 该服务应该被勾选。
另一方面,您得到的异常确实很神秘,但这只是让我思考设备是否尚未连接到某些东西。如果您检查“device.Connected”,它必须返回 false,否则您将永远无法连接。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)