Exemplo n.º 1
0
        protected async virtual Task <Peripheral> FindFirstGrayBlueAsync()
        {
            if (grayBlueCentral.KnownDevices.Any())
            {
                // 接続済みデバイスがある場合はそれを基にして返す
                var knownBleDevice = grayBlueCentral.KnownDevices.First();
                Debug.Log($"device already exist {knownBleDevice.ID}");
                return(new Peripheral(knownBleDevice));
            }
            // GrayBlueの有効化
            var valid = await grayBlueCentral.ValidateAsync();

            if (!valid)
            {
                Debug.Log("GrayBlue validate failed");
                return(null);
            }
            // スキャン
            var deviceIds = await grayBlueCentral.ScanAsync();

            // 接続
            if (deviceIds == null || deviceIds.Length < 1)
            {
                Debug.Log("GrayBlue not found");
                return(null);
            }
            Debug.Log($"GrayBlue found {deviceIds.Length} devices");
            return(await ConnectAsync(deviceIds[0]));
        }