2

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?

orcunor
  • 61
  • 7
  • 1
    Are you sure that your headphones support Bluetooth Low Energy and not just Bluetooth classic? Try to find them using a generic a generic BLE scanner app such as nRF Connect. You can only detect BLE devices if they are advertising their presence. I assume your phone does not do that and that is why you can't detect it. You can start advertisements from your phone using nRF Connect (Android: Configure GATT server, iOS: Peripheral) or through other apps for simulating a BLE peripheral – Michael Kotzjan May 31 '21 at 06:17
  • thanks for your answer. Ahh right, my headset does not support low energy, but when I run this code, it finds my friend's phone (iphone 11) but does not see my phone. (iphone 7) Is there a low energy support difference between iphone 11 and 7? – orcunor May 31 '21 at 06:45
  • 1
    As I said, you can only find phones that are advertising. I can find my iPhone 12 as well because it is advertising a few services. Maybe your iPhone doesn't do that. Try installing 'nRF Connect' to your iPhone, go to 'Peripheral' and start an advertiser. You should be able to find your phone now – Michael Kotzjan May 31 '21 at 06:49

0 Answers0