public DiscoveryEventArgs(DiscoveryType type, MAC mac, MacType macType, byte[] rawAdvertisement, sbyte rssi) { Type = type; MAC = mac; MacType = macType; RawAdvertisements = rawAdvertisement; Advertisements = new Dictionary <byte, byte[]>(); var i = 0; while (i < rawAdvertisement.Length) { // Notice that advertisement or scan response data must be formatted in accordance to the Bluetooth Core // Specification.See BLUETOOTH SPECIFICATION Version 4.0[Vol 3 - Part C - Chapter 11]. var length = rawAdvertisement[i++]; if (length == 0) { break; } var key = rawAdvertisement[i++]; var value = new byte[length - 1]; Array.Copy(rawAdvertisement, i, value, 0, value.Length); Advertisements[key] = value; i += value.Length; } Name = Advertisements.TryGetValue(0x08, out var nameValue) || Advertisements.TryGetValue(0x09, out nameValue) ? Encoding.UTF8.GetString(nameValue) : null; RSSI = rssi; }
public async Task <Peripheral> ConnectAsync(MAC mac, MacType macType) { var connectionTCS = new TaskCompletionSource <byte>(); var onStatus = new EventHandler <StatusEventArgs>((s, e) => { var eMAC = new MAC(e.Address); var eMacType = (MacType)e.AddressType; if (eMAC != mac || eMacType != macType) { return; } connectionTCS.TrySetResult(e.Connection); }); _messageHub.Connection.Status += onStatus; try { var value = mac.ToArray(); var type = (AddressType)macType; await _messageHub.GAP.ConnectDirectAsync(value, type, 0x20, 0x30, 0x100, 0); var connection = await connectionTCS.Task; var peripheral = new Peripheral(connection, mac, macType); _peripherals[connection] = peripheral; return(peripheral); } finally { _messageHub.Connection.Status -= onStatus; } }
public Peripheral(byte connection, MAC mac, MacType macType) { Services = new Dictionary <ushort, GattService>(); Characteristics = new Dictionary <ushort, GattCharacteristic>(); Connection = connection; MAC = mac; MacType = macType; }
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); }