pog*_*man 7 bluetooth rfcomm windows-runtime windows-10
我之前已经与支持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));
}
Run Code Online (Sandbox Code Playgroud)
然后在计时器上运行:
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));
}
Run Code Online (Sandbox Code Playgroud)
有没有办法查询设备何时在范围内,而不是尝试连接?我宁愿只在设备处于范围内时尝试连接.
对于 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 进行连接。
| 归档时间: |
|
| 查看次数: |
1539 次 |
| 最近记录: |