@@ -467,12 +467,36 @@ private set
467467 }
468468 }
469469
470+ private ulong gattServicesChangedInstances = 0 ;
471+
472+ public ulong GattServicesChangedInstances
473+ {
474+ get
475+ {
476+ return gattServicesChangedInstances ;
477+ }
478+
479+ private set
480+ {
481+ if ( gattServicesChangedInstances != value )
482+ {
483+ gattServicesChangedInstances = value ;
484+ OnPropertyChanged ( new PropertyChangedEventArgs ( "GattServicesChangedInstances" ) ) ;
485+ }
486+ }
487+ }
488+
470489 /// <summary>
471490 /// Releases references to Services and the BluetoothLEDevice
472491 /// </summary>
473492 public void Dispose ( )
474493 {
475- Services . Clear ( ) ;
494+ lock ( Services )
495+ {
496+ Services . Clear ( ) ;
497+ ServiceCount = 0 ;
498+ }
499+
476500 var temp = bluetoothLEDevice ;
477501 BluetoothLEDevice = null ;
478502
@@ -550,15 +574,9 @@ await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatch
550574 Debug . WriteLine ( debugMsg + "In UI thread" ) ;
551575 try
552576 {
553- if ( bluetoothLEDevice == null )
554- {
555- Debug . WriteLine ( debugMsg + "Calling BluetoothLEDevice.FromIdAsync" ) ;
556- BluetoothLEDevice = await BluetoothLEDevice . FromIdAsync ( DeviceInfo . Id ) ;
557- }
558- else
559- {
560- Debug . WriteLine ( debugMsg + "Previously connected, not calling BluetoothLEDevice.FromIdAsync" ) ;
561- }
577+ Dispose ( ) ;
578+ Debug . WriteLine ( debugMsg + "Calling BluetoothLEDevice.FromIdAsync" ) ;
579+ BluetoothLEDevice = await BluetoothLEDevice . FromIdAsync ( DeviceInfo . Id ) ;
562580
563581 if ( bluetoothLEDevice == null )
564582 {
@@ -575,6 +593,7 @@ await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatch
575593 // Setup our event handlers and view model properties
576594 BluetoothLEDevice . ConnectionStatusChanged += BluetoothLEDevice_ConnectionStatusChanged ;
577595 BluetoothLEDevice . NameChanged += BluetoothLEDevice_NameChanged ;
596+ GattServicesChangedInstances = 0 ;
578597 BluetoothLEDevice . GattServicesChanged += BluetoothLEDevice_GattServicesChanged ;
579598
580599 IsPaired = DeviceInfo . Pairing . IsPaired ;
@@ -623,24 +642,28 @@ private async Task<bool> GetAllPrimaryServices(BluetoothCacheMode cacheMode)
623642
624643 // Get all the services for this device
625644 var result = await BluetoothLEDevice . GetGattServicesAsync ( cacheMode ) ;
626- var returnedServices = new DisposableObservableCollection < ObservableGattDeviceService > ( ) ;
627645
628646 if ( result . Status == GattCommunicationStatus . Success )
629647 {
630648 System . Diagnostics . Debug . WriteLine ( debugMsg + "GetGattServiceAsync SUCCESS" ) ;
631- foreach ( var serv in result . Services )
649+
650+ lock ( Services )
632651 {
633- if ( ! GattServiceUuidHelper . IsReserved ( serv . Uuid ) )
652+ foreach ( var serv in result . Services )
634653 {
635- var temp = new ObservableGattDeviceService ( serv ) ;
636- // This isn't awaited so that the user can disconnect while the services are still being enumerated
637- temp . Initialize ( ) ;
638- returnedServices . Add ( temp ) ;
639- }
640- else
641- {
642- serv . Dispose ( ) ;
654+ if ( ! GattServiceUuidHelper . IsReserved ( serv . Uuid ) )
655+ {
656+ var temp = new ObservableGattDeviceService ( serv ) ;
657+ // This isn't awaited so that the user can disconnect while the services are still being enumerated
658+ temp . Initialize ( ) ;
659+ Services . Add ( temp ) ;
660+ }
661+ else
662+ {
663+ serv . Dispose ( ) ;
664+ }
643665 }
666+ ServiceCount = Services . Count ( ) ;
644667 }
645668
646669 succeeded = true ;
@@ -662,14 +685,6 @@ private async Task<bool> GetAllPrimaryServices(BluetoothCacheMode cacheMode)
662685 await messageDialog . ShowAsync ( ) ;
663686 }
664687
665- lock ( Services )
666- {
667- // In case we connected before, clear the service list and recreate it
668- Services . Clear ( ) ;
669- Services = returnedServices ;
670- ServiceCount = Services . Count ( ) ;
671- }
672-
673688 return succeeded ;
674689 }
675690
@@ -722,12 +737,13 @@ await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatch
722737
723738 private async void BluetoothLEDevice_GattServicesChanged ( BluetoothLEDevice sender , object args )
724739 {
725- await Windows . ApplicationModel . Core . CoreApplication . MainView . CoreWindow . Dispatcher . RunTaskAsync ( async ( ) =>
740+ await Windows . ApplicationModel . Core . CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync (
741+ Windows . UI . Core . CoreDispatcherPriority . Normal ,
742+ ( ) =>
726743 {
727- // If we received a service change. If we are in the middle of discovery services,
728- // we can ignore the service change.
729- await GetAllPrimaryServices ( IsPaired ? BluetoothCacheMode . Cached : BluetoothCacheMode . Uncached ) ;
744+ GattServicesChangedInstances += 1 ;
730745 } ) ;
746+
731747 }
732748
733749 /// <summary>
0 commit comments