none
is BluetoothLEDevice.FromBluetoothAddressAsync broken or am i using it wrong? RRS feed

  • Question

  • I'm trying to detect Bluetooth le beacon broadcasts. I've checked that the basic machinery works using the BlutoothLEAdvertisment sample in the UWP plafrom samples and I've got publish and listen working from both computers involved with that sample. So hardware and some functionality is fine.

    The reason i'm doing this is for a Xamarin cross platform project and to work with Bluetooth in a cross platform way there is a project https://github.com/aritchie/bluetoothle that aims to encapsulate everything. The problem is it won't work. It doesn't crash it just doesn't produce any data in a UWP project. I've pulled it apart and worked out what's going on and the problem is the call BluetoothLEDevice.FromBluetoothAddressAsync, it's used a bit like this:

    private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
    {
    	var device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
    	if (device==null)
    	{
    		Debug.WriteLine("Failed to find device for address " + args.BluetoothAddress.ToString());
    	}
    	else
    	{
    		Debug.WriteLine("Success: found device " + device.Name??"unnamed" + " for address " + args.BluetoothAddress.ToString());
    	}
    }

    No device is ever found, the call always returns null;

    Should this work? It isn't clear from the documentation, https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothledevice , what the returned device is in relation to. Does it fail to return a device because the address doesn't match up to a device on the local machine? in which case can the docs explain this please? Or should it return a device object given any address local or remote, in which case it's just not working.


    • Edited by David J Mart Friday, June 23, 2017 11:39 AM title fix
    Friday, June 23, 2017 11:39 AM

All replies

  • Hi David J Mart,

    As this thread, It seems that the BluetoothLEDevice.FromBluetoothAddressAsync will fail if the device is not yet paired. You can try to test it after you paired the devices.

    Besides, you can also try to use the DeviceInformation.Id to get the Bluetooth LE Device object.

    async void ConnectDevice(DeviceInformation deviceInfo)
    {
        // Note: BluetoothLEDevice.FromIdAsync must be called from a UI thread because it may prompt for consent.
        BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);
        // ...
    }

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 26, 2017 6:37 AM
    Moderator
  • Ok, that makes some sense and it seems the behaviour is by design. In this case it would be good to have the documentation updated to state this.

    I'm attempting to use ibeacon and eddystone which don't involve connections so i'll never be able to get the device object, I don't think I need it but the library needs to be altered to be aware of this. Thanks for the response.

    Monday, June 26, 2017 6:53 PM
  • In discussion with the author it's been pointed out that the thread references is now 2 years old and that the Bluetooth api was updated in the creators update. It seems the author has this particular code working in his environment, which begs the question why doesn't it work for me?

    So, can you clarify if the BluetoothLEDevice.FromBluetoothAddressAsync method call should still return null on an unpaired device with creators update 15063 please? The FromIdAsync method isn't available in an advertisement receipt context so that can't be used. Similarly I can't use the code suggested because I have no paired connection and never will have with beacon advertisements.

    Investigating further if I enable mixed mode debugging all calls to the method in question will output this line:

    onecoreuap\drivers\wdm\bluetooth\user\winrt\device\bluetoothledevice.cpp(418)\Windows.Devices.Bluetooth.dll!0EF75C57: (caller: 0EF7DAE2) ReturnHr(1) tid(8324) 80070490 Element not found.

    which does looks quite like an internal error.

    Tuesday, June 27, 2017 12:39 AM
  • Hi David J Mart,

    >>can you clarify if the BluetoothLEDevice.FromBluetoothAddressAsync method call should still return null on an unpaired device with creators update 15063 please?

    Sorry for my unclear first reply, in Creators update, it should work but return null without pairing, see this blog New Bluetooth features in Windows 10 Creators Update. but I see someone get it work by pairing, you can have a try. It maybe need the device support.

    Alternatively, you can use your developer account to open a support case by visiting here. You will get 1:1 support on it. Please kindly note that your support ticket will be free if it is Microsoft's issue. 

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 28, 2017 3:04 AM
    Moderator
  • Hello David J Mart,

    Do you have any update? Have you got it work?

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 30, 2017 6:29 AM
    Moderator
  • I've started the process of setting up a support contract. I've made no progress and other aspects of the win10 BluetoothLE api are causing me problems as well. If I get anywhere with it i'll update this thread so anyone who has similar problems doesn't get DenverCoder9'ed.
    Friday, June 30, 2017 10:29 AM
  • There is something very wrong with this scenario on the part of Microsoft. THere are many Bluetooth Low Energy devices that do not pair. If what I read above is indeed true, Microsoft has a serious problem to solve.

    The proper sequence a client should make to work with all devices  is

    1. Discover a connectable advertisement

    2. Invoke a connection attempt

    3. When the connection is completed start service discovery

    4. perform read/write/enable operations

    5. handle indications/notifications

    If at any time the client receives an insufficient authentication error, begin pairing

    If at any time the client receives a security request, begin pairing

    If the device supports pairing and becomes paired and bonded then

    when rediscovered

    invoke connection

    ready to roll


    • Edited by Sisansarah Friday, April 19, 2019 4:19 PM
    Friday, April 19, 2019 4:18 PM