public async Task <IPoweredUpBluetoothDevice> GetDeviceAsync(IPoweredUpBluetoothDeviceInfo bluetoothDeviceInfo)
        {
            var deviceId = (bluetoothDeviceInfo is XamarinBluetoothDeviceInfo local) ? local.DeviceIdentifier : throw new ArgumentException("DeviceInfo not created by adapter", nameof(bluetoothDeviceInfo));

            if (!_discoveredDevices.ContainsKey(deviceId))
            {
                CancellationTokenSource cts = new CancellationTokenSource(10000);

                // trigger scan for 10 seconds
                Discover((deviceInfo) =>
                {
                    return(Task.Run(() =>
                    {
                        cts.Cancel(false);
                    }));
                }, cts.Token);

                // 60 seconds will be ignored here, because the cancelation will happen after 10 seconds
                await Task.Delay(60000, cts.Token).ContinueWith(task => { });

                if (!_discoveredDevices.ContainsKey(deviceId))
                {
                    throw new NotSupportedException("Given bt address does not belong to a discovered device");
                }
            }

            return(new XamarinPoweredUpBluetoothDevice(_discoveredDevices[deviceId], _bluetoothAdapter));
        }
Ejemplo n.º 2
0
    public async Task <IPoweredUpBluetoothDevice> GetDeviceAsync(IPoweredUpBluetoothDeviceInfo bluetoothDeviceInfo)
    {
        var bluetoothAddress = (bluetoothDeviceInfo is PoweredUpBluetoothDeviceInfoWithMacAddress local) ? local.MacAddressAsUInt64 : throw new ArgumentException("DeviceInfo not created by adapter", nameof(bluetoothDeviceInfo));

        var device = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress);

        return(new WinRTPoweredUpBluetoothDevice(device));
    }
 public bool Equals(IPoweredUpBluetoothDeviceInfo other)
 {
     if (other != null && other is PoweredUpBluetoothDeviceInfoWithMacAddress otherMacAddress)
     {
         return(this.MacAddressAsUInt64 == otherMacAddress.MacAddressAsUInt64);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 4
0
    private static (IPoweredUpBluetoothDeviceInfo bluetoothDeviceInfo, SystemType systemType) FindAndSelectHub(IPoweredUpBluetoothAdapter poweredUpBluetoothAdapter)
    {
        IPoweredUpBluetoothDeviceInfo resultDeviceInfo = default;
        SystemType resultSystemType = default;
        var        devices          = new ConcurrentBag <(int key, IPoweredUpBluetoothDeviceInfo bluetoothDeviceInfo, PoweredUpHubManufacturerData deviceType)>();
        var        cts = new CancellationTokenSource();
        int        idx = 1;

        Console.WriteLine("Scan Started. Please select the Hub (using a number keys or 'q' to terminate):");

        poweredUpBluetoothAdapter.Discover(info =>
        {
            if (devices.FirstOrDefault(kv => kv.bluetoothDeviceInfo.Equals(info)) == default)
            {
                var deviceType = (PoweredUpHubManufacturerData)info.ManufacturerData[1];
                devices.Add((idx, info, deviceType));

                var text = (info is IPoweredUpBluetoothDeviceInfoWithMacAddress mac) ? mac.ToIdentificationString() : "not revealed";

                Console.WriteLine($"{idx}: {(PoweredUpHubManufacturerData)info.ManufacturerData[1]} (with address {text})");

                idx++;
            }

            return(Task.CompletedTask);
        }, cts.Token);

        var input = Console.ReadLine();

        cts.Cancel();

        if (int.TryParse(input, out var key))
        {
            var selected = devices.FirstOrDefault(kv => kv.key == key);

            resultDeviceInfo = selected.bluetoothDeviceInfo;
            resultSystemType = (SystemType)selected.deviceType;

            if (resultDeviceInfo != default)
            {
                Console.WriteLine($"Selected {selected.deviceType} with key {selected.key}");
            }
        }

        return(resultDeviceInfo, resultSystemType);
    }
Ejemplo n.º 5
0
 public Task <IPoweredUpBluetoothDevice> GetDeviceAsync(IPoweredUpBluetoothDeviceInfo bluetoothDeviceInfo)
 {
     return(Task.FromResult <IPoweredUpBluetoothDevice>(MockDevice));
 }