Windows 10 Bluetooth Low Energy C #

For a project, I need to get some data from a Bluetooth device in Windows 10 using C #. I am not very familiar with the Bluetooth API and cannot understand why the following does not work:

Using BluetoothLEAdvertisementWatcher, I'm looking for an ad that works fine. I get ads from the device (the local name is suitable) as well ServiceUuids. Then I try to connect to the device with the help BluetoothAddressobtained with the advertisement:

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

However, this leads to an exception:

Item not found. (Exception from HRESULT: 0x80070490).

Is it right to read data from the device? Are other options available for reading data from services? Manually pairing a device in windows is not really an option, and doesn't seem to work either.

/ 1: , , . , , , . iOS, .

+4
6

, MS , BLE - BLE Bluetooth . , FromBluetoothAddressAsync , Windows . ++, , BLE.

, MS , BLE , Apple.

+3

, BluetoothLEAdvertiseWatcher.

, . , Bluetooth.

, , GATT (TI Sensor Tag).

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();
    }
}
+1

, , , :

watcher.ScanningMode = BluetoothLEScanningMode.Active;

OnAdvertisementReceived

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

UWP, , Bluetooth.

Visual Studio *.appxmanifest, "" , "Bluetooth".

xml, ;

<Capabilities>
    <Capability Name="internetClientServer" />
    <DeviceCapability Name="bluetooth" />
</Capabilities>
Hide result
0

3 , , 13000 , . , , , Windows.Devices Ble, . OnAdvertReceived :

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

UWP GitHub, , . . MainPage.xaml.cs

check this out: https://github.com/GrooverFromHolland/SimpleBleExample_by_Devicename

0
source

Source: https://habr.com/ru/post/1609828/


All Articles