Bluetooth: Unpaired Bluetooth LE Device Connectivity - Part 2 of 3

Sign in to queue

Description

Pair no more! With the Windows 10 Creators Update, you can now communicate with your Bluetooth Smart (LE) devices without pairing.

Day:

0

Session Type:

Pre-Recorded

Code:

P4178

Embed

Download

The Discussion

  • User profile image
    Dan_Ardelean

    You should put the link to Team blog and also the slides

  • User profile image
    Bill

    I checked the blog and could not find the sample code.
    Is it going to be available as pieces of the code in video are not shown

  • User profile image
    RobZ76

    For People needing the sample code (like me), i got the BluetoothLEClient from Windows-universal-samples-master and changed some things (as shown in the video), changed StartBleDeviceWatcher() and ConnectButton_Click(), hope it helps, see changes below:

    private void StartBleDeviceWatcher()
    {
    EnumerateButton.Content = "Stop enumerating";
    // Additional properties we would like about the device.
    string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.Bluetooth.Le.IsConnectable" };
    string aqsAllBluetoothLEDevices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";
    //string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.Bluetooth.Le.IsConnectable" };
    //string aqsAllBluetoothLEDevices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-75eaa7084d49}\")";
    //string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
    //string aqsAllBluetoothLEDevices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";

    // BT_Code: Currently Bluetooth APIs don't provide a selector to get ALL devices that are both paired and non-paired.
    deviceWatcher =
    DeviceInformation.CreateWatcher(
    aqsAllBluetoothLEDevices,
    requestedProperties,
    DeviceInformationKind.AssociationEndpoint);

    // Register event handlers before starting the watcher.
    deviceWatcher.Added += DeviceWatcher_Added;
    deviceWatcher.Updated += DeviceWatcher_Updated;
    deviceWatcher.Removed += DeviceWatcher_Removed;
    deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
    deviceWatcher.Stopped += DeviceWatcher_Stopped;

    // Start over with an empty collection.
    ResultCollection.Clear();

    // Start the watcher.
    deviceWatcher.Start();
    }

    private async void ConnectButton_Click() {

    ConnectButton.IsEnabled = false;
    ClearBluetoothLEDevice();
    try {
    // BT_Code: BluetoothLEDevice.FromIdAsync must be called from a UI thread because it may prompt for consent.
    bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(rootPage.SelectedBleDeviceId);
    } catch (Exception ex) when ((uint)ex.HResult == 0x800710df) {
    // ERROR_DEVICE_NOT_AVAILABLE because the Bluetooth radio is not on.
    }
    if (bluetoothLeDevice != null) {
    // BT_Code: GattServices returns a list of all the supported services of the device.
    // If the services supported by the device are expected to change
    // during BT usage, subscribe to the GattServicesChanged event.
    GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);
    if (result.Status == GattCommunicationStatus.Success) {
    var services = result.Services;
    rootPage.NotifyUser(String.Format("Found {0} Services", services.Count), NotifyType.StatusMessage);
    foreach (var service in services) {
    ServiceCollection.Add(new BluetoothLEAttributeDisplay(service));
    }
    ConnectButton.Visibility = Visibility.Collapsed;
    ServiceList.Visibility = Visibility.Visible;
    }
    } else {
    ClearBluetoothLEDevice();
    rootPage.NotifyUser("Failed to connect to device.", NotifyType.ErrorMessage);
    }
    ConnectButton.IsEnabled = true;
    }

  • User profile image
    bobskiwobski

    @RobZ76: Thanks.  I changed the code as you mentioned -- looks like I can do unpaired connect.  But I don't get list of characteristics.   Any additional hints?

    I'm using the TI SensorTag.  

    Sure would be nice to see the source code.

Add Your 2 Cents