Trước đây tôi đã ghép nối với thiết bị Bluetooth hỗ trợ RFCOMM. Khi ứng dụng của tôi được mở, tôi liên tục cố kết nối với thiết bị bằng cách mở RFCOMM. Bằng cách này, ứng dụng của tôi sẽ tự động kết nối khi thiết bị có trong phạm vi phủ sóng.Windows 10 - cách phát hiện khi thiết bị Bluetooth nằm trong phạm vi
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));
}
Sau đó chạy trên một bộ đếm thời gian:
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));
}
Có cách nào để truy vấn khi thiết bị nằm trong phạm vi, chứ không phải cố gắng để kết nối? Tôi chỉ muốn thử kết nối khi thiết bị ở trong phạm vi phủ sóng.