Windows蓝牙驱动开发之模拟HID设备(二)(把Windows电脑模拟成蓝牙鼠标和蓝牙键盘等设备)

by fanxiushu 2024-03-24 转载或引用请注明原作者
接上文,当我们建立了蓝牙链接请求之后,就该传输数据了,
其实传输数据比起上章阐述的创建SDP和建立连接要简单许多。
使用类型 BRB_L2CA_ACL_TRANSFER 的BRB请求,就可以实现接收和发送操作,
至于具体是接收还是发送,根据设置的标志来区分,

因为我是把接收和发送请求都从驱动传输到应用层来处理的,
因此实现类似下面这样的代码:
static NTSTATUS cdo_ioctl_function(PDEVICE_OBJECT deviceObject, PIRP userIrp)
{
    NTSTATUS status = STATUS_SUCCESS;
    PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(userIrp); ///
    PFILE_OBJECT fileObject = irpStack->FileObject;
    bth_hid_user_t* user = (bth_hid_user_t*)fileObject->FsContext;  ///
    。。。。

    switch (irpStack->Parameters.DeviceIoControl.IoControlCode) 
    {
       。。。其他命令
    case IOCTL_BTH_DATA_TRANSFER:  //定义的用于接收或者发送的IOCTL命令
          status = ioctl_bth_transfer(user, userIrp);
          if (status =

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 C# 代码示例,用于连接 HID 蓝牙键盘并读取数据。请注意,这是一个简单的示例,仅用于演示如何使用 Windows 11 API 连接和读取 HID 蓝牙键盘数据。您需要根据您的具体情况进行修改和调整。 ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.GenericAttributeProfile; namespace BluetoothKeyboard { class Program { static async Task Main(string[] args) { // 获取 Bluetooth 设备 var devices = await BluetoothDevice.GetPairedDevicesAsync(); // 获取 HID 蓝牙键盘的 Service UUID var hidServiceUuid = new Guid("{00001812-0000-1000-8000-00805f9b34fb}"); // 遍历所有配对设备,并连接 HID 蓝牙键盘 foreach (var device in devices) { var services = await device.GetGattServicesForUuidAsync(hidServiceUuid); if (services.Count > 0) { var service = services[0]; var characteristics = await service.GetCharacteristicsAsync(); // 读取 HID 蓝牙键盘数据 var dataCharacteristic = characteristics.FirstOrDefault(c => c.Uuid == new Guid("{00002a4d-0000-1000-8000-00805f9b34fb}")); if (dataCharacteristic != null) { dataCharacteristic.ValueChanged += DataCharacteristic_ValueChanged; await dataCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); } } } Console.ReadKey(); } private static void DataCharacteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { // 处理 HID 蓝牙键盘数据 var data = args.CharacteristicValue.ToArray(); Console.WriteLine($"Received {data.Length} bytes of data."); } } } ``` 请注意,此代码需要在支持 Bluetooth 5.0 的 Windows 11 设备上运行。您还需要在 Visual Studio 中添加对 `Windows.Devices.Bluetooth` 和 `Windows.Devices.Bluetooth.GenericAttributeProfile` 命名空间的引用,以及在应用程序的清单文件中声明对 Bluetooth 权限的访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值