Exemplo n.º 1
0
        private void BGApi_OnGAPScanResponse(object sender, ScanResponseEventArgs e)
        {
            if (State == CentralManagerState.Disposed)
            {
                return;
            }

            try {
                if (State != CentralManagerState.Scanning)
                {
                    // Should not enter this unless device is in a weird state,
                    // most likely it was scanning and not told to stop. Attempt
                    // stop scanning and reset state.
                    Debug.WriteLine("Received a BGApi_OnGAPScanResponse at an unexpected time, attempting to reset...");
                    if (_softResetTask != null)
                    {
                        return;
                    }
                    _softResetTask = SoftResetDongleState();
                    _softResetTask.ContinueWith(task => {
                        _softResetTask = null;
                        if (task.IsFaulted)
                        {
                            Debug.Fail($"Received a BGApi_OnGAPScanResponse at an unexpected time and attempts to recover failed: {task.Exception.Message}");
                        }
                    });
                    return;
                }

                var addressType     = (BGGAPAdvertiserAddressType)Enum.ToObject(typeof(BGGAPAdvertiserAddressType), e.address_type);
                var isPublicAddress = addressType == BGGAPAdvertiserAddressType.PublicAddress;
                var address         = e.sender.ToHexString();

                var packetType    = (BGGAPPacketType)Enum.ToObject(typeof(BGGAPPacketType), e.packet_type);
                var adData        = (packetType != BGGAPPacketType.ScanResponse ? e.data : null) ?? new byte[0];
                var respData      = (packetType == BGGAPPacketType.ScanResponse ? e.data : null) ?? new byte[0];
                var isConnectable = packetType == BGGAPPacketType.ConnectableAd;

                var peripheral = new BGPeripheral(address, isPublicAddress, _bgApi, e.rssi);

                var peripheralDiscoveredArgs = new PeripheralDiscoveredEventArgs {
                    Peripheral        = peripheral,
                    AdvertisementData = adData,
                    ScanResponseData  = respData,
                    IsConnectable     = isConnectable,
                    RSSI = e.rssi
                };

                PeripheralWasDiscovered(peripheralDiscoveredArgs);
            }
            catch (Exception ex) {
                if (_scanningExceptionToRethow == null)
                {
                    _scanningExceptionToRethow = ExceptionDispatchInfo.Capture(ex);
                }
            }
        }
Exemplo n.º 2
0
        private void OnScanResponse(object sender, ScanResponseEventArgs e)
        {
            var type      = (DiscoveryType)e.PacketType;
            var mac       = new MAC(e.Sender);
            var macType   = (MacType)e.AddressType;
            var eventArgs = new DiscoveryEventArgs(type, mac, macType, e.Data, e.RSSI);

            Discovered?.Invoke(this, eventArgs);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        private void FindService(object sender, ScanResponseEventArgs e)
        {
            // pull all advertised service info from ad packet
            // taken from bglib example code
            List <Byte[]> ad_services = new List <Byte[]> ();

            Byte[] this_field   = { };
            int    bytes_left   = 0;
            int    field_offset = 0;

            for (int i = 0; i < e.data.Length; i++)
            {
                if (bytes_left == 0)
                {
                    bytes_left   = e.data[i];
                    this_field   = new Byte[e.data[i]];
                    field_offset = i + 1;
                }
                else
                {
                    this_field[i - field_offset] = e.data[i];
                    bytes_left--;
                    if (bytes_left == 0)
                    {
                        if (this_field[0] == 0x02 || this_field[0] == 0x03)
                        {
                            // partial or complete list of 16-bit UUIDs
                            ad_services.Add(this_field.Skip(1).Take(2).Reverse().ToArray());
                        }
                        else if (this_field[0] == 0x04 || this_field[0] == 0x05)
                        {
                            // partial or complete list of 32-bit UUIDs
                            ad_services.Add(this_field.Skip(1).Take(4).Reverse().ToArray());
                        }
                        else if (this_field[0] == 0x06 || this_field[0] == 0x07)
                        {
                            // partial or complete list of 128-bit UUIDs
                            ad_services.Add(this_field.Skip(1).Take(16).Reverse().ToArray());
                        }
                    }
                }
            }

            if (ad_services.Any(a => a.SequenceEqual(((byte[])ServiceUUID).Reverse())))
            {
                _scanResponse = e;
            }
        }
Exemplo n.º 5
0
        public BlePeripheralMap Connect()
        {
            // Init BgLib, open port, and start receiving thread
            this.InitTools();

            BleDiscoverService    discoverBlock = new BleDiscoverService(Ble, Port, Config.ServiceUUID);
            ScanResponseEventArgs found         = discoverBlock.Execute();

            BleConnectToService connectBlock = new BleConnectToService(Ble, Port, found.sender, found.address_type);

            this.ConnectionHandle = connectBlock.Execute().connection;

            BleFindServicesAndCharacteristics infoBlock = new BleFindServicesAndCharacteristics(Ble, Port, this.ConnectionHandle);

            this.PeripheralMap = infoBlock.Execute();

            this.InitializeCharacteristicNotifications();

            this.Ble.Lib.BLEEventConnectionDisconnected += DisconnectedHandler;

            return(this.PeripheralMap);
        }
Exemplo n.º 6
0
 private void Bglib_BLEEventGAPScanResponse(object sender, ScanResponseEventArgs e) => GAPScanResponse?.Invoke(this, e);
 private void OnDeviceFound(object sender, ScanResponseEventArgs e)
 {
     _deviceNotifications?.Invoke(BleMappings.MapBleDevice(e));
 }
Exemplo n.º 8
0
 private void OnScanResponse(object sender, ScanResponseEventArgs e)
 {
     ScanResponse?.Invoke(sender, new BleScanResponseReceivedEventArgs(e.rssi, e.packet_type, e.sender, (BleAddressType)e.address_type, e.bond, e.data));
 }