public Descriptor(CBDescriptor nativeDescriptor, CBPeripheral parentDevice, Characteristic characteristic, IBleCentralManagerDelegate bleCentralManagerDelegate) : this(characteristic)
        {
            NativeDescriptor = nativeDescriptor;

            _parentDevice = parentDevice;
            _bleCentralManagerDelegate = bleCentralManagerDelegate;
        }
Exemplo n.º 2
0
 public Service(CBService service, IDevice device, IBleCentralManagerDelegate bleCentralManagerDelegate)
     : base(device)
 {
     _service = service;
     _device  = device.NativeDevice as CBPeripheral;
     _bleCentralManagerDelegate = bleCentralManagerDelegate;
 }
Exemplo n.º 3
0
 public Characteristic(CBCharacteristic nativeCharacteristic, CBPeripheral parentDevice, IService service, IBleCentralManagerDelegate bleCentralManagerDelegate)
     : base(service)
 {
     _nativeCharacteristic      = nativeCharacteristic;
     _parentDevice              = parentDevice;
     _bleCentralManagerDelegate = bleCentralManagerDelegate;
 }
        internal Service(CBService nativeService, Device device, IBleCentralManagerDelegate bleCentralManagerDelegate) : this(device)
        {
            NativeService = nativeService;

            _device = device.NativeDevice as CBPeripheral;
            _bleCentralManagerDelegate = bleCentralManagerDelegate;
        }
Exemplo n.º 5
0
        internal void InitializeNative()
        {
            var cmDelegate = new BleCentralManagerDelegate();

            _bleCentralManagerDelegate = cmDelegate;

            var options = CreateInitOptions();

            _centralManager = new CBCentralManager(cmDelegate, DispatchQueue.CurrentQueue, options);
            _bleCentralManagerDelegate.UpdatedState += (s, e) => State = GetState();
        }
        protected override void InitializeNative()
        {
            var cmDelegate = new BleCentralManagerDelegate();

            _bleCentralManagerDelegate = cmDelegate;

            var options = string.IsNullOrEmpty(_restorationIdentifier)
                ? null
                : new CBCentralInitOptions {
                RestoreIdentifier = _restorationIdentifier
            };

            _centralManager = new CBCentralManager(cmDelegate, DispatchQueue.CurrentQueue, options);
            _bleCentralManagerDelegate.UpdatedState += (s, e) => State = GetState();
        }
Exemplo n.º 7
0
        public Device(Adapter adapter, CBPeripheral nativeDevice, IBleCentralManagerDelegate bleCentralManagerDelegate, string name, int rssi, List <AdvertisementRecord> advertisementRecords)
            : base(adapter)
        {
            _nativeDevice = nativeDevice;
            _bleCentralManagerDelegate = bleCentralManagerDelegate;

            Id   = Guid.ParseExact(_nativeDevice.Identifier.AsString(), "d");
            Name = name;

            Rssi = rssi;
            AdvertisementRecords = advertisementRecords;

            // TODO figure out if this is in any way required,
            // https://github.com/xabre/xamarin-bluetooth-le/issues/81
            //_nativeDevice.UpdatedName += OnNameUpdated;
        }
Exemplo n.º 8
0
 public Device(Adapter adapter, CBPeripheral nativeDevice, IBleCentralManagerDelegate bleCentralManagerDelegate)
     : this(adapter, nativeDevice, bleCentralManagerDelegate, nativeDevice.Name, nativeDevice.RSSI?.Int32Value ?? 0,
            new List <AdvertisementRecord>())
 {
 }
 internal Device(Adapter adapter, CBPeripheral nativeDevice, IBleCentralManagerDelegate bleCentralManagerDelegate)
     : this(adapter, nativeDevice, bleCentralManagerDelegate, nativeDevice.Name, 0,
            new List <AdvertisementRecord>())
 {
 }
Exemplo n.º 10
0
        public Adapter(CBCentralManager centralManager, IBleCentralManagerDelegate bleCentralManagerDelegate)
        {
            _centralManager            = centralManager;
            _bleCentralManagerDelegate = bleCentralManagerDelegate;

            _bleCentralManagerDelegate.DiscoveredPeripheral += (sender, e) =>
            {
                Trace.Message("DiscoveredPeripheral: {0}, Id: {1}", e.Peripheral.Name, e.Peripheral.Identifier);
                var name = e.Peripheral.Name;
                if (e.AdvertisementData.ContainsKey(CBAdvertisement.DataLocalNameKey))
                {
                    // iOS caches the peripheral name, so it can become stale (if changing)
                    // keep track of the local name key manually
                    name = ((NSString)e.AdvertisementData.ValueForKey(CBAdvertisement.DataLocalNameKey)).ToString();
                }

                var device = new Device(this, e.Peripheral, _bleCentralManagerDelegate, name, e.RSSI.Int32Value,
                                        ParseAdvertismentData(e.AdvertisementData));
                HandleDiscoveredDevice(device);
            };

            _bleCentralManagerDelegate.UpdatedState += (sender, e) =>
            {
                Trace.Message("UpdatedState: {0}", _centralManager.State);
                _stateChanged.Set();

                //handle PoweredOff state
                //notify subscribers about disconnection
                if (_centralManager.State == CBCentralManagerState.PoweredOff)
                {
                    foreach (var device in ConnectedDeviceRegistry.Values.ToList())
                    {
                        ((Device)device).ClearServices();
                        HandleDisconnectedDevice(false, device);
                    }

                    ConnectedDeviceRegistry.Clear();
                }
            };

            _bleCentralManagerDelegate.ConnectedPeripheral += (sender, e) =>
            {
                Trace.Message("ConnectedPeripherial: {0}", e.Peripheral.Name);

                // when a peripheral gets connected, add that peripheral to our running list of connected peripherals
                var guid = ParseDeviceGuid(e.Peripheral).ToString();

                IDevice device;
                if (_deviceOperationRegistry.TryGetValue(guid, out device))
                {
                    _deviceOperationRegistry.Remove(guid);
                    ((Device)device).Update(e.Peripheral);
                }
                else
                {
                    Trace.Message("Device not found in operation registry. Creating a new one.");
                    device = new Device(this, e.Peripheral, _bleCentralManagerDelegate);
                }

                ConnectedDeviceRegistry[guid] = device;
                HandleConnectedDevice(device);
            };

            _bleCentralManagerDelegate.DisconnectedPeripheral += (sender, e) =>
            {
                if (e.Error != null)
                {
                    Trace.Message("Disconnect error {0} {1} {2}", e.Error.Code, e.Error.Description, e.Error.Domain);
                }

                // when a peripheral disconnects, remove it from our running list.
                var     id       = ParseDeviceGuid(e.Peripheral);
                var     stringId = id.ToString();
                IDevice foundDevice;

                // normal disconnect (requested by user)
                var isNormalDisconnect = _deviceOperationRegistry.TryGetValue(stringId, out foundDevice);
                if (isNormalDisconnect)
                {
                    _deviceOperationRegistry.Remove(stringId);
                }

                // check if it is a peripheral disconnection, which would be treated as normal
                if (e.Error != null && e.Error.Code == 7 && e.Error.Domain == "CBErrorDomain")
                {
                    isNormalDisconnect = true;
                }

                // remove from connected devices
                if (!ConnectedDeviceRegistry.TryRemove(stringId, out foundDevice))
                {
                    Trace.Message($"Device with id '{stringId}' was not found in the connected device registry. Nothing to remove.");
                }

                foundDevice = foundDevice ?? new Device(this, e.Peripheral, _bleCentralManagerDelegate);

                //make sure all cached services are cleared this will also clear characteristics and descriptors implicitly
                ((Device)foundDevice).ClearServices();

                HandleDisconnectedDevice(isNormalDisconnect, foundDevice);
            };

            _bleCentralManagerDelegate.FailedToConnectPeripheral +=
                (sender, e) =>
            {
                var     id       = ParseDeviceGuid(e.Peripheral);
                var     stringId = id.ToString();
                IDevice foundDevice;

                // remove instance from registry
                if (_deviceOperationRegistry.TryGetValue(stringId, out foundDevice))
                {
                    _deviceOperationRegistry.Remove(stringId);
                }

                foundDevice = foundDevice ?? new Device(this, e.Peripheral, _bleCentralManagerDelegate);

                HandleConnectionFail(foundDevice, e.Error.Description);
            };
        }