首页 > 解决方案 > Can't see BlueetoothLE devices

问题描述

I'm working on BluetoothLE, I want to see the devices around me in a list with their names, and I want to see the properties, services and characters of a device, but I can't see these devices even though the bluetooth of my phone and headset is turned on, the list always comes up empty.

Here is my sample code:

    private GattCharacteristic gattCharacteristic;
    private BluetoothLEDevice bluetoothLeDevice;
    public List<DeviceInformation> bluetoothLeDevicesList = new List<DeviceInformation>();
    public DeviceInformation selectedBluetoothLeDevice = null;
  
    public bool IsScannerActiwe { get; set; }
    public bool ButtonPressed { get; set; }
    public bool IsConnected { get; set; }


    public static string StopStatus = null;
    public BluetoothLEAdvertisementWatcher watcher;
    private DeviceWatcher deviceWatcher;        



    public void StartWatcher()
    {
        try
        {
            string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.IsPresent", "System.Devices.Aep.ContainerId", "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.Manufacturer", "System.Devices.Aep.ModelId", "System.Devices.Aep.ProtocolId", "System.Devices.Aep.SignalStrength" };

            deviceWatcher =
                      DeviceInformation.CreateWatcher(
                          BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
                              requestedProperties,
                              DeviceInformationKind.AssociationEndpoint);

            // Register event handlers before starting the watcher.
            // Added, Updated and Removed are required to get all nearby devices
            deviceWatcher.Added += DeviceWatcher_Added;
            deviceWatcher.Updated += DeviceWatcher_Updated;
            deviceWatcher.Removed += DeviceWatcher_Removed;

            // EnumerationCompleted and Stopped are optional to implement.
            deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
            deviceWatcher.Stopped += DeviceWatcher_Stopped;


            // Start the watcher.
            deviceWatcher.Start();
            
        }

        catch (Exception ex)
        {

            Console.WriteLine("Exception -> ", ex.Message);
        }


    }

 public List<DeviceInformation> GetBluetoothLEDevicesList()
    {
        try
        {

            return bluetoothLeDevicesList;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception Handled -> GetBluetoothLEDevices: " + ex);
            throw ex;
        }
    }

    public List<string> GetBluetoothLEDevices()
    {
        try
        {
            return bluetoothLeDevicesList.Select(x => x.Name).ToList();
        }
        catch (System.Exception ex)
        {
            Trace.WriteLine("Exception Handled -> GetBluetoothLEDevices: " + ex);
            throw ex;
        }
    }

I started a few new advertisements with my iphone via the nrF Connect application and I can see it on my pc, but I want to put them all in a list and navigate through that list.

This is Test.cs:

class Test
{
 static void Main(string[] args)
{

    // Since the StartWatcher method is called from within the constructor method, 
      that method will always run whenever an instance is created.

    BLEControllers bLEControllers = new BLEControllers();

   var devices =  bLEControllers.GetBluetoothLEDevicesList();
    foreach (var device in devices)
    {
        if (device != null)
        {
            Console.WriteLine(device.Name);
        }
        Console.WriteLine("empty");
    }
    
    //bLEControllers.ConnectDevice(device);
    //bLEControllers.ConnectDevice();


    Console.Read();
  }
}

Whenever I run this code, neither device names nor empty text appears on the console, only the startWatcher function works and ends. Is there any function to list all the devices? And how can I see the supported service and characteristic uuids? is there a way to do this?

标签: winapibluetoothbluetooth-lowenergydevicegatt

解决方案


推荐阅读