Ejemplo n.º 1
0
        public Task <bool> Connect(OWBaseBoard board, CancellationToken cancellationToken)
        {
            _requestingDisconnect = false;
            _reconnecting         = false;

            _connectionCompletionSource = new TaskCompletionSource <bool>();

            if (board.NativePeripheral is CBPeripheral peripheral)
            {
                _board      = board;
                _peripheral = peripheral;
                _peripheral.WeakDelegate = this;

                var options = new PeripheralConnectionOptions()
                {
                    NotifyOnDisconnection = true,
#if __IOS__
                    NotifyOnConnection   = true,
                    NotifyOnNotification = true,
#endif
                };

                _centralManager.ConnectPeripheral(peripheral, options);
            }
            else
            {
                _connectionCompletionSource.SetResult(false);
            }

            return(_connectionCompletionSource.Task);
        }
Ejemplo n.º 2
0
            public override void OnScanResult(ScanCallbackType callbackType, ScanResult result)
            {
                Debug.WriteLine("OnScanResult");

                var board = new OWBaseBoard()
                {
                    ID               = result.Device.Address,
                    Name             = result.Device.Name ?? "Onewheel",
                    IsAvailable      = true,
                    NativePeripheral = result.Device,
                };

                _owble.BoardDiscovered?.Invoke(board);
            }
Ejemplo n.º 3
0
        public Task <bool> Connect(OWBaseBoard board, CancellationToken cancellationToken)
        {
            _board = board;

            _connectTaskCompletionSource = new TaskCompletionSource <bool>();

            if (board.NativePeripheral is BluetoothDevice device)
            {
                _gattCallback  = new OWBLE_BluetoothGattCallback(this);
                _bluetoothGatt = device.ConnectGatt(Xamarin.Essentials.Platform.CurrentActivity, false, _gattCallback);
            }

            return(_connectTaskCompletionSource.Task);
        }
Ejemplo n.º 4
0
            public void OnLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
            {
                Debug.WriteLine("OnLeScan");

                var board = new OWBaseBoard()
                {
                    ID               = device.JniIdentityHashCode.ToString(),
                    Name             = device.Name ?? "Onewheel",
                    IsAvailable      = true,
                    NativePeripheral = device,
                };

                _owble.BoardDiscovered?.Invoke(board);
            }
Ejemplo n.º 5
0
        public void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
        {
            Debug.WriteLine("CentralManager_DiscoveredPeripheral: " + peripheral.Name);

            var board = new OWBaseBoard()
            {
                ID               = peripheral.Identifier.ToString(),
                Name             = peripheral.Name ?? "Onewheel",
                IsAvailable      = true,
                NativePeripheral = peripheral,
            };

            BoardDiscovered?.Invoke(board);
        }
        void OWBLE_BoardDiscovered(OWBaseBoard board)
        {
            Debug.WriteLine($"OWBLE_BoardDiscovered: {board.Name} {board.ID}");
            var boardIndex = Boards.IndexOf(board);

            if (boardIndex == -1)
            {
                Boards.Add(board);
            }
            else
            {
                // Its odd that we set the name again, but when a board is just powered on its name is "Onewheel", not "ow123456"
                Boards[boardIndex].Name             = board.Name;
                Boards[boardIndex].IsAvailable      = true;
                Boards[boardIndex].NativePeripheral = board.NativePeripheral;
            }
        }
Ejemplo n.º 7
0
        public Task Disconnect()
        {
            if (_connectTaskCompletionSource != null && _connectTaskCompletionSource.Task.IsCanceled == false)
            {
                // TODO: this causes app to crash. While not ideal, this works for the current flow.
                _connectTaskCompletionSource.SetCanceled();
            }

            // TODO: Handle is connecting.
            if (_bluetoothGatt != null)
            {
                _bluetoothGatt.Disconnect();
            }

            _board = null;

            return(Task.CompletedTask);
        }