Exception while accessing RfcommDeviceService from a wearable in UWP

0

Here is what I have,

  1. A bluetooth wearable (MyoArm band).
  2. Windows Mobile 10 with anniversary update.

Both of them are paired properly.

Now, here is what I am trying to do,

  1. I am trying to enumerate the list of all services exposed by the bluetooth device connected to my windows mobile.
  2. I would then like to read input streams, if the service provides one.

I went though MSDN documentation and here is what I have done so far.

P.S. I have added Bluetooth access to the capabilities in the application manifest.

    private async void OnReceiveClick(object sender, RoutedEventArgs e)
    {
        var devices = await DeviceInformation.FindAllAsync();
        IList<DeviceInformation> myBluetoothDevices = new List<DeviceInformation>();
        foreach (var device in devices)
        {
            if (device.Name.Contains("myo"))
            {
                var trace = string.Format("Name: {2} \t  Paired: {3} \t Kind: {1} \t Id: {0}", device.Id, device.Kind, device.Name, device.Pairing?.IsPaired);
                builder.AppendLine(trace);
                myBluetoothDevices.Add(device);
            }
        }

        foreach (var myBluetoothDevice in myBluetoothDevices)
        {
            try
            {
                if (myBluetoothDevice != null)
                {
                    var service = await RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id);
                    // TODO: Read input stream somehow here!!!
                    log.Text = builder.AppendLine(string.Format("Name: {0} \t Id: {1} \t Device Info Name: {2} \t Connection Host Name: {3} \t Service Id: {4}", service.Device.Name, service.Device.DeviceId, service.Device.DeviceInformation.Name, service.ConnectionHostName, service.ServiceId.Uuid)).ToString();
                }
            }
            catch (Exception ex)
            {
                builder.AppendLine(ex.Message);
            }
            finally
            {
                log.Text = builder.ToString();
            } 
        }
    }

When I run the code and click the "Receive" button, I get an exception while calling the RfcommDeviceService.FromIdAsync method.

Exception: Element not found. (Exception from HRESULT: 0x80070490)

Am I missing something here? I am new to programming with bluetooth devices, so am I approaching the problem correctly?

c#
.net
bluetooth
uwp
windows-mobile
asked on Stack Overflow Dec 8, 2016 by sudarsanyes

1 Answer

1

Firstly, please ensure the devices queried out by device name are Bluetooth devices since you find all devices not only Bluetooth devices for query. For find Bluetooth devices, DeviceWatcher is recommended and sample code please reference StartUnpairedDeviceWatcher() method in this file.

Secondly, I'm not sure why RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id); cannot get a RfcommDeviceService instance but the official sample is not using this method for getting the service. It got the BluetoothDeivce firstly and then GetRfcommServices from the device.

 var bluetoothDevice = await BluetoothDevice.FromIdAsync(myBluetoothDevice.Id);
 var rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(Constants.RfcommChatServiceUuid)); 
 if (rfcommServices.Services.Count > 0)
 {
     service = rfcommServices.Services[0];
 }

The RfcommServiceId is same as RfcommServiceProvider created. Details please reference the official sample which I have tested can run and find RfcommDeviceService instance successfully.

answered on Stack Overflow Dec 13, 2016 by Sunteen Wu

User contributions licensed under CC BY-SA 3.0