7

I have previously paired with a Bluetooth device that supports RFCOMM. When my app is opened I continuously try to connect to the device by opening the RFCOMM. This way my app automatically connects when the device comes in range.

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));
        }

Then run this on a timer:

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));
            }

Is there any way to query when the device is in range , rather than trying to connect? I would prefer to only attempt connection when the device is in range.

pogorman
  • 1,641
  • 2
  • 22
  • 41

1 Answers1

2

For RFCOMM (BT2.0, BT2.1) you can run a device enumeration periodically, see also Get bluetooth devices in range

However your actual implementation with a connection attempt may work a little better.

For Bluetooth 4.0, you can listen to the advertisements of the BT module, see also https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BluetoothAdvertisement

If you're talking to an embedded device (e.g. some robot, or homebrew appliances using RFCOMM) I am afraid there is no better solution than what you're doing.

If you're taking to a phone (which supports both BT4.0 and BT2.1) you can use the BT4 advertisements to signal the proximity of the device, then connect via RFCOMM.

Community
  • 1
  • 1
Gee Bee
  • 1,794
  • 15
  • 17