如何在 Windows 10 上控制蓝牙 LE 连接? [英] How to control a Bluetooth LE connection on Windows 10?

查看:66
本文介绍了如何在 Windows 10 上控制蓝牙 LE 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要开发一个通过蓝牙低功耗与设备通信的应用程序.一旦应用程序通过蓝牙连接到设备,它就会使用 gatt 服务接收和发送数据.

I need to develop an application which communicates with a device via bluetooth low energy. Once the application is connected to the device via bluetooth it receives and sends data by using a gatt service.

应用程序需要在 Windows 10 环境中运行.到目前为止,我能够使用以下 UWP 类开发和试用该应用程序:

The application needs to run on a Windows 10 environment. So far I was able to develop and try the application by using the following UWP classes:

DeviceWatcher

BluetoothLEDevice

一旦设备被发现并配对,通信就会开始.我遇到的主要问题是我无法控制与设备的连接/断开连接,即一旦设备正确且先前配对后连接会自动开始,但我没有找到任何 connect()/disconnect() 方法.

Once the device has been discovered and paired the communication starts. The main problem I have is that I am not able to control the connection/disconnection with the device, that is the connection starts automatically once the device has been properly and previously paired, but I haven't found any connect()/disconnect() method so far.

有没有办法控制与特定蓝牙 LE 设备的连接?是否有其他 API 允许在不使用 UWP 框架的情况下使用蓝牙并提供对蓝牙的更多控制?

Is there a way to control the connection with a specific bluetooth LE device? Are there other APIs which allow to use the bluetooth without using the UWP framework and which offer more control over the bluetooth?

推荐答案

一旦设备配对,只要它靠近 Windows 10 机器打开,它就会尝试连接.这是蓝牙中定义的行为,因为外设在打开时总是会发出连接请求.

Once the device is paired, whenever it turns on close to the Windows 10 machine, it will try to connect. This is defined behavior in Bluetooth, as the peripheral will always send out a connection request when it is turned on.

您可以注册一个 DeviceWatcher 后台任务,以便在您的蓝牙设备连接时触发您的应用程序.您可以在此处找到 一些示例代码.

There is a DeviceWatcher background task that you can register for to trigger your app when your Bluetooth device connects. You can find some sample code here.

有没有办法控制与特定蓝牙 LE 设备的连接?

是的.启动连接:当您通过 FromBluetoothAddressAsyncFromIdAsync 创建 BluetoothLEDevice 时,系统将尝试启动与该外围设备的连接(如果有)尚未建立连接.

Yes. To initiate a connection: when you create a BluetoothLEDevice via FromBluetoothAddressAsync or FromIdAsync the system will try to initiate a connection to that peripheral, if it does not already hold a connection.

// Connects to a Bluetooth device, given some string deviceId
BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(deviceId);

要处理连接,请调用 BluetoothLEDevice 上的 close 方法.如果您的应用程序是唯一具有外围设备句柄的实体,这将导致系统断开连接.但是,如果另一个应用程序或系统服务有外设的句柄,则连接不会关闭.

To dispose of a connection, call the close method on BluetoothLEDevice. If your app is the only entity with a handle to the peripheral, this will cause the system to disconnect. However, if another app or system service has a handle to the peripheral, the connection will not be closed.

// Will disconnect from the BTLE device, if you hold the only handle
bleDevice.close()

这些取自 BluetoothLEDevice 文档这里.

还有其他 API 吗?

没有任何其他内置于 Windows 10 的 API 可以提供对蓝牙的更多控制.UWP API 提供了 Windows 10 当前提供的最多控制.您可以使用备用蓝牙堆栈,但这些堆栈必须单独安装,并且可能会破坏 Windows 10 上的其他蓝牙行为.

There are not any other APIs built in to Windows 10 that offer more control over Bluetooth. The UWP APIs offer the most control that Windows 10 currently provides. You could use an alternate Bluetooth stack, but these would have to be installed separately and likely break other Bluetooth behavior on Windows 10.

这篇关于如何在 Windows 10 上控制蓝牙 LE 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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