/// <summary>
        /// Connects to known device async.
        ///
        /// https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html
        ///
        /// </summary>
        /// <returns>The to known device async.</returns>
        /// <param name="deviceGuid">Device GUID.</param>
        public async Task <Device> ConnectToKnownDeviceAsync(Guid deviceGuid, ConnectParameters connectParameters = default(ConnectParameters), CancellationToken cancellationToken = default(CancellationToken), bool dontThrowExceptionOnNotFound = false)
        {
            // Wait for the PoweredOn state
            await WaitForState(CBCentralManagerState.PoweredOn, cancellationToken, true);

            if (cancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException("ConnectToKnownDeviceAsync cancelled");
            }

            //FYI attempted to use tobyte array insetead of string but there was a problem with byte ordering Guid->NSUui
            var uuid = new NSUuid(deviceGuid.ToString());

            Trace.Message($"[Adapter] Attempting connection to {uuid}");

            var peripherials = _centralManager.RetrievePeripheralsWithIdentifiers(uuid);
            var peripherial  = peripherials.SingleOrDefault();

            if (peripherial == null)
            {
                var systemPeripherials = _centralManager.RetrieveConnectedPeripherals(new CBUUID[0]);

#if __IOS__
                var cbuuid = CBUUID.FromNSUuid(uuid);
#endif
                peripherial = systemPeripherials.SingleOrDefault(p =>
#if __IOS__
                                                                 p.UUID.Equals(cbuuid)
#else
                                                                 p.Identifier.Equals(uuid)
#endif
                                                                 );

                if (peripherial == null)
                {
                    if (dontThrowExceptionOnNotFound == true)
                    {
                        return(null);
                    }

                    throw new DeviceNotFoundException(deviceGuid);
                }
            }


            var device = new Device(this, peripherial, _bleCentralManagerDelegate, peripherial.Name, peripherial.RSSI?.Int32Value ?? 0, new List <AdvertisementRecord>());

            await ConnectToDeviceAsync(device, connectParameters, cancellationToken);

            return(device);
        }
Esempio n. 2
0
        /// <summary>
        /// Connects to known device async.
        ///
        /// https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html
        ///
        /// </summary>
        /// <returns>The to known device async.</returns>
        /// <param name="deviceGuid">Device GUID.</param>
        public override async Task <IDevice> ConnectToKnownDeviceAsync(Guid deviceGuid, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Wait for the PoweredOn state
            await WaitForState(CBCentralManagerState.PoweredOn, cancellationToken, true);

            if (cancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException("ConnectToKnownDeviceAsync cancelled");
            }

            //FYI attempted to use tobyte array insetead of string but there was a problem with byte ordering Guid->NSUui
            var uuid = new NSUuid(deviceGuid.ToString());

            Trace.Message($"[Adapter] Attempting connection to {uuid.ToString()}");

            var peripherials = _centralManager.RetrievePeripheralsWithIdentifiers(uuid);
            var peripherial  = peripherials.SingleOrDefault();

            if (peripherial == null)
            {
                var systemPeripherials = _centralManager.RetrieveConnectedPeripherals(new CBUUID[0]);

                var cbuuid = CBUUID.FromNSUuid(uuid);
                peripherial = systemPeripherials.SingleOrDefault(p => p.UUID.Equals(cbuuid));

                if (peripherial == null)
                {
                    throw new Exception($"[Adapter] Device {deviceGuid} not found.");
                }
            }


            var device = new Device(this, peripherial, peripherial.Name, peripherial.RSSI?.Int32Value ?? 0, new List <AdvertisementRecord>());

            await ConnectToDeviceAsync(device, false, cancellationToken);

            return(device);
        }
Esempio n. 3
0
        static Task <IReadOnlyCollection <BluetoothDevice> > PlatformGetPairedDevices()
        {
#if __IOS__
            PairedDeviceHandler deviceHandler = new PairedDeviceHandler();
            OnRetrievedPeripherals += deviceHandler.OnRetrievedPeripherals;

            _manager.RetrieveConnectedPeripherals(CBUUID.FromPartial(0x180F));

            return(Task.Run(() =>
            {
                //deviceHandler.WaitOne();
                return deviceHandler.Devices;
            }));
#else
            return(Task.FromResult((IReadOnlyCollection <BluetoothDevice>) new List <BluetoothDevice>().AsReadOnly()));
#endif
        }
Esempio n. 4
0
        static Task <IReadOnlyCollection <BluetoothDevice> > PlatformGetPairedDevices()
        {
#if __IOS__
            PairedDeviceHandler deviceHandler = new PairedDeviceHandler();
            OnRetrievedPeripherals += deviceHandler.OnRetrievedPeripherals;
            List <BluetoothDevice> devices = new List <BluetoothDevice>();
            var periphs = _manager.RetrieveConnectedPeripherals(GattServiceUuids.GenericAccess, GattServiceUuids.GenericAttribute, GattServiceUuids.DeviceInformation, GattServiceUuids.Battery);
            foreach (var p in periphs)
            {
                devices.Add(p);
            }

            return(Task.Run(() =>
            {
                return (IReadOnlyCollection <BluetoothDevice>)devices.AsReadOnly();
            }));
#else
            return(Task.FromResult((IReadOnlyCollection <BluetoothDevice>) new List <BluetoothDevice>().AsReadOnly()));
#endif
        }
Esempio n. 5
0
        static Task <IReadOnlyCollection <BluetoothDevice> > PlatformGetPairedDevices()
        {
#if __IOS__
            PairedDeviceHandler deviceHandler = new PairedDeviceHandler();
            OnRetrievedPeripherals += deviceHandler.OnRetrievedPeripherals;
            List <BluetoothDevice> devices = new List <BluetoothDevice>();
            var periphs = _manager.RetrieveConnectedPeripherals(CBUUID.FromPartial(0x1801));
            foreach (var p in periphs)
            {
                devices.Add(p);
            }

            return(Task.Run(() =>
            {
                return (IReadOnlyCollection <BluetoothDevice>)devices.AsReadOnly();
            }));
#else
            return(Task.FromResult((IReadOnlyCollection <BluetoothDevice>) new List <BluetoothDevice>().AsReadOnly()));
#endif
        }
Esempio n. 6
0
 public List <IDevice> ConnectedDevices()
 {
     return(_centralManager.RetrieveConnectedPeripherals((CBUUID)null).Select(x => new Device(x)).Cast <IDevice>().ToList());
 }