Ejemplo n.º 1
0
        /// <summary>
        /// Connect with BLE Authenticator devices
        /// </summary>
        public async Task <bool> ConnectAsync(ulong bluetoothAddress)
        {
            bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress);

            if (checkDeviceInformation)
            {
                // Debug DeviceInformationチェック | check
                var devinfo = new FidoDeviceInformation();
                if (await devinfo.IsFidoDevice(bleDevice, "") == false)
                {
                    return(false);
                }

                Logger.Log($"Device Information ManufacturerName: {devinfo.ManufacturerNameString}");
                Logger.Log($"Device Information ModelNumberString: {devinfo.ModelNumberString}");
                Logger.Log($"Device Information SerialNumberString: {devinfo.SerialNumberString}");
            }

            // イベントハンドラ追加 | Added event handler
            bleDevice.ConnectionStatusChanged += onConnectionStateChange;

            {
                // FIDOのサービスをGET | GET FIDO Service
                service_Fido = await this.getFIDOService(bleDevice);

                if (service_Fido == null)
                {
                    // サービス無し | No service
                    Logger.Err("Error Connect FIDO Service");
                    return(false);
                }

                // Characteristicアクセス | Characteristic access
                // - コマンド送信ハンドラ設定 | Command transmission handler setting
                // - 応答受信ハンドラ設定 | Response reception handler setting
                {
                    if (PacketSizeByte <= 0)
                    {
                        // FIDO Control Point Length(Read-2byte)
                        var readVal = await readCharacteristicValue(service_Fido, Common.Gatt_Characteristic_FIDO_Control_Point_Length_GUID);

                        if (readVal != null)
                        {
                            this.PacketSizeByte = g.FIDO2.Common.ToUInt16(readVal, 0, true);
                            Logger.Log($"Got PacketSize: {this.PacketSizeByte}");
                        }
                    }

                    /*
                     * // FIDO Service Revision(Read)
                     * await DebugMethods.OutputLog(Service_Fido, GattCharacteristicUuids.SoftwareRevisionString);
                     *
                     * // FIDO Service Revision Bitfield(Read/Write-1+byte)
                     * await DebugMethods.OutputLog(Service_Fido, new Guid("F1D0FFF4-DEAA-ECEE-B42F-C9BA7ED623BB"));
                     */

                    // FIDO Status(Notify) 受信データ | received data
                    {
                        var characteristics = await service_Fido.GetCharacteristicsForUuidAsync(Common.GATT_CHARACTERISTIC_FIDO_STATUS_GUID);

                        if (characteristics.Characteristics.Count <= 0)
                        {
                            Logger.Err("Error Connect Characteristic FIDO Status(Notify)");
                            return(false);
                        }
                        this.characteristic_Receive = characteristics.Characteristics.First();
                        if (this.characteristic_Receive == null)
                        {
                            Logger.Err("Error Connect Characteristic FIDO Status(Notify)");
                            return(false);
                        }

                        receiver            = new CTAPBLEReceiver();
                        receiver.KeepAlive += this.KeepAlive;
                        if (this.characteristic_Receive.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
                        {
                            // イベントハンドラ追加 | Added event handler
                            this.characteristic_Receive.ValueChanged += receiver.OnReceiveFromDevice;

                            // これで有効になる | This will enable
                            //A read permission error can also be the result of the authenticator sending incorrect response data
                            await this.characteristic_Receive.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
                        }
                    }

                    // FIDO Control Point(Write) 送信データ | Transmission data
                    {
                        var characteristics = await service_Fido.GetCharacteristicsForUuidAsync(Common.GATT_CHARACTERISTIC_FIDO_CONTROL_POINT_GUID);

                        if (characteristics.Characteristics.Count <= 0)
                        {
                            Logger.Err("Error Connect CharacteristicFIDO Control Point(Write)");
                            return(false);
                        }

                        this.characteristic_Send = characteristics.Characteristics.First();
                        if (this.characteristic_Send == null)
                        {
                            Logger.Err("Error Connect Characteristic FIDO Control Point(Write)");
                            return(false);
                        }
                    }

                    Logger.Log("Connect BLE Authenticator!");
                    ConnectedDevice?.Invoke(this, EventArgs.Empty);
                }
            }
            return(true);
        }