コード例 #1
0
        public Toio GetToio(int n)
        {
            Toio t = null;

            lock (lockObj)
            {
                t = toioList.ElementAt(n);
            }
            return(t);
        }
コード例 #2
0
        private void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            lock (lockObj)
            {
                if (toioList.Count(t => t.Address == args.BluetoothAddress) > 0)
                {
                    return;
                }
            }

            var bleServiceUUIDs = args.Advertisement.ServiceUuids;

            foreach (var uuid in bleServiceUUIDs)
            {
                if (uuid == Toio.ServiceUUID)
                {
                    Task task = Task.Run(async() =>
                    {
                        BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

                        GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesForUuidAsync(Toio.ServiceUUID);

                        if (result.Status == GattCommunicationStatus.Success)
                        {
                            var service = result.Services[0];

                            Toio toio = new Toio(args.BluetoothAddress, service);

                            // Test
                            byte battery = toio.ReadBatteryLife();

                            lock (lockObj)
                            {
                                toioList.Add(toio);
                            }

                            if (newlyFound != null)
                            {
                                newlyFound(toio);
                            }
                        }
                    });
                    task.Wait();
                }
            }
        }