From b725ca9f229cd01c5c9a8db9900d2cd322c65b8a Mon Sep 17 00:00:00 2001 From: Mas Azalya Date: Wed, 27 Jul 2022 11:26:41 +0800 Subject: [PATCH 1/4] add service, invoke new event for steps count update --- ShimmerBLE/ShimmerBLEAPI/App.xaml.cs | 2 ++ .../IVerisenseByteCommunication.cs | 3 +- .../Communications/RadioPluginBLE.cs | 32 ++++++++++++++++++- .../Devices/VerisenseBLEDevice.cs | 12 +++++++ .../Models/ShimmerBLEEventData.cs | 3 +- 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/ShimmerBLE/ShimmerBLEAPI/App.xaml.cs b/ShimmerBLE/ShimmerBLEAPI/App.xaml.cs index 33eb6df9..62b484cf 100644 --- a/ShimmerBLE/ShimmerBLEAPI/App.xaml.cs +++ b/ShimmerBLE/ShimmerBLEAPI/App.xaml.cs @@ -19,6 +19,8 @@ public partial class App : Application public static Guid ServiceID = Guid.Parse("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); public static Guid TxID = Guid.Parse("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"); public static Guid RxID = Guid.Parse("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"); + public static Guid RSCServiceID = Guid.Parse("00001814-0000-1000-8000-00805f9b34fb"); + public static Guid RSCMeasurementID = Guid.Parse("00002a53-0000-1000-8000-00805f9b34fb"); public static long RealmDBSizeLimitInMB = 500; public static readonly long MinIntervalRealmDBSizeLimitInMB = 86400000; //public static bool CompactDB = RealmService.CompactAndMigrateDatabase(); //first DB operation when you open the app diff --git a/ShimmerBLE/ShimmerBLEAPI/Communications/IVerisenseByteCommunication.cs b/ShimmerBLE/ShimmerBLEAPI/Communications/IVerisenseByteCommunication.cs index ea84bd2a..e43f653e 100644 --- a/ShimmerBLE/ShimmerBLEAPI/Communications/IVerisenseByteCommunication.cs +++ b/ShimmerBLE/ShimmerBLEAPI/Communications/IVerisenseByteCommunication.cs @@ -35,7 +35,8 @@ public class ByteLevelCommunicationEvent public enum CommEvent { Disconnected = 1, - NewBytes = 2 + NewBytes = 2, + NewSteps = 3 } } } diff --git a/ShimmerBLE/ShimmerBLEAPI/Communications/RadioPluginBLE.cs b/ShimmerBLE/ShimmerBLEAPI/Communications/RadioPluginBLE.cs index 042062fe..82aab564 100644 --- a/ShimmerBLE/ShimmerBLEAPI/Communications/RadioPluginBLE.cs +++ b/ShimmerBLE/ShimmerBLEAPI/Communications/RadioPluginBLE.cs @@ -25,6 +25,9 @@ public class RadioPluginBLE : IVerisenseByteCommunication ICharacteristic UartRX { get; set; } ICharacteristic UartTX { get; set; } IService ServiceTXRX { get; set; } + IService ServiceRSC { get; set; } + ICharacteristic RSCMeasure { get; set; } + ConnectivityState StateOfConnectivity = ConnectivityState.Unknown; /// @@ -86,7 +89,8 @@ public async Task Connect() AdvanceLog(nameof(RadioPluginBLE), "Connect ASM Hash", ConnectedASM.GetHashCode(), Asm_uuid.ToString()); await Task.Delay(500); System.Console.WriteLine("Getting Service"); - ServiceTXRX = await ConnectedASM.GetServiceAsync(App.ServiceID); + ServiceTXRX = await ConnectedASM.GetServiceAsync(App.ServiceID); + ServiceRSC = await ConnectedASM.GetServiceAsync(App.RSCServiceID); if (ServiceTXRX != null) { @@ -97,6 +101,12 @@ public async Task Connect() System.Console.WriteLine("Getting RX Characteristics Completed"); UartRX.ValueUpdated += UartRX_ValueUpdated; await UartRX.StartUpdatesAsync(); + if(ServiceRSC != null) + { + RSCMeasure = await ServiceRSC.GetCharacteristicAsync(App.RSCMeasurementID); + RSCMeasure.ValueUpdated += RSCMeasurement_ValueUpdated; + await RSCMeasure.StartUpdatesAsync(); + } AdvanceLog(nameof(RadioPluginBLE), "GetKnownDevice", "Success", Asm_uuid.ToString()); //StateChange(ShimmerDeviceBluetoothState.Connected); @@ -201,6 +211,12 @@ public async Task Disconnect() UartRX.Service.Dispose(); UartRX = null; } + if (RSCMeasure != null) + { + RSCMeasure.ValueUpdated -= RSCMeasurement_ValueUpdated; + RSCMeasure.Service.Dispose(); + RSCMeasure = null; + } if (UartTX != null) { UartTX.Service.Dispose(); @@ -288,6 +304,14 @@ private void UartRX_ValueUpdated(object sender, Plugin.BLE.Abstractions.EventArg } } + private void RSCMeasurement_ValueUpdated(object sender, Plugin.BLE.Abstractions.EventArgs.CharacteristicUpdatedEventArgs e) + { + if (CommunicationEvent != null) + { + CommunicationEvent.Invoke(null, new ByteLevelCommunicationEvent { Bytes = e.Characteristic.Value, Event = Communications.ByteLevelCommunicationEvent.CommEvent.NewSteps }); + } + } + private bool disposedValue = false; /// /// Disconnect and Dispose Veriense BLE Device @@ -308,6 +332,12 @@ public void Dispose(bool disposing) UartRX = null; } + if (RSCMeasure != null) + { + RSCMeasure.ValueUpdated -= RSCMeasurement_ValueUpdated; + RSCMeasure = null; + } + UartTX = null; //ResponseBuffer = null; diff --git a/ShimmerBLE/ShimmerBLEAPI/Devices/VerisenseBLEDevice.cs b/ShimmerBLE/ShimmerBLEAPI/Devices/VerisenseBLEDevice.cs index 7b45584f..d7b3f0b3 100644 --- a/ShimmerBLE/ShimmerBLEAPI/Devices/VerisenseBLEDevice.cs +++ b/ShimmerBLE/ShimmerBLEAPI/Devices/VerisenseBLEDevice.cs @@ -284,6 +284,17 @@ protected void UartRX_ValueUpdated(object sender, ByteLevelCommunicationEvent co DataRequestTimer.Dispose(); //there is no point having the timer, if the connection is disconnected } StateChange(ShimmerDeviceBluetoothState.Disconnected); + } + else if (comEvent.Event == ByteLevelCommunicationEvent.CommEvent.NewSteps) + { + byte[] bytes = comEvent.Bytes; + + int strideLength = int.Parse(bytes[4].ToString(), System.Globalization.NumberStyles.HexNumber); + int totalDistance = int.Parse(bytes[6].ToString(), System.Globalization.NumberStyles.HexNumber); + int stepsCount = totalDistance / strideLength; + + if (ShimmerBLEEvent != null) + ShimmerBLEEvent.Invoke(null, new ShimmerBLEEventData { ASMID = Asm_uuid.ToString(), CurrentEvent = VerisenseBLEEvent.NewStepsData, ObjMsg = stepsCount }); } } @@ -1255,6 +1266,7 @@ void HandleCompletePayload() { DataBuffer.Finish = DateTime.Now; DataBuffer.Transfer = DataBuffer.CurrentLength / (DataBuffer.Finish - DataBuffer.Start).TotalSeconds; + Console.WriteLine("Current Length Complete Payload = " + DataBuffer.CurrentLength); string syncProgress = string.Format("{0:.##} KB/s", DataBuffer.Transfer / 1024.0) + "(" + ShimmerBLEAPI.Resources.AppResources.PayloadIndex + ": " + PayloadIndex + ")"; AdvanceLog(LogObject, "Payload transfer rate", syncProgress, ASMName); //InvokeSyncEvent(Asm_uuid.ToString(), new SyncEventData { ASMID = Asm_uuid.ToString(), CurrentEvent = SyncEvent.DataSync, SyncProgress = syncProgress }); diff --git a/ShimmerBLE/ShimmerBLEAPI/Models/ShimmerBLEEventData.cs b/ShimmerBLE/ShimmerBLEAPI/Models/ShimmerBLEEventData.cs index 85cec28f..794df6f9 100644 --- a/ShimmerBLE/ShimmerBLEAPI/Models/ShimmerBLEEventData.cs +++ b/ShimmerBLE/ShimmerBLEAPI/Models/ShimmerBLEEventData.cs @@ -23,7 +23,8 @@ public enum VerisenseBLEEvent RequestResponse = 5, WriteResponse = 6, RequestResponseFail = 7, - DataStreamCRCFail = 8 + DataStreamCRCFail = 8, + NewStepsData = 9 } } From 04e2129a959f79252c2a28876b0762730d04a45c Mon Sep 17 00:00:00 2001 From: Mas Azalya Date: Tue, 2 Aug 2022 15:30:27 +0800 Subject: [PATCH 2/4] minor fix to RSC service subscribe and dispose --- .../Communications/RadioPluginBLE.cs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/ShimmerBLE/ShimmerBLEAPI/Communications/RadioPluginBLE.cs b/ShimmerBLE/ShimmerBLEAPI/Communications/RadioPluginBLE.cs index 82aab564..499bd2c2 100644 --- a/ShimmerBLE/ShimmerBLEAPI/Communications/RadioPluginBLE.cs +++ b/ShimmerBLE/ShimmerBLEAPI/Communications/RadioPluginBLE.cs @@ -88,10 +88,19 @@ public async Task Connect() AdvanceLog(nameof(RadioPluginBLE), "Connect ASM Hash", ConnectedASM.GetHashCode(), Asm_uuid.ToString()); await Task.Delay(500); - System.Console.WriteLine("Getting Service"); - ServiceTXRX = await ConnectedASM.GetServiceAsync(App.ServiceID); + System.Console.WriteLine("Getting Services"); + ServiceRSC = await ConnectedASM.GetServiceAsync(App.RSCServiceID); + if (ServiceRSC != null) + { + RSCMeasure = await ServiceRSC.GetCharacteristicAsync(App.RSCMeasurementID); + RSCMeasure.ValueUpdated += RSCMeasurement_ValueUpdated; + await RSCMeasure.StartUpdatesAsync(); + } + + ServiceTXRX = await ConnectedASM.GetServiceAsync(App.ServiceID); + if (ServiceTXRX != null) { UartTX = await ServiceTXRX.GetCharacteristicAsync(App.TxID); @@ -101,13 +110,7 @@ public async Task Connect() System.Console.WriteLine("Getting RX Characteristics Completed"); UartRX.ValueUpdated += UartRX_ValueUpdated; await UartRX.StartUpdatesAsync(); - if(ServiceRSC != null) - { - RSCMeasure = await ServiceRSC.GetCharacteristicAsync(App.RSCMeasurementID); - RSCMeasure.ValueUpdated += RSCMeasurement_ValueUpdated; - await RSCMeasure.StartUpdatesAsync(); - } - + AdvanceLog(nameof(RadioPluginBLE), "GetKnownDevice", "Success", Asm_uuid.ToString()); //StateChange(ShimmerDeviceBluetoothState.Connected); localTask.TrySetResult(true); @@ -227,7 +230,13 @@ public async Task Disconnect() ServiceTXRX.Dispose(); ServiceTXRX = null; } - + + if (ServiceRSC != null) + { + ServiceRSC.Dispose(); + ServiceRSC = null; + } + //ResponseBuffer = null; if (ConnectedASM != null) From c7c949bd00ca4dd53f121eafd4b0b0adf0453687 Mon Sep 17 00:00:00 2001 From: weiwentan23 Date: Wed, 3 Aug 2022 15:27:40 +0800 Subject: [PATCH 3/4] add step count option --- .../BLE.Client/Pages/DeviceListPage.xaml | 4 ++ .../ViewModels/DeviceListViewModel.cs | 24 ++++++++++++ .../ShimmerBLEAPI/Sensors/SensorLSM6DS3.cs | 39 +++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/ShimmerBLE/BLE.Client/BLE.Client/Pages/DeviceListPage.xaml b/ShimmerBLE/BLE.Client/BLE.Client/Pages/DeviceListPage.xaml index ba6ce9bb..69bc0d20 100644 --- a/ShimmerBLE/BLE.Client/BLE.Client/Pages/DeviceListPage.xaml +++ b/ShimmerBLE/BLE.Client/BLE.Client/Pages/DeviceListPage.xaml @@ -610,6 +610,10 @@