bluetooth - Windows 10 - 如何检测蓝牙设备何时在范围内

标签 bluetooth windows-runtime windows-10 rfcomm

我之前曾与支持 RFCOMM 的蓝牙设备配对过。 当我的应用程序打开时,我不断尝试通过打开 RFCOMM 连接到设备。这样,当设备进入范围时,我的应用程序会自动连接。

deviceInfoCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
        LogData(String.Format("Number of mldp devices is {0}", deviceInfoCollection.Count));
        foreach (DeviceInformation deviceInfo in deviceInfoCollection)
        {
            LogData(String.Format("ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
        }

然后在计时器上运行它:

try
            {
                // The first time this method is invoked by a store app, it should be called 
                // from a UI thread in order to display the consent prompt
                // https://msdn.microsoft.com/en-us/windows.devices.bluetooth.rfcomm.rfcommdeviceservice.fromidasync
                RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(deviceInfo.Id);
                LogData(String.Format("ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
            }
            catch (Exception)
            {
                LogData(String.Format("Can not request rfcomm service from device ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
            }

有什么方法可以查询设备何时在范围内,而不是尝试连接?我宁愿只在设备处于范围内时尝试连接。

最佳答案

对于 RFCOMM(BT2.0、BT2.1),您可以定期运行设备枚举,另请参阅 Get bluetooth devices in range

但是,您通过连接尝试进行的实际实现可能会更好一些。

对于蓝牙4.0,可以收听BT模块的广告,另见https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BluetoothAdvertisement

如果您正在与嵌入式设备(例如某些机器人或使用 RFCOMM 的自制设备)交谈,恐怕没有比您正在做的更好的解决方案了。

如果您使用手机(同时支持 BT4.0 和 BT2.1),您可以使用 BT4 广告来表示设备接近,然后通过 RFCOMM 进行连接。

关于bluetooth - Windows 10 - 如何检测蓝牙设备何时在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830597/

相关文章:

安卓蓝牙打印机连接

c# - Windows 10 UAP 应用程序上的 Google 登录错误

windows - Universal App漫游数据在设备之间不同步

printing - 如何等待WebViewBrush.Redraw()完成(UWP打印)?

c# - 如何在 Windows 10 通用应用程序中显示模态窗口?

c++ - 我正在研究 Arduino Uno。错误 : "' RX' not declared in the scope ."and "duplicate case value"for switch case

Android 和 Windows 通过蓝牙通信

安卓蓝牙耳机连接

c# - 在 Windows Store App 中上传/下载文件到 Google Drive

Windows 8 MDM - 公司应用