Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BluetoothLE.Droid.Adapter"/> class.
        /// </summary>
        public Adapter()
        {
            var appContext = Android.App.Application.Context;

            _manager = (BluetoothManager)appContext.GetSystemService(Context.BluetoothService);
            _adapter = _manager.Adapter;

            _callback = new GattCallback();
            _callback.DeviceConnected    += BluetoothGatt_DeviceConnected;
            _callback.DeviceDisconnected += BluetoothGatt_DeviceDisconnected;

            _advertiseCallback = new AdvertiseCallback();
            _advertiseCallback.AdvertiseStartFailed  += BluetoothGatt_AdvertiseStartFailed;
            _advertiseCallback.AdvertiseStartSuccess += Bluetooth_AdvertiseStartSuccess;

            var callback = new GattServerCallback();

            _gattServer        = _manager.OpenGattServer(appContext, callback);
            callback.Server    = _gattServer;
            _broadcastListener = new BroadcastListener();

            CharacteristicsFactory = new CharacteristicsFactory();
            ServiceFactory         = new ServiceFactory();

            PeripheralStateChanged = _broadcastListener.StateUpdatedSubject.Select(state =>
            {
                if (state == ManagerState.PoweredOn)
                {
                    if (_adapter.BluetoothLeAdvertiser == null)
                    {
                        return(ManagerState.Unsupported);
                    }
                    if (!_adapter.IsMultipleAdvertisementSupported)
                    {
                        return(ManagerState.PartialSupport);
                    }
                }
                return(state);
            }).AsObservable();

            CentralStateChanged = _broadcastListener.StateUpdatedSubject.Select(state =>
            {
                if (state == ManagerState.PoweredOn)
                {
                    if (_adapter.BluetoothLeScanner == null)
                    {
                        return(ManagerState.Unsupported);
                    }
                    if (!(_adapter.IsOffloadedFilteringSupported && _adapter.IsOffloadedScanBatchingSupported))
                    {
                        return(ManagerState.PartialSupport);
                    }
                }

                return(state);
            }).AsObservable();

            _peripheralStateDisposable = PeripheralStateChanged.Subscribe(state => _peripheralState = state);
            _centralStateDisposable    = CentralStateChanged.Subscribe(state => _centralState = state);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BluetoothLE.iOS.Adapter"/> class.
        /// </summary>
        public Adapter()
        {
            _centralManager = new CBCentralManager();

            _centralManager.DiscoveredPeripheral      += DiscoveredPeripheral;
            _centralManager.ConnectedPeripheral       += ConnectedPeripheral;
            _centralManager.DisconnectedPeripheral    += DisconnectedPeripheral;
            _centralManager.FailedToConnectPeripheral += FailedToConnectPeripheral;

            _current = this;

            CentralStateChanged     = Observable.FromEventPattern(eh => _centralManager.UpdatedState += eh, eh => _centralManager.UpdatedState -= eh).Select(x => (ManagerState)_centralManager.State);
            _scanCancellationToken  = new CancellationTokenSource();
            _peripheralStateSubject = new Subject <ManagerState>();

            PeripheralStateChanged = _peripheralStateSubject.AsObservable();
            _peripheralManager     = new CBPeripheralManager(this, null);

            CharacteristicsFactory = new CharacteristicsFactory();
            ServiceFactory         = new ServiceFactory();

            _peripheralStateDisposable = _peripheralStateSubject.Subscribe(state => _peripheralState = state);
            _centralStateDisposable    = CentralStateChanged.Subscribe(state => _centralState = state);
        }