Exemple #1
0
        public override async Task <IDevice> ConnectToKnownDeviceAsync(Guid deviceGuid,
                                                                       ConnectParameters connectParameters, CancellationToken cancellationToken)
        {
            //convert GUID to string and take last 12 characters as MAC address
            String            guidString    = deviceGuid.ToString("N").Substring(20);
            UInt64            bluetoothAddr = Convert.ToUInt64(guidString, 16);
            BluetoothLEDevice nativeDevice  = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddr);

            Device currDevice = new Device(this, nativeDevice, 0, guidString);

            await this.ConnectToDeviceAsync(currDevice);

            return(currDevice);
        }
Exemple #2
0
        public void Connect(ConnectParameters connectParameters, CancellationToken cancellationToken)
        {
            this.IsOperationRequested = true;

            if (connectParameters.ForceBleTransport)
            {
                this.ConnectToGattForceBleTransportAPI(connectParameters.AutoConnect, cancellationToken);
            }
            else
            {
                BluetoothGatt connectGatt = this.BluetoothDevice.ConnectGatt(Application.Context,
                                                                             connectParameters.AutoConnect, this._gattCallback);
                this._connectCancellationTokenRegistration.Dispose();
                this._connectCancellationTokenRegistration = cancellationToken.Register(() => connectGatt.Disconnect());
            }
        }
Exemple #3
0
        /// <summary>
        /// Connects to known device async.
        /// https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html
        /// </summary>
        /// <returns>The to known device async.</returns>
        /// <param name="deviceGuid">Device GUID.</param>
        public override async Task <IDevice> ConnectToKnownDeviceAsync(Guid deviceGuid,
                                                                       ConnectParameters connectParameters = default(ConnectParameters),
                                                                       CancellationToken cancellationToken = default(CancellationToken))
        {
            // Wait for the PoweredOn state
            await this.WaitForState(CBCentralManagerState.PoweredOn, cancellationToken, true);

            if (cancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException("ConnectToKnownDeviceAsync cancelled");
            }

            //FYI attempted to use tobyte array insetead of string but there was a problem with byte ordering Guid->NSUui
            NSUuid uuid = new NSUuid(deviceGuid.ToString());

            Trace.Message($"[Adapter] Attempting connection to {uuid}");

            CBPeripheral[] peripherials = this._centralManager.RetrievePeripheralsWithIdentifiers(uuid);
            CBPeripheral   peripherial  = peripherials.SingleOrDefault();

            if (peripherial == null)
            {
                CBPeripheral[] systemPeripherials = this._centralManager.RetrieveConnectedPeripherals(new CBUUID[0]);

                CBUUID cbuuid = CBUUID.FromNSUuid(uuid);
                peripherial = systemPeripherials.SingleOrDefault(p => p.UUID.Equals(cbuuid));

                if (peripherial == null)
                {
                    throw new Exception($"[Adapter] Device {deviceGuid} not found.");
                }
            }


            Device device = new Device(this, peripherial, this._centralManager, peripherial.Name,
                                       peripherial.RSSI?.Int32Value ?? 0, new List <AdvertisementRecord>());

            await this.ConnectToDeviceAsync(device, connectParameters, cancellationToken);

            return(device);
        }
Exemple #4
0
        protected override Task ConnectToDeviceNativeAsync(IDevice device, ConnectParameters connectParameters,
                                                           CancellationToken cancellationToken)
        {
            if (connectParameters.AutoConnect)
            {
                Trace.Message("Warning: Autoconnect is not supported in iOS");
            }

            this._deviceOperationRegistry[device.Id.ToString()] = device;

            this._centralManager.ConnectPeripheral(device.NativeDevice as CBPeripheral,
                                                   new PeripheralConnectionOptions());

            // this is dirty: We should not assume, AdapterBase is doing the cleanup for us...
            // move ConnectToDeviceAsync() code to native implementations.
            cancellationToken.Register(() =>
            {
                Trace.Message("Canceling the connect attempt");
                this._centralManager.CancelPeripheralConnection(device.NativeDevice as CBPeripheral);
            });

            return(Task.FromResult(true));
        }
Exemple #5
0
        protected override async Task ConnectToDeviceNativeAsync(IDevice device, ConnectParameters connectParameters,
                                                                 CancellationToken cancellationToken)
        {
            Trace.Message($"Connecting to device with ID:  {device.Id.ToString()}");

            ObservableBluetoothLEDevice nativeDevice = device.NativeDevice as ObservableBluetoothLEDevice;

            if (nativeDevice == null)
            {
                return;
            }

            Device uwpDevice = (Device)device;

            uwpDevice.ConnectionStatusChanged += this.Device_ConnectionStatusChanged;

            await nativeDevice.ConnectAsync();

            if (!this.ConnectedDeviceRegistry.ContainsKey(uwpDevice.Id.ToString()))
            {
                this.ConnectedDeviceRegistry.Add(uwpDevice.Id.ToString(), device);
            }
        }
Exemple #6
0
 protected override Task ConnectToDeviceNativeAsync(IDevice device, ConnectParameters connectParameters,
                                                    CancellationToken cancellationToken)
 {
     ((Device)device).Connect(connectParameters, cancellationToken);
     return(Task.CompletedTask);
 }