@onekeyfe/react-native-ble-utils 0.1.0

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.
@@ -0,0 +1,41 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+ folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
5
+
6
+ Pod::Spec.new do |s|
7
+ s.name = "BleUtils"
8
+ s.version = package["version"]
9
+ s.summary = package["description"]
10
+ s.homepage = package["homepage"]
11
+ s.license = package["license"]
12
+ s.authors = package["author"]
13
+
14
+ s.platforms = { :ios => min_ios_version_supported }
15
+ s.source = { :git => "https://onekey.so.git", :tag => "#{s.version}" }
16
+
17
+ s.source_files = "ios/**/*.{h,m,mm,swift}"
18
+
19
+ # Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
20
+ # See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
21
+ if respond_to?(:install_modules_dependencies, true)
22
+ install_modules_dependencies(s)
23
+ else
24
+ s.dependency "React-Core"
25
+
26
+ # Don't install the dependencies when we run `pod install` in the old architecture.
27
+ if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
28
+ s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
29
+ s.pod_target_xcconfig = {
30
+ "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
31
+ "OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
32
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
33
+ }
34
+ s.dependency "React-Codegen"
35
+ s.dependency "RCT-Folly"
36
+ s.dependency "RCTRequired"
37
+ s.dependency "RCTTypeSafety"
38
+ s.dependency "ReactCommon/turbomodule/core"
39
+ end
40
+ end
41
+ end
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 OneKey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # react-native-ble-utils
2
+
3
+ ble uilts
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install react-native-ble-utils
9
+ ```
10
+
11
+ ## Usage
12
+
13
+
14
+ ```js
15
+ import { multiply } from 'react-native-ble-utils';
16
+
17
+ // ...
18
+
19
+ const result = await multiply(3, 7);
20
+ ```
21
+
22
+
23
+ ## Contributing
24
+
25
+ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
26
+
27
+ ## License
28
+
29
+ MIT
30
+
31
+ ---
32
+
33
+ Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
@@ -0,0 +1,88 @@
1
+ buildscript {
2
+ ext.getExtOrDefault = {name ->
3
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['BleUtils_' + name]
4
+ }
5
+
6
+ repositories {
7
+ google()
8
+ mavenCentral()
9
+ }
10
+
11
+ dependencies {
12
+ classpath "com.android.tools.build:gradle:8.3.0"
13
+ // noinspection DifferentKotlinGradleVersion
14
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
15
+ }
16
+ }
17
+
18
+
19
+ def isNewArchitectureEnabled() {
20
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
21
+ }
22
+
23
+ apply plugin: "com.android.library"
24
+ apply plugin: "kotlin-android"
25
+
26
+ if (isNewArchitectureEnabled()) {
27
+ apply plugin: "com.facebook.react"
28
+ }
29
+
30
+ def getExtOrIntegerDefault(name) {
31
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["BleUtils_" + name]).toInteger()
32
+ }
33
+
34
+ def supportsNamespace() {
35
+ def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
36
+ def major = parsed[0].toInteger()
37
+ def minor = parsed[1].toInteger()
38
+
39
+ // Namespace support was added in 7.3.0
40
+ return (major == 7 && minor >= 3) || major >= 8
41
+ }
42
+
43
+ android {
44
+ if (supportsNamespace()) {
45
+ namespace "so.onekey.lib.ble.utils"
46
+
47
+ sourceSets {
48
+ main {
49
+ manifest.srcFile "src/main/AndroidManifestNew.xml"
50
+ }
51
+ }
52
+ }
53
+
54
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
55
+
56
+ defaultConfig {
57
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
58
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
59
+ }
60
+
61
+ buildTypes {
62
+ release {
63
+ minifyEnabled false
64
+ }
65
+ }
66
+
67
+ lintOptions {
68
+ disable "GradleCompatible"
69
+ }
70
+
71
+ compileOptions {
72
+ sourceCompatibility JavaVersion.VERSION_1_8
73
+ targetCompatibility JavaVersion.VERSION_1_8
74
+ }
75
+ }
76
+
77
+ repositories {
78
+ mavenCentral()
79
+ google()
80
+ }
81
+
82
+ def kotlin_version = getExtOrDefault("kotlinVersion")
83
+
84
+ dependencies {
85
+ implementation "com.facebook.react:react-native"
86
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
87
+ }
88
+
@@ -0,0 +1,5 @@
1
+ BleUtils_kotlinVersion=1.9.0
2
+ BleUtils_minSdkVersion=24
3
+ BleUtils_targetSdkVersion=34
4
+ BleUtils_compileSdkVersion=34
5
+ BleUtils_ndkVersion=25.1.8937393
@@ -0,0 +1,3 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="so.onekey.lib.ble.utils">
3
+ </manifest>
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -0,0 +1,7 @@
1
+ #ifndef bleutils_Bridging_Header_h
2
+ #define bleutils_Bridging_Header_h
3
+
4
+ #import "React/RCTBridgeModule.h"
5
+ #import "React/RCTViewManager.h"
6
+
7
+ #endif /* bleutils_Bridging_Header_h */
@@ -0,0 +1,18 @@
1
+ #import "React/RCTBridgeModule.h"
2
+ #import <CoreBluetooth/CoreBluetooth.h>
3
+ #import <Foundation/Foundation.h>
4
+
5
+ @interface RCT_EXTERN_MODULE(BleUtilsModule, NSObject)
6
+
7
+
8
+ RCT_EXTERN_METHOD(getConnectedPeripherals:(NSArray *)serviceUUIDStrings
9
+ callback:(RCTResponseSenderBlock)callback)
10
+
11
+ RCT_EXTERN_METHOD(checkState:(RCTResponseSenderBlock)callback)
12
+
13
+ + (BOOL)requiresMainQueueSetup
14
+ {
15
+ return NO;
16
+ }
17
+
18
+ @end
@@ -0,0 +1,58 @@
1
+ import Foundation
2
+ import CoreBluetooth
3
+
4
+ @objc(BleUtilsModule)
5
+ class BleUtilsModule: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
6
+ private var manager: CBCentralManager?
7
+ private let serialQueue = DispatchQueue(label: "BleUtilsModule.serialQueue")
8
+
9
+ @objc
10
+ static func requiresMainQueueSetup() -> Bool {
11
+ return false
12
+ }
13
+
14
+ override init() {
15
+ super.init()
16
+ manager = CBCentralManager(delegate: self, queue: nil)
17
+ }
18
+
19
+ func centralManagerDidUpdateState(_ central: CBCentralManager) {
20
+
21
+ }
22
+
23
+ @objc public func checkState(_ callback: @escaping RCTResponseSenderBlock) {
24
+ if let manager = manager {
25
+ let stateName = Helper.centralManagerStateToString(manager.state)
26
+ callback([stateName])
27
+ }
28
+ }
29
+
30
+ @objc public func getConnectedPeripherals(_ serviceUUIDStrings: [String],
31
+ callback: @escaping RCTResponseSenderBlock) {
32
+ NSLog("Get connected peripherals")
33
+ var serviceUUIDs: [CBUUID] = []
34
+
35
+ for uuidString in serviceUUIDStrings {
36
+ serviceUUIDs.append(CBUUID(string: uuidString))
37
+ }
38
+
39
+ var connectedPeripherals: [Peripheral] = []
40
+
41
+ let connectedCBPeripherals: [CBPeripheral] = manager?.retrieveConnectedPeripherals(withServices: serviceUUIDs) ?? []
42
+
43
+ serialQueue.sync {
44
+ for ph in connectedCBPeripherals {
45
+ let peripheral = Peripheral(peripheral: ph)
46
+ connectedPeripherals.append(peripheral)
47
+ }
48
+ }
49
+
50
+ var foundedPeripherals: [[String: Any]] = []
51
+
52
+ for peripheral in connectedPeripherals {
53
+ foundedPeripherals.append(peripheral.advertisingInfo())
54
+ }
55
+
56
+ callback([NSNull(), foundedPeripherals])
57
+ }
58
+ }
@@ -0,0 +1,88 @@
1
+ import Foundation
2
+ import CoreBluetooth
3
+
4
+ class Helper {
5
+ static func centralManagerStateToString(_ state: CBManagerState) -> String {
6
+ switch state {
7
+ case .unknown:
8
+ return "unknown"
9
+ case .resetting:
10
+ return "resetting"
11
+ case .unsupported:
12
+ return "unsupported"
13
+ case .unauthorized:
14
+ return "unauthorized"
15
+ case .poweredOff:
16
+ return "off"
17
+ case .poweredOn:
18
+ return "on"
19
+ @unknown default:
20
+ return "unknown"
21
+ }
22
+ }
23
+
24
+ static func reformatAdvertisementData(_ advertisementData: [String:Any]) -> [String:Any] {
25
+ var adv = advertisementData
26
+ // Rename 'local name' key
27
+ if let localName = adv[CBAdvertisementDataLocalNameKey] {
28
+ adv.removeValue(forKey: CBAdvertisementDataLocalNameKey)
29
+ adv["localName"] = localName
30
+ }
31
+
32
+ // Rename 'isConnectable' key
33
+ if let isConnectable = adv[CBAdvertisementDataIsConnectable] {
34
+ adv.removeValue(forKey: CBAdvertisementDataIsConnectable)
35
+ adv["isConnectable"] = isConnectable
36
+ }
37
+
38
+ // Rename 'power level' key
39
+ if let powerLevel = adv[CBAdvertisementDataTxPowerLevelKey] {
40
+ adv.removeValue(forKey: CBAdvertisementDataTxPowerLevelKey)
41
+ adv["txPowerLevel"] = powerLevel
42
+ }
43
+
44
+ return adv
45
+ }
46
+ }
47
+
48
+ class Peripheral:Hashable {
49
+ var instance: CBPeripheral
50
+ var rssi: NSNumber?
51
+ var advertisementData: [String:Any]?
52
+
53
+ init(peripheral: CBPeripheral, rssi: NSNumber? = nil, advertisementData: [String:Any]? = nil) {
54
+ self.instance = peripheral
55
+ self.rssi = rssi
56
+ self.advertisementData = advertisementData
57
+ }
58
+
59
+ func setAdvertisementData(_ advertisementData: [String:Any]) {
60
+ self.advertisementData = advertisementData
61
+ }
62
+
63
+ func advertisingInfo() -> Dictionary<String, Any> {
64
+ var peripheralInfo: [String: Any] = [:]
65
+
66
+ peripheralInfo["name"] = instance.name
67
+ peripheralInfo["id"] = instance.uuidAsString()
68
+ if let adv = self.advertisementData {
69
+ peripheralInfo["advertising"] = Helper.reformatAdvertisementData(adv)
70
+ }
71
+
72
+ return peripheralInfo
73
+ }
74
+
75
+ static func == (lhs: Peripheral, rhs: Peripheral) -> Bool {
76
+ return lhs.instance.uuidAsString() == rhs.instance.uuidAsString()
77
+ }
78
+
79
+ func hash(into hasher: inout Hasher) {
80
+ hasher.combine(instance.uuidAsString())
81
+ }
82
+ }
83
+
84
+ extension CBPeripheral {
85
+ func uuidAsString() -> String {
86
+ return self.identifier.uuidString.lowercased()
87
+ }
88
+ }
package/ios/Info.plist ADDED
File without changes
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _reactNative = require("react-native");
8
+ const {
9
+ BleUtilsModule
10
+ } = _reactNative.NativeModules;
11
+ class BleUtils {
12
+ UiEventEmitter = null;
13
+ constructor() {
14
+ if (_reactNative.Platform.OS !== 'android') return;
15
+ this.UiEventEmitter = new _reactNative.NativeEventEmitter(BleUtilsModule);
16
+ }
17
+ checkState() {
18
+ return new Promise((fulfill, _) => {
19
+ BleUtilsModule.checkState(state => {
20
+ fulfill(state);
21
+ });
22
+ });
23
+ }
24
+
25
+ /**
26
+ *
27
+ * @param serviceUUIDs [optional] not used on android, optional on ios.
28
+ * @returns
29
+ */
30
+ getConnectedPeripherals(serviceUUIDs = []) {
31
+ return new Promise((fulfill, reject) => {
32
+ BleUtilsModule.getConnectedPeripherals(serviceUUIDs, (error, result) => {
33
+ if (error) {
34
+ reject(error);
35
+ } else {
36
+ if (result) {
37
+ fulfill(result);
38
+ } else {
39
+ fulfill([]);
40
+ }
41
+ }
42
+ });
43
+ });
44
+ }
45
+
46
+ /**
47
+ * [Android only]
48
+ * @returns
49
+ */
50
+ getBondedPeripherals() {
51
+ return new Promise((fulfill, reject) => {
52
+ BleUtilsModule.getBondedPeripherals((error, result) => {
53
+ if (error) {
54
+ reject(error);
55
+ } else {
56
+ if (result) {
57
+ fulfill(result);
58
+ } else {
59
+ fulfill([]);
60
+ }
61
+ }
62
+ });
63
+ });
64
+ }
65
+
66
+ /**
67
+ * [Android only]
68
+ * preState: 'BOND_NONE', state: 'BOND_BONDING' => start bonding
69
+ * preState: 'BOND_BONDING', state: 'BOND_BONDED' => bonding success
70
+ * preState: 'BOND_BONDED', state: 'BOND_NONE' => bonding failed or bonding canceled
71
+ * @param callback
72
+ */
73
+ onDeviceBondState(callback) {
74
+ if (_reactNative.Platform.OS !== 'android') return;
75
+ this.UiEventEmitter?.addListener('onDeviceBondState', callback);
76
+ return () => {
77
+ this.UiEventEmitter?.removeAllListeners('onDeviceBondState');
78
+ };
79
+ }
80
+ }
81
+ var _default = exports.default = new BleUtils();
82
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_reactNative","require","BleUtilsModule","NativeModules","BleUtils","UiEventEmitter","constructor","Platform","OS","NativeEventEmitter","checkState","Promise","fulfill","_","state","getConnectedPeripherals","serviceUUIDs","reject","error","result","getBondedPeripherals","onDeviceBondState","callback","addListener","removeAllListeners","_default","exports","default"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAGA,MAAM;EAAEC;AAAe,CAAC,GAAGC,0BAAa;AAExC,MAAMC,QAAQ,CAAC;EACbC,cAAc,GAA8B,IAAI;EAEhDC,WAAWA,CAAA,EAAG;IACZ,IAAIC,qBAAQ,CAACC,EAAE,KAAK,SAAS,EAAE;IAC/B,IAAI,CAACH,cAAc,GAAG,IAAII,+BAAkB,CAACP,cAAc,CAAC;EAC9D;EAEAQ,UAAUA,CAAA,EAAG;IACX,OAAO,IAAIC,OAAO,CAAW,CAACC,OAAO,EAAEC,CAAC,KAAK;MAC3CX,cAAc,CAACQ,UAAU,CAAEI,KAAe,IAAK;QAC7CF,OAAO,CAACE,KAAK,CAAC;MAChB,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;EACEC,uBAAuBA,CAACC,YAAsB,GAAG,EAAE,EAAE;IACnD,OAAO,IAAIL,OAAO,CAAe,CAACC,OAAO,EAAEK,MAAM,KAAK;MACpDf,cAAc,CAACa,uBAAuB,CACpCC,YAAY,EACZ,CAACE,KAAoB,EAAEC,MAA2B,KAAK;QACrD,IAAID,KAAK,EAAE;UACTD,MAAM,CAACC,KAAK,CAAC;QACf,CAAC,MAAM;UACL,IAAIC,MAAM,EAAE;YACVP,OAAO,CAACO,MAAM,CAAC;UACjB,CAAC,MAAM;YACLP,OAAO,CAAC,EAAE,CAAC;UACb;QACF;MACF,CACF,CAAC;IACH,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;EACEQ,oBAAoBA,CAAA,EAAG;IACrB,OAAO,IAAIT,OAAO,CAAe,CAACC,OAAO,EAAEK,MAAM,KAAK;MACpDf,cAAc,CAACkB,oBAAoB,CACjC,CAACF,KAAoB,EAAEC,MAA2B,KAAK;QACrD,IAAID,KAAK,EAAE;UACTD,MAAM,CAACC,KAAK,CAAC;QACf,CAAC,MAAM;UACL,IAAIC,MAAM,EAAE;YACVP,OAAO,CAACO,MAAM,CAAC;UACjB,CAAC,MAAM;YACLP,OAAO,CAAC,EAAE,CAAC;UACb;QACF;MACF,CACF,CAAC;IACH,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACES,iBAAiBA,CAACC,QAA0C,EAAE;IAC5D,IAAIf,qBAAQ,CAACC,EAAE,KAAK,SAAS,EAAE;IAC/B,IAAI,CAACH,cAAc,EAAEkB,WAAW,CAAC,mBAAmB,EAAED,QAAQ,CAAC;IAC/D,OAAO,MAAM;MACX,IAAI,CAACjB,cAAc,EAAEmB,kBAAkB,CAAC,mBAAmB,CAAC;IAC9D,CAAC;EACH;AACF;AAAC,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEc,IAAIvB,QAAQ,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.BleState = void 0;
7
+ /**
8
+ * android states: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE
9
+ * ios states: https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerstate
10
+ * */
11
+ let BleState = exports.BleState = /*#__PURE__*/function (BleState) {
12
+ /**
13
+ * [iOS only]
14
+ */
15
+ BleState["Unknown"] = "unknown";
16
+ /**
17
+ * [iOS only]
18
+ */
19
+ BleState["Resetting"] = "resetting";
20
+ BleState["Unsupported"] = "unsupported";
21
+ /**
22
+ * [iOS only]
23
+ */
24
+ BleState["Unauthorized"] = "unauthorized";
25
+ BleState["On"] = "on";
26
+ BleState["Off"] = "off";
27
+ /**
28
+ * [android only]
29
+ */
30
+ BleState["TurningOn"] = "turning_on";
31
+ /**
32
+ * [android only]
33
+ */
34
+ BleState["TurningOff"] = "turning_off";
35
+ return BleState;
36
+ }({});
37
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["BleState","exports"],"sourceRoot":"../../src","sources":["type.ts"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AAHA,IAIYA,QAAQ,GAAAC,OAAA,CAAAD,QAAA,0BAARA,QAAQ;EAClB;AACF;AACA;EAHYA,QAAQ;EAKlB;AACF;AACA;EAPYA,QAAQ;EAARA,QAAQ;EAUlB;AACF;AACA;EAZYA,QAAQ;EAARA,QAAQ;EAARA,QAAQ;EAgBlB;AACF;AACA;EAlBYA,QAAQ;EAoBlB;AACF;AACA;EAtBYA,QAAQ;EAAA,OAARA,QAAQ;AAAA","ignoreList":[]}
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
4
+ const {
5
+ BleUtilsModule
6
+ } = NativeModules;
7
+ class BleUtils {
8
+ UiEventEmitter = null;
9
+ constructor() {
10
+ if (Platform.OS !== 'android') return;
11
+ this.UiEventEmitter = new NativeEventEmitter(BleUtilsModule);
12
+ }
13
+ checkState() {
14
+ return new Promise((fulfill, _) => {
15
+ BleUtilsModule.checkState(state => {
16
+ fulfill(state);
17
+ });
18
+ });
19
+ }
20
+
21
+ /**
22
+ *
23
+ * @param serviceUUIDs [optional] not used on android, optional on ios.
24
+ * @returns
25
+ */
26
+ getConnectedPeripherals(serviceUUIDs = []) {
27
+ return new Promise((fulfill, reject) => {
28
+ BleUtilsModule.getConnectedPeripherals(serviceUUIDs, (error, result) => {
29
+ if (error) {
30
+ reject(error);
31
+ } else {
32
+ if (result) {
33
+ fulfill(result);
34
+ } else {
35
+ fulfill([]);
36
+ }
37
+ }
38
+ });
39
+ });
40
+ }
41
+
42
+ /**
43
+ * [Android only]
44
+ * @returns
45
+ */
46
+ getBondedPeripherals() {
47
+ return new Promise((fulfill, reject) => {
48
+ BleUtilsModule.getBondedPeripherals((error, result) => {
49
+ if (error) {
50
+ reject(error);
51
+ } else {
52
+ if (result) {
53
+ fulfill(result);
54
+ } else {
55
+ fulfill([]);
56
+ }
57
+ }
58
+ });
59
+ });
60
+ }
61
+
62
+ /**
63
+ * [Android only]
64
+ * preState: 'BOND_NONE', state: 'BOND_BONDING' => start bonding
65
+ * preState: 'BOND_BONDING', state: 'BOND_BONDED' => bonding success
66
+ * preState: 'BOND_BONDED', state: 'BOND_NONE' => bonding failed or bonding canceled
67
+ * @param callback
68
+ */
69
+ onDeviceBondState(callback) {
70
+ if (Platform.OS !== 'android') return;
71
+ this.UiEventEmitter?.addListener('onDeviceBondState', callback);
72
+ return () => {
73
+ this.UiEventEmitter?.removeAllListeners('onDeviceBondState');
74
+ };
75
+ }
76
+ }
77
+ export default new BleUtils();
78
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["NativeEventEmitter","NativeModules","Platform","BleUtilsModule","BleUtils","UiEventEmitter","constructor","OS","checkState","Promise","fulfill","_","state","getConnectedPeripherals","serviceUUIDs","reject","error","result","getBondedPeripherals","onDeviceBondState","callback","addListener","removeAllListeners"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA,SAASA,kBAAkB,EAAEC,aAAa,EAAEC,QAAQ,QAAQ,cAAc;AAG1E,MAAM;EAAEC;AAAe,CAAC,GAAGF,aAAa;AAExC,MAAMG,QAAQ,CAAC;EACbC,cAAc,GAA8B,IAAI;EAEhDC,WAAWA,CAAA,EAAG;IACZ,IAAIJ,QAAQ,CAACK,EAAE,KAAK,SAAS,EAAE;IAC/B,IAAI,CAACF,cAAc,GAAG,IAAIL,kBAAkB,CAACG,cAAc,CAAC;EAC9D;EAEAK,UAAUA,CAAA,EAAG;IACX,OAAO,IAAIC,OAAO,CAAW,CAACC,OAAO,EAAEC,CAAC,KAAK;MAC3CR,cAAc,CAACK,UAAU,CAAEI,KAAe,IAAK;QAC7CF,OAAO,CAACE,KAAK,CAAC;MAChB,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;EACEC,uBAAuBA,CAACC,YAAsB,GAAG,EAAE,EAAE;IACnD,OAAO,IAAIL,OAAO,CAAe,CAACC,OAAO,EAAEK,MAAM,KAAK;MACpDZ,cAAc,CAACU,uBAAuB,CACpCC,YAAY,EACZ,CAACE,KAAoB,EAAEC,MAA2B,KAAK;QACrD,IAAID,KAAK,EAAE;UACTD,MAAM,CAACC,KAAK,CAAC;QACf,CAAC,MAAM;UACL,IAAIC,MAAM,EAAE;YACVP,OAAO,CAACO,MAAM,CAAC;UACjB,CAAC,MAAM;YACLP,OAAO,CAAC,EAAE,CAAC;UACb;QACF;MACF,CACF,CAAC;IACH,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;EACEQ,oBAAoBA,CAAA,EAAG;IACrB,OAAO,IAAIT,OAAO,CAAe,CAACC,OAAO,EAAEK,MAAM,KAAK;MACpDZ,cAAc,CAACe,oBAAoB,CACjC,CAACF,KAAoB,EAAEC,MAA2B,KAAK;QACrD,IAAID,KAAK,EAAE;UACTD,MAAM,CAACC,KAAK,CAAC;QACf,CAAC,MAAM;UACL,IAAIC,MAAM,EAAE;YACVP,OAAO,CAACO,MAAM,CAAC;UACjB,CAAC,MAAM;YACLP,OAAO,CAAC,EAAE,CAAC;UACb;QACF;MACF,CACF,CAAC;IACH,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACES,iBAAiBA,CAACC,QAA0C,EAAE;IAC5D,IAAIlB,QAAQ,CAACK,EAAE,KAAK,SAAS,EAAE;IAC/B,IAAI,CAACF,cAAc,EAAEgB,WAAW,CAAC,mBAAmB,EAAED,QAAQ,CAAC;IAC/D,OAAO,MAAM;MACX,IAAI,CAACf,cAAc,EAAEiB,kBAAkB,CAAC,mBAAmB,CAAC;IAC9D,CAAC;EACH;AACF;AAEA,eAAe,IAAIlB,QAAQ,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * android states: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE
5
+ * ios states: https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerstate
6
+ * */
7
+ export let BleState = /*#__PURE__*/function (BleState) {
8
+ /**
9
+ * [iOS only]
10
+ */
11
+ BleState["Unknown"] = "unknown";
12
+ /**
13
+ * [iOS only]
14
+ */
15
+ BleState["Resetting"] = "resetting";
16
+ BleState["Unsupported"] = "unsupported";
17
+ /**
18
+ * [iOS only]
19
+ */
20
+ BleState["Unauthorized"] = "unauthorized";
21
+ BleState["On"] = "on";
22
+ BleState["Off"] = "off";
23
+ /**
24
+ * [android only]
25
+ */
26
+ BleState["TurningOn"] = "turning_on";
27
+ /**
28
+ * [android only]
29
+ */
30
+ BleState["TurningOff"] = "turning_off";
31
+ return BleState;
32
+ }({});
33
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["BleState"],"sourceRoot":"../../src","sources":["type.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA,WAAYA,QAAQ,0BAARA,QAAQ;EAClB;AACF;AACA;EAHYA,QAAQ;EAKlB;AACF;AACA;EAPYA,QAAQ;EAARA,QAAQ;EAUlB;AACF;AACA;EAZYA,QAAQ;EAARA,QAAQ;EAARA,QAAQ;EAgBlB;AACF;AACA;EAlBYA,QAAQ;EAoBlB;AACF;AACA;EAtBYA,QAAQ;EAAA,OAARA,QAAQ;AAAA","ignoreList":[]}
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,30 @@
1
+ import { NativeEventEmitter } from 'react-native';
2
+ import type { BleState, Peripheral, BondState, AdvertisingData } from './type';
3
+ declare class BleUtils {
4
+ UiEventEmitter: NativeEventEmitter | null;
5
+ constructor();
6
+ checkState(): Promise<BleState>;
7
+ /**
8
+ *
9
+ * @param serviceUUIDs [optional] not used on android, optional on ios.
10
+ * @returns
11
+ */
12
+ getConnectedPeripherals(serviceUUIDs?: string[]): Promise<Peripheral[]>;
13
+ /**
14
+ * [Android only]
15
+ * @returns
16
+ */
17
+ getBondedPeripherals(): Promise<Peripheral[]>;
18
+ /**
19
+ * [Android only]
20
+ * preState: 'BOND_NONE', state: 'BOND_BONDING' => start bonding
21
+ * preState: 'BOND_BONDING', state: 'BOND_BONDED' => bonding success
22
+ * preState: 'BOND_BONDED', state: 'BOND_NONE' => bonding failed or bonding canceled
23
+ * @param callback
24
+ */
25
+ onDeviceBondState(callback: (peripheral: Peripheral) => void): (() => void) | undefined;
26
+ }
27
+ declare const _default: BleUtils;
28
+ export default _default;
29
+ export type { BleState, Peripheral, BondState, AdvertisingData };
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA2B,MAAM,cAAc,CAAC;AAC3E,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAI/E,cAAM,QAAQ;IACZ,cAAc,EAAE,kBAAkB,GAAG,IAAI,CAAQ;;IAOjD,UAAU;IAQV;;;;OAIG;IACH,uBAAuB,CAAC,YAAY,GAAE,MAAM,EAAO;IAmBnD;;;OAGG;IACH,oBAAoB;IAkBpB;;;;;;OAMG;IACH,iBAAiB,CAAC,QAAQ,EAAE,CAAC,UAAU,EAAE,UAAU,KAAK,IAAI;CAO7D;;AAED,wBAA8B;AAC9B,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * android states: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE
3
+ * ios states: https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerstate
4
+ * */
5
+ export declare enum BleState {
6
+ /**
7
+ * [iOS only]
8
+ */
9
+ Unknown = "unknown",
10
+ /**
11
+ * [iOS only]
12
+ */
13
+ Resetting = "resetting",
14
+ Unsupported = "unsupported",
15
+ /**
16
+ * [iOS only]
17
+ */
18
+ Unauthorized = "unauthorized",
19
+ On = "on",
20
+ Off = "off",
21
+ /**
22
+ * [android only]
23
+ */
24
+ TurningOn = "turning_on",
25
+ /**
26
+ * [android only]
27
+ */
28
+ TurningOff = "turning_off"
29
+ }
30
+ export interface Peripheral {
31
+ id: string;
32
+ name?: string;
33
+ advertising: AdvertisingData;
34
+ bondState: BondState;
35
+ }
36
+ export interface BondState {
37
+ state: string;
38
+ preState: string;
39
+ }
40
+ export interface AdvertisingData {
41
+ isConnectable?: boolean;
42
+ localName?: string;
43
+ }
44
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../../src/type.ts"],"names":[],"mappings":"AAAA;;;KAGK;AACL,oBAAY,QAAQ;IAClB;;OAEG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB,WAAW,gBAAgB;IAC3B;;OAEG;IACH,YAAY,iBAAiB;IAC7B,EAAE,OAAO;IACT,GAAG,QAAQ;IACX;;OAEG;IACH,SAAS,eAAe;IACxB;;OAEG;IACH,UAAU,gBAAgB;CAC3B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,eAAe,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;CACtB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB"}
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1,30 @@
1
+ import { NativeEventEmitter } from 'react-native';
2
+ import type { BleState, Peripheral, BondState, AdvertisingData } from './type';
3
+ declare class BleUtils {
4
+ UiEventEmitter: NativeEventEmitter | null;
5
+ constructor();
6
+ checkState(): Promise<BleState>;
7
+ /**
8
+ *
9
+ * @param serviceUUIDs [optional] not used on android, optional on ios.
10
+ * @returns
11
+ */
12
+ getConnectedPeripherals(serviceUUIDs?: string[]): Promise<Peripheral[]>;
13
+ /**
14
+ * [Android only]
15
+ * @returns
16
+ */
17
+ getBondedPeripherals(): Promise<Peripheral[]>;
18
+ /**
19
+ * [Android only]
20
+ * preState: 'BOND_NONE', state: 'BOND_BONDING' => start bonding
21
+ * preState: 'BOND_BONDING', state: 'BOND_BONDED' => bonding success
22
+ * preState: 'BOND_BONDED', state: 'BOND_NONE' => bonding failed or bonding canceled
23
+ * @param callback
24
+ */
25
+ onDeviceBondState(callback: (peripheral: Peripheral) => void): (() => void) | undefined;
26
+ }
27
+ declare const _default: BleUtils;
28
+ export default _default;
29
+ export type { BleState, Peripheral, BondState, AdvertisingData };
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA2B,MAAM,cAAc,CAAC;AAC3E,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAI/E,cAAM,QAAQ;IACZ,cAAc,EAAE,kBAAkB,GAAG,IAAI,CAAQ;;IAOjD,UAAU;IAQV;;;;OAIG;IACH,uBAAuB,CAAC,YAAY,GAAE,MAAM,EAAO;IAmBnD;;;OAGG;IACH,oBAAoB;IAkBpB;;;;;;OAMG;IACH,iBAAiB,CAAC,QAAQ,EAAE,CAAC,UAAU,EAAE,UAAU,KAAK,IAAI;CAO7D;;AAED,wBAA8B;AAC9B,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * android states: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE
3
+ * ios states: https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerstate
4
+ * */
5
+ export declare enum BleState {
6
+ /**
7
+ * [iOS only]
8
+ */
9
+ Unknown = "unknown",
10
+ /**
11
+ * [iOS only]
12
+ */
13
+ Resetting = "resetting",
14
+ Unsupported = "unsupported",
15
+ /**
16
+ * [iOS only]
17
+ */
18
+ Unauthorized = "unauthorized",
19
+ On = "on",
20
+ Off = "off",
21
+ /**
22
+ * [android only]
23
+ */
24
+ TurningOn = "turning_on",
25
+ /**
26
+ * [android only]
27
+ */
28
+ TurningOff = "turning_off"
29
+ }
30
+ export interface Peripheral {
31
+ id: string;
32
+ name?: string;
33
+ advertising: AdvertisingData;
34
+ bondState: BondState;
35
+ }
36
+ export interface BondState {
37
+ state: string;
38
+ preState: string;
39
+ }
40
+ export interface AdvertisingData {
41
+ isConnectable?: boolean;
42
+ localName?: string;
43
+ }
44
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../../src/type.ts"],"names":[],"mappings":"AAAA;;;KAGK;AACL,oBAAY,QAAQ;IAClB;;OAEG;IACH,OAAO,YAAY;IACnB;;OAEG;IACH,SAAS,cAAc;IACvB,WAAW,gBAAgB;IAC3B;;OAEG;IACH,YAAY,iBAAiB;IAC7B,EAAE,OAAO;IACT,GAAG,QAAQ;IACX;;OAEG;IACH,SAAS,eAAe;IACxB;;OAEG;IACH,UAAU,gBAAgB;CAC3B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,eAAe,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;CACtB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB"}
package/package.json ADDED
@@ -0,0 +1,179 @@
1
+ {
2
+ "name": "@onekeyfe/react-native-ble-utils",
3
+ "version": "0.1.0",
4
+ "description": "ble uilts",
5
+ "source": "./src/index.tsx",
6
+ "main": "./lib/commonjs/index.js",
7
+ "module": "./lib/module/index.js",
8
+ "react-native": "src/index.ts",
9
+ "types": "./lib/typescript/module/src/index.d.ts",
10
+ "files": [
11
+ "src",
12
+ "lib",
13
+ "android",
14
+ "ios",
15
+ "cpp",
16
+ "*.podspec",
17
+ "react-native.config.js",
18
+ "!ios/build",
19
+ "!android/build",
20
+ "!android/gradle",
21
+ "!android/gradlew",
22
+ "!android/gradlew.bat",
23
+ "!android/local.properties",
24
+ "!**/__tests__",
25
+ "!**/__fixtures__",
26
+ "!**/__mocks__",
27
+ "!**/.*"
28
+ ],
29
+ "scripts": {
30
+ "example": "cd example && yarn start",
31
+ "test": "jest",
32
+ "typecheck": "tsc",
33
+ "lint": "eslint \"**/*.{js,ts,tsx}\"",
34
+ "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
35
+ "prepare": "bob build",
36
+ "release": "release-it"
37
+ },
38
+ "keywords": [
39
+ "react-native",
40
+ "ios",
41
+ "android"
42
+ ],
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git@github.com:OneKeyHQ/react-native-ble-utils.git"
46
+ },
47
+ "author": "OnekeyHQ <dev@onekey.so> (https://onekey.so)",
48
+ "license": "MIT",
49
+ "bugs": {
50
+ "url": "https://github.com/OneKeyHQ/react-native-ble-utils/issues"
51
+ },
52
+ "homepage": "https://github.com/OneKeyHQ/react-native-ble-utils#readme",
53
+ "publishConfig": {
54
+ "registry": "https://registry.npmjs.org/",
55
+ "access": "public"
56
+ },
57
+ "devDependencies": {
58
+ "@commitlint/config-conventional": "^17.0.2",
59
+ "@evilmartians/lefthook": "^1.5.0",
60
+ "@react-native/eslint-config": "^0.73.1",
61
+ "@release-it/conventional-changelog": "^9.0.2",
62
+ "@types/jest": "^29.5.5",
63
+ "@types/react": "^18.2.44",
64
+ "commitlint": "^17.0.2",
65
+ "del-cli": "^5.1.0",
66
+ "eslint": "^8.51.0",
67
+ "eslint-config-prettier": "^9.0.0",
68
+ "eslint-plugin-prettier": "^5.0.1",
69
+ "jest": "^29.7.0",
70
+ "prettier": "^3.0.3",
71
+ "react": "19.0.0",
72
+ "react-native": "0.73.7",
73
+ "react-native-builder-bob": "^0.37.0",
74
+ "release-it": "^17.10.0",
75
+ "turbo": "^1.10.7",
76
+ "typescript": "^5.2.2"
77
+ },
78
+ "resolutions": {
79
+ "@types/react": "^18.2.44"
80
+ },
81
+ "peerDependencies": {
82
+ "react": "*",
83
+ "react-native": "*"
84
+ },
85
+ "workspaces": [
86
+ "example"
87
+ ],
88
+ "packageManager": "yarn@3.6.1",
89
+ "jest": {
90
+ "preset": "react-native",
91
+ "modulePathIgnorePatterns": [
92
+ "<rootDir>/example/node_modules",
93
+ "<rootDir>/lib/"
94
+ ]
95
+ },
96
+ "commitlint": {
97
+ "extends": [
98
+ "@commitlint/config-conventional"
99
+ ]
100
+ },
101
+ "release-it": {
102
+ "git": {
103
+ "commitMessage": "chore: release ${version}",
104
+ "tagName": "v${version}"
105
+ },
106
+ "npm": {
107
+ "publish": true
108
+ },
109
+ "github": {
110
+ "release": true
111
+ },
112
+ "plugins": {
113
+ "@release-it/conventional-changelog": {
114
+ "preset": "angular"
115
+ }
116
+ }
117
+ },
118
+ "eslintConfig": {
119
+ "root": true,
120
+ "extends": [
121
+ "@react-native",
122
+ "prettier"
123
+ ],
124
+ "rules": {
125
+ "react/react-in-jsx-scope": "off",
126
+ "prettier/prettier": [
127
+ "error",
128
+ {
129
+ "quoteProps": "consistent",
130
+ "singleQuote": true,
131
+ "tabWidth": 2,
132
+ "trailingComma": "es5",
133
+ "useTabs": false
134
+ }
135
+ ]
136
+ }
137
+ },
138
+ "eslintIgnore": [
139
+ "node_modules/",
140
+ "lib/"
141
+ ],
142
+ "prettier": {
143
+ "quoteProps": "consistent",
144
+ "singleQuote": true,
145
+ "tabWidth": 2,
146
+ "trailingComma": "es5",
147
+ "useTabs": false
148
+ },
149
+ "react-native-builder-bob": {
150
+ "source": "src",
151
+ "output": "lib",
152
+ "targets": [
153
+ [
154
+ "commonjs",
155
+ {
156
+ "esm": true
157
+ }
158
+ ],
159
+ [
160
+ "module",
161
+ {
162
+ "esm": true
163
+ }
164
+ ],
165
+ [
166
+ "typescript",
167
+ {
168
+ "project": "tsconfig.build.json",
169
+ "esm": true
170
+ }
171
+ ]
172
+ ]
173
+ },
174
+ "create-react-native-library": {
175
+ "type": "legacy-module",
176
+ "languages": "kotlin-swift",
177
+ "version": "0.48.3"
178
+ }
179
+ }
package/src/index.ts ADDED
@@ -0,0 +1,85 @@
1
+ import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
2
+ import type { BleState, Peripheral, BondState, AdvertisingData } from './type';
3
+
4
+ const { BleUtilsModule } = NativeModules;
5
+
6
+ class BleUtils {
7
+ UiEventEmitter: NativeEventEmitter | null = null;
8
+
9
+ constructor() {
10
+ if (Platform.OS !== 'android') return;
11
+ this.UiEventEmitter = new NativeEventEmitter(BleUtilsModule);
12
+ }
13
+
14
+ checkState() {
15
+ return new Promise<BleState>((fulfill, _) => {
16
+ BleUtilsModule.checkState((state: BleState) => {
17
+ fulfill(state);
18
+ });
19
+ });
20
+ }
21
+
22
+ /**
23
+ *
24
+ * @param serviceUUIDs [optional] not used on android, optional on ios.
25
+ * @returns
26
+ */
27
+ getConnectedPeripherals(serviceUUIDs: string[] = []) {
28
+ return new Promise<Peripheral[]>((fulfill, reject) => {
29
+ BleUtilsModule.getConnectedPeripherals(
30
+ serviceUUIDs,
31
+ (error: string | null, result: Peripheral[] | null) => {
32
+ if (error) {
33
+ reject(error);
34
+ } else {
35
+ if (result) {
36
+ fulfill(result);
37
+ } else {
38
+ fulfill([]);
39
+ }
40
+ }
41
+ }
42
+ );
43
+ });
44
+ }
45
+
46
+ /**
47
+ * [Android only]
48
+ * @returns
49
+ */
50
+ getBondedPeripherals() {
51
+ return new Promise<Peripheral[]>((fulfill, reject) => {
52
+ BleUtilsModule.getBondedPeripherals(
53
+ (error: string | null, result: Peripheral[] | null) => {
54
+ if (error) {
55
+ reject(error);
56
+ } else {
57
+ if (result) {
58
+ fulfill(result);
59
+ } else {
60
+ fulfill([]);
61
+ }
62
+ }
63
+ }
64
+ );
65
+ });
66
+ }
67
+
68
+ /**
69
+ * [Android only]
70
+ * preState: 'BOND_NONE', state: 'BOND_BONDING' => start bonding
71
+ * preState: 'BOND_BONDING', state: 'BOND_BONDED' => bonding success
72
+ * preState: 'BOND_BONDED', state: 'BOND_NONE' => bonding failed or bonding canceled
73
+ * @param callback
74
+ */
75
+ onDeviceBondState(callback: (peripheral: Peripheral) => void) {
76
+ if (Platform.OS !== 'android') return;
77
+ this.UiEventEmitter?.addListener('onDeviceBondState', callback);
78
+ return () => {
79
+ this.UiEventEmitter?.removeAllListeners('onDeviceBondState');
80
+ };
81
+ }
82
+ }
83
+
84
+ export default new BleUtils();
85
+ export type { BleState, Peripheral, BondState, AdvertisingData };
package/src/type.ts ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * android states: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE
3
+ * ios states: https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerstate
4
+ * */
5
+ export enum BleState {
6
+ /**
7
+ * [iOS only]
8
+ */
9
+ Unknown = 'unknown',
10
+ /**
11
+ * [iOS only]
12
+ */
13
+ Resetting = 'resetting',
14
+ Unsupported = 'unsupported',
15
+ /**
16
+ * [iOS only]
17
+ */
18
+ Unauthorized = 'unauthorized',
19
+ On = 'on',
20
+ Off = 'off',
21
+ /**
22
+ * [android only]
23
+ */
24
+ TurningOn = 'turning_on',
25
+ /**
26
+ * [android only]
27
+ */
28
+ TurningOff = 'turning_off',
29
+ }
30
+
31
+ export interface Peripheral {
32
+ id: string;
33
+ name?: string;
34
+ advertising: AdvertisingData;
35
+ bondState: BondState;
36
+ }
37
+
38
+ export interface BondState {
39
+ state: string;
40
+ preState: string;
41
+ }
42
+
43
+ export interface AdvertisingData {
44
+ isConnectable?: boolean;
45
+ localName?: string;
46
+ }