Windows 10蓝牙低功耗连接c#

时间:2015-10-01 16:49:09

标签: c# windows bluetooth bluetooth-lowenergy windows-10

对于一个项目,我需要使用C#从Windows 10上的蓝牙设备获取一些数据。我对蓝牙API不太熟悉,也无法弄清楚以下原因无效:

使用BluetoothLEAdvertisementWatcher我搜索广告,效果很好。我确实从设备接收广告(本地名称适合)以及ServiceUuids。接下来,我尝试使用与广告一起收到的BluetoothAddress连接到设备:

private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, 
                    BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
    ulong blAdress = eventArgs.BluetoothAddress;
    BluetoothLEDevice blDevice = await 
         Windows.Devices.Bluetooth.BluetoothLEDevice.FromBluetoothAddressAsync(blAdress);
}

然而,这样做会导致异常:

  

找不到元素。 (HRESULT的例外情况:0x80070490)。

这是从设备读取数据的正确方法吗?是否有其他选项可用于从服务中读取数据?在Windows中手动配对设备实际上并不是一种选择,似乎也失败了。

/编辑1:我检查设备的本地名称,以确保我只尝试连接到正确的设备。所以我想连接到这个特定的设备有问题,但我仍然不知道如何解决这个问题。在iOS上成功读取了服务数据,因此应该可以。

6 个答案:

答案 0 :(得分:3)

在MS修复此问题之前,我发现连接到BLE设备的唯一可靠解决方案是向注册表询问配对的BLE设备列表,并将广告中的蓝牙地址与配对的注册表列表进行比较设备。我的经验是,当在未配对的设备上调用FromBluetoothAddressAsync时,Windows会抛出异常并杀死观察者线程。我有一些C ++代码,我很乐意分享它读取注册表并创建配对的BLE设备列表。

希望MS能像苹果那样花时间全力支持BLE。

答案 1 :(得分:2)

以下是MS(https://social.msdn.microsoft.com/Forums/vstudio/en-US/e321cb3c-462a-4b16-b7e4-febdb3d0c7d6/windows-10-pairing-a-ble-device-from-code?forum=wdk)的参考资料。似乎要使用这个BluetoothLEDevice.FromBluetoothAddressAsync,我们必须在设备广告但尚未配对时处理异常。

答案 2 :(得分:1)

当我直接使用BluetoothLEAdvertisementWatcher时,我遇到了同样的问题。

然后我测试了观察者列出的不同地址。我发现它与蓝牙设备有关。

按照以下方式添加过滤器后,我可以成功连接到GATT设备(TI传感器标签)。

public sealed partial class MainPage : Page
{
    private BluetoothLEAdvertisementWatcher watcher;

    public MainPage()
    {
        this.InitializeComponent();

        // Create and initialize a new watcher instance.
        watcher = new BluetoothLEAdvertisementWatcher();

        // Part 1B: Configuring the signal strength filter for proximity scenarios

        // Configure the signal strength filter to only propagate events when in-range
        // Please adjust these values if you cannot receive any advertisement 
        // Set the in-range threshold to -70dBm. This means advertisements with RSSI >= -70dBm 
        // will start to be considered "in-range".
        watcher.SignalStrengthFilter.InRangeThresholdInDBm = -70;

        // Set the out-of-range threshold to -75dBm (give some buffer). Used in conjunction with OutOfRangeTimeout
        // to determine when an advertisement is no longer considered "in-range"
        watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -75;

        // Set the out-of-range timeout to be 2 seconds. Used in conjunction with OutOfRangeThresholdInDBm
        // to determine when an advertisement is no longer considered "in-range"
        watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000);

        // By default, the sampling interval is set to zero, which means there is no sampling and all
        // the advertisement received is returned in the Received event

        // End of watcher configuration. There is no need to comment out any code beyond this point.
    }


    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        watcher.Received += OnAdvertisementReceived;

        watcher.Stopped += OnAdvertisementWatcherStopped;

        App.Current.Suspending += App_Suspending;

        App.Current.Resuming += App_Resuming;
    }


    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        App.Current.Suspending -= App_Suspending;
        App.Current.Resuming -= App_Resuming;

        watcher.Stop();

        watcher.Received -= OnAdvertisementReceived;
        watcher.Stopped -= OnAdvertisementWatcherStopped;

        base.OnNavigatingFrom(e);
    }


    private void App_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
    {
        // Make sure to stop the watcher on suspend.
        watcher.Stop();
        // Always unregister the handlers to release the resources to prevent leaks.
        watcher.Received -= OnAdvertisementReceived;
        watcher.Stopped -= OnAdvertisementWatcherStopped;

    }

    private void App_Resuming(object sender, object e)
    {
        watcher.Received += OnAdvertisementReceived;
        watcher.Stopped += OnAdvertisementWatcherStopped;
    }

    private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
    {
        var address = eventArgs.BluetoothAddress;

        BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(address);

        var cnt =device.GattServices.Count;

        watcher.Stop();
    }

    /// <summary>
    /// Invoked as an event handler when the watcher is stopped or aborted.
    /// </summary>
    /// <param name="watcher">Instance of watcher that triggered the event.</param>
    /// <param name="eventArgs">Event data containing information about why the watcher stopped or aborted.</param>
    private void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
    {

    }

    private void start_Click(object sender, RoutedEventArgs e)
    {
        watcher.Start();
    }
}

答案 3 :(得分:0)

只是一个猜测,但也许你需要这个:

watcher.ScanningMode = BluetoothLEScanningMode.Active;

和OnAdvertisementReceived事件

if (e.AdvertisementType == BluetoothLEAdvertisementType.ScanResponse)
{
  BluetoothLEDevice blDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(e.BluetoothAddress);
}

答案 4 :(得分:0)

如果这是UWP项目,请确保启用蓝牙功能。

要在Visual Studio解决方案资源管理器中执行此操作,请双击* .appxmanifest,选择“功能”选项卡,并确保已选中“蓝牙”。

它将添加一些与以下内容相似的xml;

<Capabilities>
    <Capability Name="internetClientServer" />
    <DeviceCapability Name="bluetooth" />
</Capabilities>

答案 5 :(得分:0)

这个问题已有3年历史了,但由于它有13000多次浏览,我将回答。 找不到Element的原因是Windows.Devices在配对或连接之前,不知道广告Ble设备。 而是在OnAdvertisementReceived中使用:

 var device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);

我在github上也有一个非常简单的uwp示例,它没有控件使它尽可能简单。所有结果都显示在调试输出窗口中。 最有用的信息在MainPage.xaml.cs

签出:https://github.com/GrooverFromHolland/SimpleBleExample_by_Devicename