Share via

DeviceInformation.CreateFromIdAsync(deviceId) crashed

longevity uyl 285 Reputation points
2023-11-07T03:21:48.57+00:00

The following code crashed when deviceId="BluetoothLE#BluetoothLEd8:f8:83:9f:72:1a-c4:4d:87:46:02:3a"

DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync(deviceId);

And I get deviceId from member function GetId() of Device class. Do you have suggestion to fix this issue?

internal abstract class Device

{

    private readonly Windows.Devices.Enumeration.DeviceInformation info;

    protected abstract bool IsConnected { get; }

    public ConnectionStatus ConnectionStatus =>

        IsConnected

            ? ConnectionStatus.Connected

            : PairingInfo.IsPaired

                ? ConnectionStatus.Paired

                : ConnectionStatus.NotPaired;

    public Windows.Devices.Enumeration.DeviceInformationPairing PairingInfo => info.Pairing;

    public DeviceInfoId Id { get; }

    public string Name => info.Name;

    protected Device(Windows.Devices.Enumeration.DeviceInformation info)

    {

        this.info = info;

        Id = new DeviceInfoId(info);

    }

    public string GetId() {

        return info.Id;

    }

    public override string ToString()

    {

        return $"name:'{Name}' mac:'{Id.DeviceMac}' type:'{Id.DeviceType}' ConnectionStatus:'{ConnectionStatus}'";

    }

}


Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.