Exemplo n.º 1
0
        public static BgBleDevice MapBleDevice(ScanResponseEventArgs e)
        {
            var bluetoothAddress = new byte[8];
            var id = e.sender.Take(8).Reverse().ToArray();

            Buffer.BlockCopy(id, 0, bluetoothAddress, 8 - id.Length, id.Length);
            var data   = e.data;
            var device = new BgBleDevice
            {
                BluetoothAddress = BitConverter.ToUInt64(bluetoothAddress),
                Identifier       = e.sender,
                Address          = ByteArrayToHexString(e.sender)
            };

            device.Advertisement.Data        = data;
            device.Advertisement.AddressType = e.address_type;
            byte[] serviceIdentifier       = { };
            var    bytesRemaining          = 0;
            var    serviceIdentifieroffset = 0;

            for (var i = 0; i < data.Length; i++)
            {
                if (bytesRemaining == 0)
                {
                    bytesRemaining          = data[i];
                    serviceIdentifier       = new byte[data[i]];
                    serviceIdentifieroffset = i + 1;
                }
                else
                {
                    serviceIdentifier[i - serviceIdentifieroffset] = data[i];
                    bytesRemaining--;
                    if (bytesRemaining != 0)
                    {
                        continue;
                    }
                    if (serviceIdentifier[0] != 0x06 && serviceIdentifier[0] != 0x07)
                    {
                        continue;
                    }
                    var serviceId =
                        new Guid(
                            ByteArrayToHexString(serviceIdentifier.Skip(1).Take(16).Reverse().ToArray()));
                    if (!device.Advertisement.AvailableServices.Contains(serviceId))
                    {
                        device.Advertisement.AvailableServices.Add(serviceId);
                    }
                }
            }
            return(device);
        }
 private void ProcessBgBle_Callback_DeviceFound(BgBleDevice device)
 {
     if (!device.Advertisement.AvailableServices.Contains(LegoHubServiceId))
     {
         return;
     }
     if (Devices.ContainsKey(device.BluetoothAddress))
     {
         return;
     }
     _logger.LogInformation($"BgBle Service: Found Device containing Lego Hub service id: {device.Address}");
     Devices.Add(device.BluetoothAddress, device);
     _currentActionTokenSource.Cancel();
 }
 public void Connect(CancellationToken connectToken, CancellationToken appToken,
                     int connectTimeoutInMilliseconds, BgBleDevice deviceToConnect)
 {
     try
     {
         var lockTaken = false;
         Monitor.TryEnter(ThreadLock, 150, ref lockTaken);
         if (lockTaken)
         {
             if (_busy)
             {
                 return;
             }
             _busy = true;
             Monitor.Exit(ThreadLock);
         }
         _device        = deviceToConnect;
         _currentAction = new CancellationTokenSource();
         var connectToDevicesTokenSource =
             CancellationTokenSource.CreateLinkedTokenSource(connectToken,
                                                             _currentAction.Token);
         _bglib.SendCommand(_serialPort,
                            _bglib.BLECommandGAPConnectDirect(deviceToConnect.Identifier,
                                                              deviceToConnect.Advertisement.AddressType, 0x20, 0x30, 0x100, 0));
         WaitHandle.WaitAny(new[] { connectToDevicesTokenSource.Token.WaitHandle }, connectTimeoutInMilliseconds);
         // cleanup takes 125ms
         _bglib.SendCommand(_serialPort, _bglib.BLECommandGAPEndProcedure());
         WaitHandle.WaitAny(new[] { appToken.WaitHandle }, 130);
         _logger.LogInformation(
             $"ControlPlus Repository: Connected to device {deviceToConnect.Address} with {_device.Services.Count} services");
     }
     catch (Exception exception)
     {
         _logger.LogWarning(exception,
                            $"ControlPlus Repository: Unable to connect to device {deviceToConnect.Address}");
     }
     _deviceService = null;
     _device        = null;
     _busy          = false;
 }
 public void FetchCharacteristics(CancellationToken appToken, int connectTimeoutInMilliseconds,
                                  BgBleDeviceService deviceService)
 {
     deviceService.Characteristics.Clear();
     try
     {
         var lockTaken = false;
         Monitor.TryEnter(ThreadLock, 150, ref lockTaken);
         if (lockTaken)
         {
             if (_busy)
             {
                 return;
             }
             _busy = true;
             Monitor.Exit(ThreadLock);
         }
         _deviceService = deviceService;
         _currentAction = new CancellationTokenSource();
         var populateClientInformationTokenSource =
             CancellationTokenSource.CreateLinkedTokenSource(appToken,
                                                             _currentAction.Token);
         _bglib.SendCommand(_serialPort,
                            _bglib.BLECommandATTClientFindInformation(deviceService.ConnectionHandle, deviceService.Start,
                                                                      deviceService.End));
         WaitHandle.WaitAny(new[] { populateClientInformationTokenSource.Token.WaitHandle },
                            connectTimeoutInMilliseconds);
     }
     catch (Exception exception)
     {
         _logger.LogWarning(exception,
                            $"ControlPlus Repository: Unable to populate Client Information {deviceService.ServiceId}");
     }
     _lastCharacteristic = null;
     _deviceService      = null;
     _device             = null;
     _busy = false;
 }
Exemplo n.º 5
0
 protected virtual void Dispose(bool disposing)
 {
     _bgBleService = null;
     _bgBleDevice  = null;
 }
Exemplo n.º 6
0
 public BluegigaPoweredUpBluetoothDevice(BgBleService bgBleService, BgBleDevice bgBleDevice)
 {
     _bgBleService = bgBleService;
     _bgBleDevice  = bgBleDevice;
 }