in Windows/ExpressivePixelsActivator/ConnectivityCore/BLE_DeviceManager.cs [158:270]
public async void OnBLEAdvertismentReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
// The local name of the advertising device contained within the payload, if any
string localName = eventArgs.Advertisement.LocalName.TrimEnd('\0');
UInt16 extractedAppearanceID = 0;
//Debug.WriteLine("## BLE SCAN ENTRY " + localName);
try
{
// The BLE scan response contains the service IDs
if(eventArgs.AdvertisementType == BluetoothLEAdvertisementType.ScanResponse)
{
ScannedDevice scannedDevice = null;
// See if the Advertisement has been received for this device
if (_scannedDevices.TryGetValue(eventArgs.BluetoothAddress, out scannedDevice))
{
if (scannedDevice.ScanResponsePending)
{
// Look for the UART service
Guid guid = eventArgs.Advertisement.ServiceUuids.FirstOrDefault(i => i == GattServiceUuids.UART);
if (guid != null)
{
// Device is now registered
_scannedDevices[eventArgs.BluetoothAddress].ScanResponsePending = false;
BLE_Device bleConntectibleDevice = new BLE_Device(scannedDevice.Name, scannedDevice.BLEAddress);
#if WINDOWS_UWP
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
#else
Application.Current.Dispatcher.Invoke(() =>
#endif
{
_discoveredDevices.Add(bleConntectibleDevice);
});
}
}
else
{
// Debug.WriteLine(eventArgs.Advertisement.ManufacturerData.Count);
if (eventArgs.Advertisement.ManufacturerData.Count > 0)
{
// Only print the first one of the list
var manufacturerData = eventArgs.Advertisement.ManufacturerData[0];
var data = new byte[manufacturerData.Data.Length];
using (var reader = DataReader.FromBuffer(manufacturerData.Data))
{
reader.ReadBytes(data);
if (scannedDevice.LastManufacturerData == null || !data.SequenceEqual(scannedDevice.LastManufacturerData))
scannedDevice.LastManufacturerData = data;
}
}
}
}
}
// Extract the Appearance ID from the advertisement
BluetoothLEAdvertisementDataSection dataItem = eventArgs.Advertisement.DataSections.FirstOrDefault(i => i.DataType == GAP_ADTYPE_APPEARANCE);
if(dataItem != null && dataItem.Data.Capacity == sizeof(UInt16))
{
var data = new byte[dataItem.Data.Length];
using (var reader = DataReader.FromBuffer(dataItem.Data))
reader.ReadBytes(data);
extractedAppearanceID = BitConverter.ToUInt16(data, 0);
if (extractedAppearanceID == GAP_APPEARANCE_ID)
{
ScannedDevice scannedDevice = null;
// Debug.WriteLine("FOUND CANDIDATE DEVICE " + localName);
if (_scannedDevices.TryGetValue(eventArgs.BluetoothAddress, out scannedDevice))
{
// Update the scan time
scannedDevice.ScanTime = DateTime.Now;
// See if the device's name has changed
if (scannedDevice.Name.CompareTo(localName) != 0)
{
scannedDevice.Name = localName;
#if WINDOWS_UWP
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
#else
Application.Current.Dispatcher.Invoke(() =>
#endif
{
var discoveredItem = _discoveredDevices.FirstOrDefault(i => i.DeviceID == scannedDevice.BLEAddress.ToString());
if (discoveredItem != null)
discoveredItem.DeviceName = localName;
});
}
}
else
{
scannedDevice = new ScannedDevice();
scannedDevice.Name = localName;
scannedDevice.BLEAddress = eventArgs.BluetoothAddress;
scannedDevice.ScanTime = DateTime.Now;
scannedDevice.ScanResponsePending = true;
_scannedDevices.Add(eventArgs.BluetoothAddress, scannedDevice);
}
}
}
ulong address = eventArgs.BluetoothAddress;
short rssi = eventArgs.RawSignalStrengthInDBm;
}
catch(Exception ex)
{
Debug.WriteLine("## BLE SCAN EXIT EXCEPTION " + localName + " " + ex.ToString());
DiagnosticsHandler?.Invoke(this, "## BLE SCAN EXIT EXCEPTION " + localName + " " + ex.ToString());
}
}