Windows 10 - 如何检测蓝牙设备是否在范围内

7

我之前已经与一个支持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));
            }

有没有办法查询设备何时在范围内,而不是尝试连接?我希望只在设备在范围内时尝试连接。
1个回答

2
对于 RFCOMM(BT2.0,BT2.1),您可以定期运行设备枚举,同时请参阅 获取范围内的蓝牙设备。但是,使用连接尝试的实际实现可能会更好一些。
对于蓝牙 4.0,您可以侦听 BT 模块的广告,同时请参阅 https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BluetoothAdvertisement
如果您正在与嵌入式设备(例如某些机器人或使用 RFCOMM 的自制家用电器)通信,恐怕没有比您所做的更好的解决方案。
如果您正在与支持 BT4.0 和 BT2.1 的手机通信,则可以使用 BT4 广告来信号该设备的接近度,然后通过 RFCOMM 进行连接。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接