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

Bluetooth Windows 10-如何检测蓝牙设备是否在范围内,bluetooth,windows-runtime,windows-10,rfcomm,Bluetooth,Windows Runtime,Windows 10,Rfcomm,我以前曾与支持RFCOMM的蓝牙设备配对。 当我的应用程序打开时,我会不断尝试通过打开RFCOMM连接到设备。这样,当设备进入范围时,我的应用程序会自动连接 deviceInfoCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)); LogData(String.Format("Number of ml

我以前曾与支持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),您可以定期运行设备枚举,另请参阅

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));
        }
但是,您的实际连接实现可能会更好一些

对于Bluetooth 4.0,您可以收听BT模块的广告,另请参阅

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

如果您正在使用一部电话(支持BT4.0和BT2.1),您可以使用BT4广告来表示设备的接近程度,然后通过RFCOMM进行连接