public async Task ConnectToDevice(Device device)
        {
            var gatt = ((BluetoothDevice)device.NativeDevice).ConnectGatt(Android.App.Application.Context, true, new GattCallback(this));

            _gattServices[device] = gatt;
            await Task.Run(() => _deviceConnectedEvent.WaitOne());

            _deviceConnectedEvent.Reset();
        }
예제 #2
0
        public async Task ConnectToDevice(Device device)
        {
            if (device.Connected)
            {
                return;
            }
            var nativeDevice = (CBPeripheral)device.NativeDevice;

            _centralManager.ConnectPeripheral(nativeDevice);
            await WaitForConnection();
            await MapDevice(device, nativeDevice);
        }
예제 #3
0
        private async Task MapDevice(Device device, CBPeripheral nativeDevice)
        {
            nativeDevice.DiscoveredService += nativeDevice_DiscoveredService;

            nativeDevice.DiscoverServices();
            await Task.Run(() => _servicesDiscovered.WaitOne(TimeSpan.FromSeconds(10)));

            nativeDevice.DiscoveredService -= nativeDevice_DiscoveredService;
            _servicesDiscovered.Reset();

            foreach (var cbService in nativeDevice.Services)
            {
                nativeDevice.DiscoveredCharacteristic += nativeDevice_DiscoveredCharacteristic;

                nativeDevice.DiscoverCharacteristics(cbService);
                await Task.Run(() => _characteristicsDiscovered.WaitOne(TimeSpan.FromSeconds(10)));

                nativeDevice.DiscoveredCharacteristic -= nativeDevice_DiscoveredCharacteristic;
                _characteristicsDiscovered.Reset();

                var service = new Service()
                {
                    Id              = BluetoothConverter.ConvertBluetoothLeUuid(cbService.UUID.Uuid),
                    Device          = device,
                    Characteristics = new List <Characteristic>()
                };

                foreach (var cbCharacteristic in cbService.Characteristics)
                {
                    var characteristic = await ConvertCharacteristic(cbCharacteristic);

                    characteristic.Service = service;
                    service.Characteristics.Add(characteristic);
                }
                service.Device = device;
                device.Services.Add(service);
            }
        }