看不到蓝牙设备

问题描述

我正在开发 BluetoothLE,我想在一个列表中看到我周围的设备及其名称,我想查看设备的属性、服务和字符,但我看不到这些设备即使我的手机和耳机的蓝牙都打开了,列表总是空的。

这是我的示例代码

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

我通过 nrF Connect 应用程序在我的 iphone 上开始了一些新广告,我可以在我的电脑上看到它,但我想将它们全部放在一个列表中并浏览该列表。

这是 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();
  }
}

每当我运行此代码时,控制台上都不会出现设备名称和空文本,只有 startWatcher 函数起作用并结束。有没有列出所有设备的功能?以及如何查看支持的服务和特征 uuid?有没有办法做到这一点?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)