call to GetGattServicesAsync throws a The system cannot find the file specified. (Exception from HRESULT: 0x80070002)" exception

oregonduckman 6 Reputation points
2022-07-07T22:49:00.877+00:00

The following code runs OK for a bit the throws a "The system cannot find the file specified. (Exception from HRESULT: 0x80070002)" on the call to GetGattServicesAsync. Any thoughts on this are appreciated.

    private async void WatcherAdvertisementReceivedAsync(Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)  
    {  
        await BluetoothLEDevicesLock.WaitAsync();  
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(  
          Windows.UI.Core.CoreDispatcherPriority.Normal,  
          async () =>  
          {  
                BluetoothLEDevice bluetoothLEDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);  
                if (stop == false && bluetoothLEDevice != null)  
                {  
                        int count = 0;  
                        try  
                        {  
                            GattDeviceServicesResult gattDeviceServicesResult = await bluetoothLEDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);  
                            if (gattDeviceServicesResult != null)  
                            {  
                                count = gattDeviceServicesResult.Services.Count();  
                                if (gattDeviceServicesResult.Services.Count() > 0)  
                                {  
                                    IEnumerable<GattDeviceService> query = from c in gattDeviceServicesResult.Services  
                                                                            where c.Uuid == GattServiceUuids.GenericAccess  
                                                                            select c;  
                                    GattDeviceService service = query.Count() > 0 ? query.First() : null;  
                                    if (service != null)  
                                    {  
                                        GattCharacteristicsResult characteristics = await service.GetCharacteristicsAsync();  
                                        if (characteristics != null)  
                                        {  
                                            IEnumerable<Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic> query2 = from c in characteristics.Characteristics  
                                                                                                                                        where c.Uuid == GattCharacteristicUuids.GapDeviceName  
                                                                                                                                        select c;  
                                            if (query2.Count() > 0)  
                                            {  

                                                GattReadResult readResult = await query2.First().ReadValueAsync();  
                                                byte[] bytes = null;  
                                                string value;  
                                                if (readResult != null && readResult.Value != null)  
                                                {  
                                                    Windows.Storage.Streams.DataReader dataReader;  
                                                    dataReader = Windows.Storage.Streams.DataReader.FromBuffer(readResult.Value);  
                                                    dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;  
                                                    bytes = new byte[readResult.Value.Length];  
                                                    dataReader.ReadBytes(bytes);  
                                                    value = Encoding.Default.GetString(bytes);  
                                                    Debug.WriteLine("************************Device name = " + value);  
                                                }  
                                            }  
                                        }  
                                    }  
                                }  
                            }  
                        }  
                        catch(Exception e)  
                        {  

                        }  
                        bluetoothLEDevice.Dispose();  
                }  
            });  
            BluetoothLEDevicesLock.Release();  
    }
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. oregonduckman 6 Reputation points
    2022-07-08T18:28:13.99+00:00

    Don't know what you mean by "your current ble device info" but the BT chip is from Broadcom running the Microsoft drivers.
    Windows version 10.0.19044 Build 19044
    Min Version 1809 (10.0; Build 17763)

    Below is the code from my test app that was generated by Visual Studio 2022 using the Blank UWP Template included with VS 2022.

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Text;
    using System.Threading;
    using Windows.Devices.Bluetooth;
    using Windows.Devices.Bluetooth.Advertisement;
    using Windows.Devices.Bluetooth.GenericAttributeProfile;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;

    // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

    namespace bleTester
    {
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
    private BluetoothLEAdvertisementWatcher mWatcher = new BluetoothLEAdvertisementWatcher();
    private bool stop = false;
    private SemaphoreSlim BluetoothLEDevicesLock = new SemaphoreSlim(1, 1);
    public MainPage()
    {
    this.InitializeComponent();
    mWatcher.Received += WatcherAdvertisementReceivedAsync;
    }
    private async void WatcherAdvertisementReceivedAsync(Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
    {
    await BluetoothLEDevicesLock.WaitAsync();
    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
    Windows.UI.Core.CoreDispatcherPriority.Normal,
    async () =>
    {
    BluetoothLEDevice bluetoothLEDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
    if (stop == false && bluetoothLEDevice != null)
    {
    int count = 0;
    try
    {
    GattDeviceServicesResult gattDeviceServicesResult = await bluetoothLEDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);
    if (gattDeviceServicesResult != null)
    {
    count = gattDeviceServicesResult.Services.Count();
    if (gattDeviceServicesResult.Services.Count() > 0)
    {
    IEnumerable<GattDeviceService> query = from c in gattDeviceServicesResult.Services
    where c.Uuid == GattServiceUuids.GenericAccess
    select c;
    GattDeviceService service = query.Count() > 0 ? query.First() : null;
    if (service != null)
    {
    GattCharacteristicsResult characteristics = await service.GetCharacteristicsAsync();
    if (characteristics != null)
    {
    IEnumerable<Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic> query2 = from c in characteristics.Characteristics
    where c.Uuid == GattCharacteristicUuids.GapDeviceName
    select c;
    if (query2.Count() > 0)
    {

                                                    GattReadResult readResult = await query2.First().ReadValueAsync();  
                                                    byte[] bytes = null;  
                                                    string value;  
                                                    if (readResult != null && readResult.Value != null)  
                                                    {  
                                                        Windows.Storage.Streams.DataReader dataReader;  
                                                        dataReader = Windows.Storage.Streams.DataReader.FromBuffer(readResult.Value);  
                                                        dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;  
                                                        bytes = new byte[readResult.Value.Length];  
                                                        dataReader.ReadBytes(bytes);  
                                                        value = Encoding.Default.GetString(bytes);  
                                                        Debug.WriteLine("************************Device name = " + value);  
                                                    }  
                                                }  
                                            }  
                                        }  
                                    }  
                                }  
                            }  
                            catch(Exception e)  
                            {  
    
                            }  
                            bluetoothLEDevice.Dispose();  
                    }  
                });  
                BluetoothLEDevicesLock.Release();  
        }  
        private async void Stop(object sender, RoutedEventArgs e)  
        {  
            await BluetoothLEDevicesLock.WaitAsync();  
            mWatcher.Stop();  
            stop = true;  
            BluetoothLEDevicesLock.Release();  
        }  
    
        private void Start(object sender, RoutedEventArgs e)  
        {  
            stop = false;  
            mWatcher.Start();  
        }  
    }  
    

    }


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.