Winapi Can';看不到Bluetooth设备
我正在使用蓝牙,我想在一个列表中看到我周围的设备及其名称,我想看到设备的属性、服务和字符,但我看不到这些设备,即使我的手机和耳机的蓝牙已打开,列表总是空的 以下是我的示例代码:Winapi Can';看不到Bluetooth设备,winapi,bluetooth,bluetooth-lowenergy,device,gatt,Winapi,Bluetooth,Bluetooth Lowenergy,Device,Gatt,我正在使用蓝牙,我想在一个列表中看到我周围的设备及其名称,我想看到设备的属性、服务和字符,但我看不到这些设备,即使我的手机和耳机的蓝牙已打开,列表总是空的 以下是我的示例代码: private GattCharacteristic gattCharacteristic; private BluetoothLEDevice bluetoothLeDevice; public List<DeviceInformation> bluetoothLeDevicesLi
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;
}
}
每当我运行此代码时,控制台上既不会出现设备名称也不会出现空文本,只有startWatcher函数起作用并结束。是否有列出所有设备的功能?我怎样才能看到支持的服务和特征UUID?有办法做到这一点吗?您确定您的耳机支持蓝牙低能量而不仅仅是蓝牙经典吗?尝试使用通用BLE扫描仪应用程序(如nRF Connect)查找它们。您只能在不可移动设备宣传其存在时检测它们。我想你的手机不会这样做,这就是为什么你无法检测到它。您可以使用nRF Connect(Android:Configure GATT server,iOS:Peripheral)或通过其他用于模拟可编程外围设备的应用程序从手机启动广告谢谢您的回答。啊,没错,我的耳机不支持低能量,但当我运行此代码时,它会找到我朋友的手机(iphone 11),但看不到我的手机。(iphone7)iphone11和iphone7之间是否存在低能耗支持差异?正如我所说的,你只能找到正在做广告的手机。我也能找到我的iphone12,因为它正在为一些服务做广告。也许你的iPhone没有做到这一点。尝试将“nRF Connect”安装到iPhone,转到“外围设备”并启动广告商。你现在应该可以找到你的手机了
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();
}
}