Skip to content

Commit b3d328c

Browse files
committed
feat: configurable with different features
1 parent 525b03f commit b3d328c

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

android/src/main/java/com/reactnativepolarble/PolarBleModule.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class PolarBleModule(private val reactContext: ReactApplicationContext) : React
3838
PolarEvent.values().forEach {
3939
put(it.name, it.name)
4040
}
41+
put("HR", PolarBleApi.FEATURE_HR)
42+
put("DEVICE_INFO", PolarBleApi.FEATURE_DEVICE_INFO)
43+
put("BATTERY_STATUS", PolarBleApi.FEATURE_BATTERY_INFO)
44+
put("POLAR_SENSOR_STREAMING", PolarBleApi.FEATURE_POLAR_SENSOR_STREAMING)
45+
put("POLAR_FILE_TRANSFER", PolarBleApi.FEATURE_POLAR_FILE_TRANSFER)
46+
put("ALL_FEATURES", PolarBleApi.ALL_FEATURES)
4147
}
4248
}
4349

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ PODS:
293293
- React-jsinspector (0.67.3)
294294
- React-logger (0.67.3):
295295
- glog
296-
- react-native-polar-ble (0.1.0-alpha.0):
296+
- react-native-polar-ble (0.1.0-alpha.4):
297297
- PolarBleSdk (~> 3.2)
298298
- React-Core
299299
- React-perflogger (0.67.3)
@@ -548,7 +548,7 @@ SPEC CHECKSUMS:
548548
React-jsiexecutor: 15ea57ead631a11fad57634ff69f78e797113a39
549549
React-jsinspector: 1e1e03345cf6d47779e2061d679d0a87d9ae73d8
550550
React-logger: 1e10789cb84f99288479ba5f20822ce43ced6ffe
551-
react-native-polar-ble: 81b492d1631343826cd6d70b1510fceff730ca38
551+
react-native-polar-ble: f48b69f3b907ec91fe81c6aed6cf98b03936ea10
552552
React-perflogger: 93d3f142d6d9a46e635f09ba0518027215a41098
553553
React-RCTActionSheet: 87327c3722203cc79cf79d02fb83e7332aeedd18
554554
React-RCTAnimation: 009c87c018d50e0b38692699405ebe631ff4872d

example/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import {useEffect} from 'react';
33

44
import {StyleSheet, View, Text, Button} from 'react-native';
5-
import {usePolarBle} from 'react-native-polar-ble';
5+
import {Features, usePolarBle} from 'react-native-polar-ble';
66

77
export default function App() {
88
const {
@@ -14,7 +14,7 @@ export default function App() {
1414
} = usePolarBle();
1515

1616
useEffect(() => {
17-
configure(0xff);
17+
configure(Features.ALL_FEATURES);
1818
}, [configure]);
1919

2020
return (

ios/PolarBle.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@ class PolarBle: RCTEventEmitter, PolarBleApiObserver, PolarBleApiPowerStateObser
5252
}
5353

5454
@objc override func constantsToExport() -> [AnyHashable: Any]! {
55-
let keyValuePairs = PolarEvent.allCases.map {
56-
(
57-
$0.rawValue
58-
.reduce("") { $0 + ($1.isUppercase ? "_\($1.lowercased())" : "\($1)") }
59-
.uppercased(),
60-
$0.rawValue
61-
)
55+
let eventKeyValuePairs = PolarEvent.allCases.map {
56+
($0.rawValue.toCamelCase(), $0.rawValue as Any)
57+
}
58+
let featureKeyValuePairs = Features.allCases.map {
59+
(String(describing: $0), $0.rawValue as Any)
6260
}
6361

64-
return Dictionary(uniqueKeysWithValues: keyValuePairs)
62+
return Dictionary(uniqueKeysWithValues: eventKeyValuePairs + featureKeyValuePairs)
6563
}
6664

6765
@objc override class func requiresMainQueueSetup() -> Bool {
@@ -253,3 +251,12 @@ class PolarBle: RCTEventEmitter, PolarBleApiObserver, PolarBleApiPowerStateObser
253251
api?.deviceFeaturesObserver = self
254252
}
255253
}
254+
255+
extension String {
256+
func toCamelCase() -> String {
257+
return
258+
self
259+
.reduce("") { $0 + ($1.isUppercase ? "_\($1.lowercased())" : "\($1)") }
260+
.uppercased()
261+
}
262+
}

src/constants.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ const {
1515
HR_VALUE_RECEIVED,
1616
} = (PolarBle?.getConstants() as Record<string, string>) ?? {};
1717

18+
const {
19+
HR,
20+
DEVICE_INFO,
21+
BATTERY_STATUS,
22+
POLAR_SENSOR_STREAMING,
23+
POLAR_FILE_TRANSFER,
24+
ALL_FEATURES,
25+
} = (PolarBle?.getConstants() as Record<string, number>) ?? {};
26+
1827
export const Events = {
1928
DEVICE_FOUND,
2029
DEVICE_CONNECTING,
@@ -29,3 +38,12 @@ export const Events = {
2938
STREAMING_FEATURES_READY,
3039
HR_VALUE_RECEIVED,
3140
};
41+
42+
export const Features = {
43+
HR,
44+
DEVICE_INFO,
45+
BATTERY_STATUS,
46+
POLAR_SENSOR_STREAMING,
47+
POLAR_FILE_TRANSFER,
48+
ALL_FEATURES,
49+
};

src/nativeModule.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ export const PolarBleEventEmitter = Object.assign<
4141
},
4242
});
4343

44-
export const configure = (features: number) =>
45-
PolarBle.configure(features) as Promise<void>;
44+
export const configure = (features: number | number[]) =>
45+
PolarBle.configure(
46+
Array.isArray(features)
47+
? // eslint-disable-next-line no-bitwise
48+
features.reduce((prev, curr) => prev | curr)
49+
: features,
50+
) as Promise<void>;
4651

4752
export const connectToDevice = (id: string) =>
4853
PolarBle.connectToDevice(id) as Promise<void>;

0 commit comments

Comments
 (0)