读通过蓝牙原始数据 [英] Reading raw data via Bluetooth

查看:546
本文介绍了读通过蓝牙原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台数码人机接口设备是我试图连接到使用32feet.net这样我就可以从中读取输入数据和处理在我的应用程序的输出。我从来没有编程之前,蓝牙和我学习,我去。

I have a digital Human Interface Device that I'm trying to connect to using 32feet.net so I can read input data from it and process an output in my application. I've never programmed with bluetooth before and am learning as I go.

我能使用Microsoft我的设备连接到Windows 7 /博通堆栈没有问题。我还可以使用32feet.net发现该设备,但是当我尝试连接它,我得到一个错误。我确信我的筹码得到了支持使用BluetoothRadio.IsSupported这里是code片断:

I can connect my device to Windows 7 using the microsoft/broadcom stack no problem. I can also discover the device using 32feet.net but when I try to connect it I get an error. I made sure my stack was supported using BluetoothRadio.IsSupported Here is a code snippet:

 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.Net.Sockets.SocketException在System.dll中发生

The last line causes the following error: A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

我已经尝试了一些其他的方法,以及与client.Connect。我已经使用DiscoverDevices获得DeviceInfo阵列和手动选择设备从该阵列并连接到它尝试。我试着设置serviceClass为空,因为这是显示出来,当我使用DeviceInfo.ClassOfDevice.Service。我已经能够将设备连接到使用DeviceInfo.SetServiceState(BluetoothService.HumanInterfaceDevice,真),但不能从它那里得到一个流的窗口。

I've tried some other methods as well with client.Connect. I've tried using DiscoverDevices to get a DeviceInfo array and manually choosing the device out of that array and connecting to it. I've tried setting the serviceClass as Empty because that is what shows up when I use DeviceInfo.ClassOfDevice.Service. I've been able to connect the device to windows using DeviceInfo.SetServiceState(BluetoothService.HumanInterfaceDevice, true) but not able to get a stream from it.

即使我可以从后它被连接在Windows设备的数据流,这将是罚款。我的目标很简单,能够从设备,如按钮preSS读取输入。

Even if I can get a data stream from the device after it's been connected in Windows, that would be fine. My goal here is simple to be able to read input from the device like a button press.

推荐答案

试试这个:

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);

我不是专家以任何方式,但对我来说:

I am no expert by any means, but for me:

  1. 在我需要的配对的设备如果设备没有在蓝牙设备显示。你只需要使用你发现的蓝牙无线电,或者如果你卸载它,我每次做吧,以防万一新设备选择每一个新的设备,一旦做到这一点。
  2. 开始服务,一旦我做这个设备将显示一个串行端口(在你的情况HID)是下设备的性能服务提供。
  3. 连接
  1. I need to pair the device if the device doesn't show in the Bluetooth devices. You just need to do this once with every new device you discover with the Bluetooth radio, or if you uninstall it, I do it every time anyway just in case a new device is selected.
  2. Start the service, once I do this the device will show that a serial port (in your case HID) is available under "Services" in the device's properties.
  3. Connect

在每一个阶段的东西需要做的:

In every stage something needs to happen:

  1. 确保该设备出现在控制面板中的蓝牙设备。
  2. 在设备内部的属性中的服务选项卡,应该说人机界面什么的。
  3. 在该服务应勾选。

在另一方面,你得到的例外是真正的神秘,但它只是让我觉得如果设备尚未连接到一些东西。如果选中device.Connected它必须返回虚假或你永远不会能够连接。


On another note, the exception that you get is really cryptic, but it just made me think if the device is not already connected to something. If you check "device.Connected" it must return false or you won't ever be able to connect.

这篇关于读通过蓝牙原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆