private async void OnBleAdvertisementUpdated(BleAdvertisementWatcher sender, DeviceWatchEvent eventCode, BLEAdvertisementInfo bleAdvertisementInfo) { DeviceInformation btDeviceInformation = await BluetoothDevices.CreateDeviceInformation(bleAdvertisementInfo.ToBtAddressString(), AddressType.Bluetooth); DeviceInformation bleDeviceInformation = await BluetoothDevices.CreateDeviceInformation(bleAdvertisementInfo.ToBtAddressString(), AddressType.BLE); if (!App.sUserManager.IsSingnIn()) { // FIXME: SERVICE CODE to configuration if (bleAdvertisementInfo.Advertisement.ManufacturerData[0].Data.Length > 5) { // ignore, because it has user Key. Log.D(string.Format($"Other User's Device Found: {bleAdvertisementInfo.Advertisement.ManufacturerData[0].Data}")); return; } } if (eventCode == DeviceWatchEvent.Added) { Log.D(string.Format($"New BLE Advertisement Found: {bleAdvertisementInfo.ToBtAddressString()}")); AddNearByEarbuds(bleDeviceInformation.Id); AddNearByBtDevice(bleDeviceInformation.Id, btDeviceInformation.Name); } else if (eventCode == DeviceWatchEvent.Removed) { Log.D(string.Format($"BLE Advertisement Removed: {bleAdvertisementInfo.ToBtAddressString()}")); Earbuds removeEarBuds = RemoveNearByEarbuds(bleDeviceInformation.Id); if (removeEarBuds != null) { RemoveNearByBtDevice(bleDeviceInformation.Id, btDeviceInformation.Name); } } }
// BLE Advertisement Handler private void OnBleAdvertisementUpdated(BleAdvertisementWatcher sender, DeviceWatchEvent eventCode, BLEAdvertisementInfo bleAdvertisementInfo) { if (userKey == null || userKey.Length == 0) { // FIXME: SERVICE CODE to configuration if (bleAdvertisementInfo.Advertisement.ManufacturerData[0].Data.Length > 5) { // ignore, because it has user Key. return; } } if (eventCode == DeviceWatchEvent.Added) { Log.D($"New BLE Advertisement Found: {bleAdvertisementInfo.ToBtAddressString()}"); ValueSet newBleDeviceMessage = new ValueSet { { "opcode", "ble_device" }, { "status", "added" }, { "name", bleAdvertisementInfo.Advertisement.LocalName }, { "id", $"Bluetooth#Bluetooth{localBtAddress}-{bleAdvertisementInfo.ToBtAddressString()}" } }; systrayApplicationServiceConnection.SendMessageAsync(newBleDeviceMessage); } else if (eventCode == DeviceWatchEvent.Removed) { Log.D($"BLE Advertisement Removed: {bleAdvertisementInfo.ToBtAddressString()}"); ValueSet removeBleDeviceMessage = new ValueSet { { "opcode", "ble_device" }, { "status", "removed" }, { "name", bleAdvertisementInfo.Advertisement.LocalName }, { "id", $"Bluetooth#Bluetooth{localBtAddress}-{bleAdvertisementInfo.ToBtAddressString()}" } }; systrayApplicationServiceConnection.SendMessageAsync(removeBleDeviceMessage); } // Propergate Event to Earbuds Menu string deviceId = $"Bluetooth#Bluetooth{localBtAddress}-{bleAdvertisementInfo.ToBtAddressString()}"; foreach (EarbudsMenuItem earbudsMenuItem in PairedDeviceMenuItems) { if (BluetoothDevices.EqualDevice(deviceId, earbudsMenuItem.BtID)) { earbudsMenuItem.HandleBleAdvertisement(eventCode); break; } } }