Exemplo n.º 1
0
        public Device(BluetoothDevice nativeDevice, BluetoothGatt gatt,
                      GattCallback gattCallback, int rssi)
        {
            try
            {
                _nativeDevice = nativeDevice;
                _gatt         = gatt;
                _gattCallback = gattCallback;
                _rssi         = rssi;

                // when the services are discovered on the gatt callback, cache them here
                if (_gattCallback != null)
                {
                    _gattCallback.ServicesDiscovered += (s, e) =>
                    {
                        if (_gattCallback.Services != null)
                        {
                            var services = _gattCallback.Services;

                            _services = new List <IService>();
                            foreach (var item in services)
                            {
                                _services.Add(new Service(item, _gatt, _gattCallback));
                            }
                            if (ServicesDiscovered != null)
                            {
                                ServicesDiscovered(this, e);
                            }
                        }
                    };
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error :{0}", ex.Message);
            }
        }
Exemplo n.º 2
0
        public Characteristic(BluetoothGattCharacteristic nativeCharacteristic, BluetoothGatt gatt,
                              GattCallback gattCallback)
        {
            _nativeCharacteristic = nativeCharacteristic;
            _gatt         = gatt;
            _gattCallback = gattCallback;

            if (_gattCallback != null)
            {
                // wire up the characteristic value updating on the gattcallback
                _gattCallback.CharacteristicValueUpdated += (sender, e) =>
                {
                    // it may be other characteristics, so we need to test
                    if (e.Characteristic.ID == ID)
                    {
                        // update our underlying characteristic (this one will have a value)
                        //TODO: is this necessary? probably the underlying reference is the same.
                        //this._nativeCharacteristic = e.Characteristic;

                        ValueUpdated(this, e);
                    }
                };
            }
        }
Exemplo n.º 3
0
 public Service(BluetoothGattService nativeService, BluetoothGatt gatt, GattCallback _gattCallback)
 {
     _nativeService     = nativeService;
     _gatt              = gatt;
     this._gattCallback = _gattCallback;
 }
Exemplo n.º 4
0
        public Adapter()
        {
            var appContext = Application.Context;

            // get a reference to the bluetooth system service
            _manager = (BluetoothManager)appContext.GetSystemService("bluetooth");


            _adapter = _manager.Adapter;

            IsBluetoothEnabled = _adapter.State == State.On;


            _gattCallback = new GattCallback(this);


            _gattCallback.DeviceConnected += (sender, e) =>
            {
                if (!IsScanning)
                {
                    DisconnectDevice(e.Device);
                    return;
                }
                cancelScan = true;

//				if (e.Device.State != DeviceState.Connected)
                if (!ContaisDevice(_connectedDevices, e.Device))
                {
                    _connectedDevices.Add(e.Device);
                    DeviceConnected(this, e);
                }
            };

            _gattCallback.DeviceDisconnected += (sender, e) =>
            {
                // TODO: remove the disconnected device from the _connectedDevices list
                // i don't think this will actually work, because i'm created a new underlying device here.
                //if(this._connectedDevices.Contains(
                var device = e.Device;
                foreach (var item in _connectedDevices)
                {
                    if (item.DeviceGUID == e.Device.DeviceGUID)
                    {
                        device = item;
                    }
                }

//				if (e.Device.State != DeviceState.Connected) {
                if (e.Device.DeviceGUID == BLEManager.SharedInstance.SelectedDevice.DeviceGUID)
                {
                    _connectedDevices.Remove(device);
                    DeviceDisconnected(this, e);
                    StopScanningForDevices();
                    DiscoveredDevices.Clear();
                }
            };

            MessagingCenter.Subscribe <AndroidBluetoothStatusMessage>(this, DroidConstant.DEVICE_BLUETOOTH_ONOFF_MESSAGE,
                                                                      sender =>
            {
                var msg            = sender;
                IsBluetoothEnabled = msg.IsEnabled;
                BluetoothStateChanged(this, null);
            });
        }