Пример #1
0
 private void BluetoothLEService_ConnectionStateChanged(object sender, ConnectionStateChangedEventArgs e)
 {
     if (e.NewStatus == ProfileState.Disconnected && _bluetoothLEService != null)
     {
         _bluetoothLEService.CharacteristicNotificationReceived -= BluetoothLEService_CharacteristicNotificationReceived;
         _bluetoothLEService = null;
         SetControlsEnabled(false);
         RunOnUiThread(() =>
         {
             _retryConnectButton.Visibility = ViewStates.Visible;
             _retryConnectButton.Enabled    = true;
         });
         ShowAlert("Bluetooth Disconnected", "Device has disconnected or lost connection. Reconnect and try again.");
     }
 }
Пример #2
0
        private async void BluetoothLEScanner_DeviceDiscovered(object sender, DeviceDiscoveredEventArgs e)
        {
            _bluetoothLEService = new BluetoothLEService(this, e.Device);
            BluetoothLEScanner.Current.StopScan();

            _progressBar.Visibility = ViewStates.Visible;
            var result = await _bluetoothLEService.ConnectGattAsync();

            if (result.NewStatus != ProfileState.Connected)
            {
                _retryConnectButton.Enabled = true;
                _progressBar.Visibility     = ViewStates.Gone;
                ShowAlert("Bluetooth Connection Error", "Could not connect to device.");
                return;
            }

            _bluetoothLEService.ConnectionStateChanged -= BluetoothLEService_ConnectionStateChanged;
            _bluetoothLEService.ConnectionStateChanged += BluetoothLEService_ConnectionStateChanged;

            var readResult = await _bluetoothLEService.ReadCharacteristicAsync(BluetoothConstants.ServiceUuid,
                                                                               BluetoothConstants.StatusCharacteristicUuid);

            _progressBar.Visibility = ViewStates.Gone;

            if (readResult.GattStatus != GattStatus.Success)
            {
                ShowAlert("Get Status Failed", $"Failed to get current status.");
            }

            _bluetoothLEService.CharacteristicNotificationReceived -= BluetoothLEService_CharacteristicNotificationReceived;
            _bluetoothLEService.CharacteristicNotificationReceived += BluetoothLEService_CharacteristicNotificationReceived;
            var subscribeResult = _bluetoothLEService.SubscribeCharacteristicNotification(BluetoothConstants.ServiceUuid,
                                                                                          BluetoothConstants.StatusCharacteristicUuid);

            if (subscribeResult.GattStatus != GattStatus.Success)
            {
                ShowAlert("Status Subscription Failed", $"Failed to subscribe for status changes.");
            }

            UpdateStatus(readResult.Response);

            SetControlsEnabled(true);
            _retryConnectButton.Visibility = ViewStates.Gone;
        }
Пример #3
0
        private void Init()
        {
            if (_bluetoothLEService != null)
            {
                _bluetoothLEService.CharacteristicNotificationReceived -= BluetoothLEService_CharacteristicNotificationReceived;
            }

            _bluetoothLEService = null;
            SetControlsEnabled(false);

            _retryConnectButton.Visibility = ViewStates.Visible;

            var permissions = new[]
            {
                Manifest.Permission.AccessCoarseLocation,
                Manifest.Permission.AccessFineLocation,
                Manifest.Permission.Bluetooth,
                Manifest.Permission.BluetoothAdmin
            };

            RequestPermissions(permissions, PermissionsRequestCode);
        }