Exemple #1
0
#pragma warning restore 4014

        private async Task <BtError> _enumerateDevices()
        {
            if (!isScanning && !_isConnected)
            {
                devices.Clear();
                deviceAddresses.Clear();
                serviceObjects.Clear();
                characteristicObjects.Clear();
                descriptorObjects.Clear();
                subscribeCharacteristics.Clear();
                BtError error = await _checkBtSupport();

                if (error != BtError.None)
                {
                    return(error);
                }
                watcher = new BluetoothLEAdvertisementWatcher();
                watcher.ScanningMode = BluetoothLEScanningMode.Active;
                watcher.Received    += DeviceDiscovered;
                watcher.Start();
                isScanning = true;
                return(BtError.None);
            }
            else
            {
                return(BtError.AlreadyRunning);
            }
        }
Exemple #2
0
        /// <summary>
        /// Handle the connect button. If connected disconnect. Otherwise connect.
        /// </summary>
        /// <param name="src">Source</param>
        /// <param name="e">EventArgs</param>
        void OnConnectClicked(object src, EventArgs e)
        {
            Debug.WriteLine("IsConnected: " + bluetooth.isConnected());
            if (!bluetooth.isConnected())
            {
                bluetooth.endEnumeration();
                bluetooth.disconnect();
                BtError error = bluetooth.checkBtSupport();
                switch (error)
                {
                case BtError.Disabled:
                    bluetooth.showEnableBtPrompt(AppResources.EnableBTTitle, AppResources.EnableBTMessage, AppResources.EnableBTConfirm, AppResources.EnableBTCancel);
                    requestTime = Environment.TickCount;
                    break;

                case BtError.None:
                    // Do not allow this to be shown more than once at a time
                    if (bluetoothDevicePage == null)
                    {
                        discoveredDevices.Clear();
                        deviceNames.Clear();
                        bluetoothDevicePage = new BluetoothDevicePage();
                        Navigation.PushModalAsync(bluetoothDevicePage);
                    }
                    break;

                default:
                    UserDialogs.Instance.Alert(AppResources.AlertBtErrorMessage, AppResources.AlertBtErrorTitle, AppResources.AlertOk);
                    break;
                }
            }
            else
            {
                UserDialogs.Instance.Confirm(new ConfirmConfig()
                {
                    Title      = AppResources.ConfirmDisconnectTitle,
                    Message    = AppResources.ConfirmDisconnectMessage.Replace("%DEV%", connectedDeviceName),
                    OkText     = AppResources.Yes,
                    CancelText = AppResources.No,
                    OnAction   = (result) => {
                        if (result)
                        {
                            bluetooth.endEnumeration();
                            manualDisconnect = true;
                            bluetooth.disconnect();
                        }
                    }
                });
            }
        }
Exemple #3
0
#pragma warning disable 4014
        public UWPBluetooth(BTCallback btCallback)
        {
            this.callback = btCallback;
            Task.Run(async() => {
                BtError error = await _checkBtSupport();
                if (error != BtError.NoBluetooth && error != BtError.NoBLE)
                {
                    var lastState = error == BtError.None;
                    // Watch for bt power changes
                    while (true)
                    {
                        BtError e = await _checkBtSupport();
                        var state = e == BtError.None;
                        if (state != lastState)
                        {
                            lastState = state;
                            // Should not wait for this. That would slow down checking for power changes
                            callback.onBluetoothPowerChanged(state);
                        }
                        await Task.Delay(100);
                    }
                }
            });
        }