示例#1
0
        public static async Task <bool> OutputLog(GattDeviceService service)
        {
            try {
                Console.WriteLine($"Service.Uuid...{service.Uuid}");
                Console.WriteLine($"Service Name...{DebugMethods.GetServiceUUIDName(service.Uuid)}");
                Console.WriteLine($"Servicev.DeviceId...{service.DeviceId}");

                var characteristics = await service.GetCharacteristicsAsync(BluetoothCacheMode.Cached);

                foreach (var ch in characteristics.Characteristics)
                {
                    Console.WriteLine($"Characteristic...");
                    Console.WriteLine($"...AttributeHandle=0x{ch.AttributeHandle.ToString("X2")}");
                    Console.WriteLine($"...Properties={ch.CharacteristicProperties}");
                    Console.WriteLine($"...ProtectionLevel={ch.ProtectionLevel}");
                    Console.WriteLine($"...UUID={ch.Uuid}");
                }
            } catch (Exception) {
            }
            return(true);
        }
示例#2
0
        public async void CheckArgs(BluetoothLEAdvertisementReceivedEventArgs args)
        {
            try {
                Console.WriteLine("★Scan");

                // console log
                DebugMethods.OutputLog(args);

                // FIDOサービスを検索
                var fidoServiceUuid = new Guid("0000fffd-0000-1000-8000-00805f9b34fb");
                if (args.Advertisement.ServiceUuids.Contains(fidoServiceUuid) == false)
                {
                    return;
                }

                // 発見
                addLog("Scan FIDO Device");

                this.AdvWatcher.Stop();

                // connect
                {
                    addLog("Conncect FIDO Device");
                    BleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

                    DebugMethods.OutputLog(BleDevice);
                }

                // FIDOのサービスをGET
                {
                    addLog("Connect FIDO Service");
                    var services = await BleDevice.GetGattServicesForUuidAsync(fidoServiceUuid);

                    if (services.Services.Count <= 0)
                    {
                        // サービス無し
                        addLog("Error Connect FIDO Service");
                        return;
                    }
                    Service_Fido = services.Services.First();
                }

                // Characteristicアクセス
                // - コマンド送信ハンドラ設定
                // - 応答受信ハンドラ設定
                {
                    // FIDO Service Revision(Read)
                    await DebugMethods.OutputLog(Service_Fido, GattCharacteristicUuids.SoftwareRevisionString);

                    // FIDO Control Point Length(Read-2byte)
                    await DebugMethods.OutputLog(Service_Fido, new Guid("F1D0FFF3-DEAA-ECEE-B42F-C9BA7ED623BB"));

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

                    // FIDO Status(Notiry) 受信データ
                    {
                        var characteristics = await Service_Fido.GetCharacteristicsForUuidAsync(new Guid("F1D0FFF2-DEAA-ECEE-B42F-C9BA7ED623BB"));

                        if (characteristics.Characteristics.Count > 0)
                        {
                            this.Characteristic_Receive = characteristics.Characteristics.First();
                            if (this.Characteristic_Receive == null)
                            {
                                Console.WriteLine("Characteristicに接続できない...");
                            }
                            else
                            {
                                if (this.Characteristic_Receive.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
                                {
                                    // イベントハンドラ追加
                                    this.Characteristic_Receive.ValueChanged += characteristicChanged_OnReceiveFromDevice;

                                    // これで有効になる
                                    await this.Characteristic_Receive.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
                                }
                            }
                        }
                    }

                    // FIDO Control Point(Write) 送信データ
                    {
                        var characteristics = await Service_Fido.GetCharacteristicsForUuidAsync(new Guid("F1D0FFF1-DEAA-ECEE-B42F-C9BA7ED623BB"));

                        if (characteristics.Characteristics.Count > 0)
                        {
                            this.Characteristic_Send = characteristics.Characteristics.First();
                            if (this.Characteristic_Send == null)
                            {
                                Console.WriteLine("Characteristicに接続できない...");
                            }
                        }
                    }

                    addLog("BLE FIDOキーと接続しました!");
                    addLog("");
                }
            } catch (Exception ex) {
                addLog($"Exception {ex.Message}");
            }
        }
示例#3
0
        public static void OutputLog(BluetoothLEAdvertisementReceivedEventArgs args)
        {
            try {
                Console.WriteLine("<< OutputLog >>");

                // タイムスタンプ(スキャン日時?)
                Console.WriteLine($"TimeStamp = {args.Timestamp:HH\\:mm\\:ss}");

                // アドバタイズパケット発信元の Bluetoothデバイスアドレス-48bit(6byte)
                Console.WriteLine($"BluetoothAddress = {args.BluetoothAddress.ToString("X")}");

                // シグナル強度
                Console.WriteLine($"RSSI = {args.RawSignalStrengthInDBm}");

                // アドバタイズパケット種別
                // https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement.bluetoothleadvertisementtype
                Console.WriteLine($"AdvertisementType = {args.AdvertisementType}");
                switch (args.AdvertisementType)
                {
                case BluetoothLEAdvertisementType.ConnectableUndirected:
                    Console.WriteLine($"-> ADV_IND:コネクション可能、スキャン可能");
                    break;

                case BluetoothLEAdvertisementType.ConnectableDirected:
                    Console.WriteLine($"-> ADV_DIRECT_IND:コネクション可能、スキャン×");
                    break;

                case BluetoothLEAdvertisementType.ScannableUndirected:
                    Console.WriteLine($"-> ADV_SCAN_IND:コネクション×、スキャン可能");
                    break;

                case BluetoothLEAdvertisementType.NonConnectableUndirected:
                    Console.WriteLine($"-> ADV_NONCONN_IND:コネクション×、スキャン×");
                    break;

                case BluetoothLEAdvertisementType.ScanResponse:
                    Console.WriteLine($"-> SCAN_RSP:スキャン要求に対するスキャン応答");
                    break;

                default:
                    Console.WriteLine($"-> ???");
                    break;
                }

                // アドバタイズデータ
                Console.WriteLine("");
                Console.WriteLine("<Data>");

                // Flags (DataSectionにも同じ情報あるが、こっちの方が見やすい)
                Console.WriteLine($"Flags = {args.Advertisement.Flags}");

                // LocalName (DataSectionにも同じ情報あるが、こっちの方が見やすい)
                Console.WriteLine($"LocalName = {args.Advertisement.LocalName}");

                // Service UUID (DataSectionにも同じ情報あるが、こっちの方が見やすい)
                {
                    var bleServiceUUIDs = args.Advertisement.ServiceUuids;
                    Console.WriteLine($"Service UUID Num = {bleServiceUUIDs.Count}");
                    foreach (var uuidone in bleServiceUUIDs)
                    {
                        // サービスUUIDってなに?→http://jellyware.jp/kurage/bluejelly/uuid.html
                        Console.WriteLine($"-> Service UUID = {uuidone} -> {DebugMethods.GetServiceUUIDName(uuidone)}");
                    }
                    Console.WriteLine("");
                }

                {
                    var manufacturerSections = args.Advertisement.ManufacturerData;
                    Console.WriteLine($"Manufacturer Num = {manufacturerSections.Count}");
                    if (manufacturerSections.Count > 0)
                    {
                        foreach (var manuone in manufacturerSections)
                        {
                            Console.WriteLine($"<Manufacturer>");
                            Console.WriteLine($"-> CompanyId = {manuone.CompanyId.ToString("X2")}");
                            Console.WriteLine($"-> Data Length = {manuone.Data.Length}");
                            if (manuone.Data.Length > 0)
                            {
                                var data = new byte[manuone.Data.Length];
                                using (var reader = Windows.Storage.Streams.DataReader.FromBuffer(manuone.Data)) {
                                    reader.ReadBytes(data);

                                    var tmp = BitConverter.ToString(data);
                                    Console.WriteLine($"-> Data = {tmp}");
                                }
                            }
                        }
                    }
                    Console.WriteLine("");
                }


                // DataSection
                {
                    var bleDataSections = args.Advertisement.DataSections;
                    Console.WriteLine($"Advertising Data Num = {bleDataSections.Count}");
                    foreach (var datasecone in bleDataSections)
                    {
                        byte type = datasecone.DataType;
                        Console.WriteLine($"<Data>");
                        Console.WriteLine($"-> AD Type = 0x{type.ToString("X2")} -> {DebugMethods.GetADTypeName(type)}");
                        Console.WriteLine($"-> Data Length = {datasecone.Data.Length}");

                        if (datasecone.Data.Length > 0)
                        {
                            var data = new byte[datasecone.Data.Length];
                            using (var reader = Windows.Storage.Streams.DataReader.FromBuffer(datasecone.Data)) {
                                reader.ReadBytes(data);

                                var tmp = BitConverter.ToString(data);
                                Console.WriteLine($"-> Data = {tmp}");
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("Error");
            } finally {
                Console.WriteLine("<< END >>");
            }
            return;
        }