【问题标题】:Unable to connect to Bluetooth LE device, DevicePairingResultStatus.Failed无法连接到蓝牙 LE 设备,DevicePairingResultStatus.Failed
【发布时间】:2018-11-16 15:59:30
【问题描述】:

我正在创建一个跨平台应用程序来连接蓝牙 LE 设备。该应用程序适用于 Android 和 iOS,但无法在 Windows 上连接。当我拨打pairAsync() 时,“正在连接”窗口会弹出一会儿,然后变为“连接失败”。返回状态为 19,DevicePairingResultStatus.Failed,“发生未知故障。”根据MS documentation.

我正在使用cordova-plugin-bluetoothle 来处理跨平台差异。我已经在多台带有内置和 USB 蓝牙适配器的计算机上进行了尝试。

连接代码:

connect: function (successCallback, errorCallback, params) {
    if (!initialized) {
        errorCallback({ error: "connect", message: "Not initialized." });
        return;
    }

    var address = params && params[0] && params[0].address;
    if (!address) {
        errorCallback({ error: "connect", message: "Device address is not specified" });
        return;
    }

    var DeviceInformation = Windows.Devices.Enumeration.DeviceInformation;
    var DeviceInformationKind = Windows.Devices.Enumeration.DeviceInformationKind;

    WinJS.Promise.wrap(address)
    .then(function (deviceAddress) {
        // If we have cached device info return it right now
        if (WATCH_CACHE[deviceAddress]) return [WATCH_CACHE[deviceAddress]];
        // Otherwise try to search it again
        var selector = "System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\" AND " +
                                        "System.Devices.Aep.ContainerId:=\"{" + deviceAddress + "}\" AND " +
                                        "(System.Devices.Aep.CanPair:=System.StructuredQueryType.Boolean#True OR " +
                                        "System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True)";
        return DeviceInformation.findAllAsync(selector, ["System.Devices.Aep.ContainerId"], DeviceInformationKind.associationEndpoint);
    })
    .then(function (devices) {
        return Windows.Devices.Bluetooth.BluetoothLEDevice.fromIdAsync(devices[0].id);
    })
    .then(function (bleDevice) {
        var DevicePairingProtectionLevel = Windows.Devices.Enumeration.DevicePairingProtectionLevel;
        var DevicePairingResultStatus = Windows.Devices.Enumeration.DevicePairingResultStatus;
        var DevicePairingKinds = Windows.Devices.Enumeration.DevicePairingKinds;

        if (bleDevice.deviceInformation.pairing.isPaired) {
            return bleDevice;
        }

        if (!bleDevice.deviceInformation.pairing.canPair) {
            throw { error: "connect", message: "The device does not support pairing" };
        }

        // TODO: investigate if it is possible to pair without user prompt
        return bleDevice.deviceInformation.pairing.pairAsync(DevicePairingProtectionLevel.none)
        .then(function (res) {
            if (res.status === DevicePairingResultStatus.paired ||
                    res.status === DevicePairingResultStatus.alreadyPaired)
                return bleDevice;

                // I modified these two lines to return the actual error message instead of a generic rejection message
                var msg = getDevicePairingResultStatusMessage(res.status);
                throw { error: "connect", message: "(" + res.status + ") " + msg };
        });
    })
    .done(function (bleDevice) {
        var result = {
            name: bleDevice.deviceInformation.name,
            address: address,
            status: "connected"
        };

        // Attach listener to device to report disconnected event
        bleDevice.addEventListener('connectionstatuschanged', function connectionStatusListener(e) {
            if (e.target.connectionStatus === Windows.Devices.Bluetooth.BluetoothConnectionStatus.disconnected) {
                result.status = "disconnected";
                successCallback(result);
                bleDevice.removeEventListener('connectionstatuschanged', connectionStatusListener);
            }
        });

        // Need to use keepCallback to be able to report "disconnect" event
        // https://github.com/randdusing/cordova-plugin-bluetoothle#connect
        successCallback(result, { keepCallback: true });
    }, function (err) {
        errorCallback(err);
    });
}

【问题讨论】:

    标签: javascript windows uwp bluetooth-lowenergy phonegap-plugins


    【解决方案1】:

    bleDevice.deviceInformation.pairing.自定义.Pairasync

    private async void deviceListView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var item = e.ClickedItem as DeviceInformation;
        if (item.Pairing.CanPair)
        {
            var customPairing = item.Pairing.Custom;
            customPairing.PairingRequested += CustomPairing_PairingRequested;
            var result = await customPairing.PairAsync(DevicePairingKinds.ProvidePin);
            customPairing.PairingRequested -= CustomPairing_PairingRequested;
            if ((result.Status == DevicePairingResultStatus.Paired) ||
                (result.Status == DevicePairingResultStatus.AlreadyPaired))
            {
                this.Frame.Navigate(typeof(DevicePage), item);
            }
        }
        else if (item.Pairing.IsPaired == true)
        {
            this.Frame.Navigate(typeof(DevicePage), item);
        }
    }
    
    private void CustomPairing_PairingRequested(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
    {
        args.Accept("123456");
    }
    

    【讨论】:

    • 我也试过return bleDevice.deviceInformation.pairing.custom.pairAsync(DevicePairingKinds.confirmOnly, DevicePairingProtectionLevel.none) .then(function (res) { console.log(res); }, function (res) { console.error(res); }); 并得到相同的 19 状态码
    • 您还必须设置自定义配对事件处理程序。请参阅我的答案中的更新
    • 尝试过bleDevice.deviceInformation.pairing.custom.addEventListener("pairingrequested", function (a) { a.accept(); });,但从未调用过该事件。我确实看到本机弹出窗口询问我是否要连接到设备。
    • 您使用 ConfigmrOnly。您的设备是什么型号,支持哪种配对方式?
    • 这是一个自定义设备,只有几个按钮和灯作为 IO,没有屏幕。我目前正在使用 Microchip Curiosity HPC 开发板和模拟设备蓝牙部分的 Android 应用程序进行测试。
    猜你喜欢
    • 2014-10-04
    • 1970-01-01
    • 2016-07-08
    • 2023-03-22
    • 2013-04-11
    • 1970-01-01
    • 2018-07-07
    • 2016-12-02
    • 2014-12-17
    相关资源
    最近更新 更多