react-native-polar-bridge 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,12 +17,14 @@ import io.reactivex.rxjava3.core.Flowable
17
17
  import io.reactivex.rxjava3.core.Single
18
18
  import java.util.*
19
19
  import java.util.concurrent.atomic.AtomicInteger
20
+ import java.util.concurrent.TimeUnit
20
21
  import java.time.Instant
21
22
 
22
23
  @ReactModule(name = PolarBridgeModule.NAME)
23
24
  class PolarBridgeModule(reactContext: ReactApplicationContext) :
24
25
  NativePolarBridgeSpec(reactContext) {
25
26
  private val reactContext: ReactApplicationContext = reactContext
27
+ private val SENSOR_BUFFER_SECONDS = 10L
26
28
 
27
29
  override fun getName(): String {
28
30
  return NAME
@@ -617,17 +619,34 @@ class PolarBridgeModule(reactContext: ReactApplicationContext) :
617
619
  }
618
620
  }
619
621
 
622
+ data class HrSampleWithTimestamp(
623
+ val data: PolarHrData.PolarHrSample,
624
+ val timestampMs: Long
625
+ )
626
+
620
627
  override fun fetchHrData(deviceId: String) {
621
628
  Log.e(TAG, "Fetch Heart Data called on: $deviceId ")
622
629
  val isDisposed = hrDisposable?.isDisposed ?: true
623
630
  try{
624
631
  if (isDisposed) {
625
632
  hrDisposable = api.startHrStreaming(deviceId)
633
+ .flatMapIterable { hrData ->
634
+ hrData.samples.map { sample ->
635
+ HrSampleWithTimestamp(
636
+ data = sample,
637
+ timestampMs = System.currentTimeMillis()
638
+ )
639
+ }
640
+ }
641
+ .buffer(SENSOR_BUFFER_SECONDS, TimeUnit.SECONDS)
626
642
  .observeOn(AndroidSchedulers.mainThread())
627
643
  .subscribe(
628
- { hrData: PolarHrData ->
629
- Log.i(TAG, "PolarHrData ${hrData.samples.size}")
630
- for (sample in hrData.samples) {
644
+ { samples ->
645
+ if (samples.isEmpty()) return@subscribe
646
+
647
+ Log.d(TAG, "Flushing HR buffer (${samples.size} samples)")
648
+ for (item in samples) {
649
+ val sample = item.data
631
650
  Log.d(TAG, "HR bpm: ${sample.hr} " +
632
651
  "rrs: ${sample.rrsMs} " +
633
652
  "rrAvailable: ${sample.rrAvailable} " +
@@ -645,6 +664,7 @@ class PolarBridgeModule(reactContext: ReactApplicationContext) :
645
664
  event.putBoolean("rrAvailable", sample.rrAvailable)
646
665
  event.putBoolean("contactStatus", sample.contactStatus)
647
666
  event.putBoolean("contactStatusSupported", sample.contactStatusSupported)
667
+ event.putDouble("timestamp", item.timestampMs.toDouble())
648
668
 
649
669
  sendEvent("PolarHrData", event)
650
670
  }
@@ -719,10 +739,15 @@ class PolarBridgeModule(reactContext: ReactApplicationContext) :
719
739
  .flatMap { settings: PolarSensorSetting ->
720
740
  api.startAccStreaming(deviceId, settings)
721
741
  }
742
+ .flatMapIterable { it.samples }
743
+ .buffer(SENSOR_BUFFER_SECONDS, TimeUnit.SECONDS)
722
744
  .observeOn(AndroidSchedulers.mainThread())
723
745
  .subscribe(
724
- { polarAccelerometerData: PolarAccelerometerData ->
725
- for (data in polarAccelerometerData.samples) {
746
+ { samples ->
747
+ if (samples.isEmpty()) return@subscribe
748
+ Log.d(TAG, "Flushing ACC buffer (${samples.size} samples)")
749
+
750
+ for (data in samples) {
726
751
  Log.d(TAG, "ACC x: ${data.x} y: ${data.y} z: ${data.z} timeStamp: ${data.timeStamp}")
727
752
 
728
753
  val event: WritableMap = Arguments.createMap()
@@ -770,10 +795,16 @@ class PolarBridgeModule(reactContext: ReactApplicationContext) :
770
795
  .flatMap { settings: PolarSensorSetting ->
771
796
  api.startGyroStreaming(deviceId, settings)
772
797
  }
798
+ .flatMapIterable { it.samples }
799
+ .buffer(SENSOR_BUFFER_SECONDS, TimeUnit.SECONDS)
773
800
  .observeOn(AndroidSchedulers.mainThread())
774
801
  .subscribe(
775
- { polarGyroData: PolarGyroData ->
776
- for (data in polarGyroData.samples) {
802
+ { samples ->
803
+ if (samples.isEmpty()) return@subscribe
804
+
805
+ Log.d(TAG, "Flushing GYR buffer (${samples.size} samples)")
806
+
807
+ for (data in samples) {
777
808
  Log.d(TAG, "GYR x: ${data.x} y: ${data.y} z: ${data.z} timeStamp: ${data.timeStamp}")
778
809
 
779
810
  val event: WritableMap = Arguments.createMap()
@@ -822,25 +853,31 @@ class PolarBridgeModule(reactContext: ReactApplicationContext) :
822
853
  .flatMap { settings: PolarSensorSetting ->
823
854
  api.startPpgStreaming(deviceId, settings)
824
855
  }
856
+ .filter { it.type == PolarPpgData.PpgDataType.PPG3_AMBIENT1 }
857
+ .flatMapIterable { it.samples }
858
+ .buffer(SENSOR_BUFFER_SECONDS, TimeUnit.SECONDS)
859
+ .observeOn(AndroidSchedulers.mainThread())
825
860
  .subscribe(
826
- { polarPpgData: PolarPpgData ->
827
- if (polarPpgData.type == PolarPpgData.PpgDataType.PPG3_AMBIENT1) {
828
- for (data in polarPpgData.samples) {
829
- Log.d(TAG, "PPG ppg0: ${data.channelSamples[0]} ppg1: ${data.channelSamples[1]} ppg2: ${data.channelSamples[2]} ambient: ${data.channelSamples[3]} timeStamp: ${data.timeStamp}")
830
-
831
- val event: WritableMap = Arguments.createMap()
832
- // Float not supported
833
- // See: https://github.com/facebook/react-native/issues/9685
834
- // See: https://javadoc.io/doc/com.facebook.react/react-native/0.20.1/com/facebook/react/bridge/WritableMap.html
835
- event.putString("ppg0", "${data.channelSamples[0]}")
836
- event.putString("ppg1", "${data.channelSamples[1]}")
837
- event.putString("ppg2", "${data.channelSamples[2]}")
838
- event.putString("ambient", "${data.channelSamples[3]}")
839
- // Long not supported, use double as workaround
840
- event.putDouble("ppgTimestamp", data.timeStamp.toDouble())
841
-
842
- sendEvent("PolarPpgData", event)
843
- }
861
+ { samples ->
862
+ if (samples.isEmpty()) return@subscribe
863
+
864
+ Log.d(TAG, "Flushing PPG buffer (${samples.size} samples)")
865
+
866
+ for (data in samples) {
867
+ Log.d(TAG, "PPG ppg0: ${data.channelSamples[0]} ppg1: ${data.channelSamples[1]} ppg2: ${data.channelSamples[2]} ambient: ${data.channelSamples[3]} timeStamp: ${data.timeStamp}")
868
+
869
+ val event: WritableMap = Arguments.createMap()
870
+ // Float not supported
871
+ // See: https://github.com/facebook/react-native/issues/9685
872
+ // See: https://javadoc.io/doc/com.facebook.react/react-native/0.20.1/com/facebook/react/bridge/WritableMap.html
873
+ event.putString("ppg0", "${data.channelSamples[0]}")
874
+ event.putString("ppg1", "${data.channelSamples[1]}")
875
+ event.putString("ppg2", "${data.channelSamples[2]}")
876
+ event.putString("ambient", "${data.channelSamples[3]}")
877
+ // Long not supported, use double as workaround
878
+ event.putDouble("ppgTimestamp", data.timeStamp.toDouble())
879
+
880
+ sendEvent("PolarPpgData", event)
844
881
  }
845
882
  },
846
883
  { error: Throwable ->
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-polar-bridge",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Polar SDK for React Native",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/module/index.js",