react-native-sdk-ble-middleware-v2 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.
- package/LICENSE +20 -0
- package/Middleware.podspec +21 -0
- package/README.md +187 -0
- package/android/build.gradle +77 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/middleware/MiddlewareModule.kt +23 -0
- package/android/src/main/java/com/middleware/MiddlewarePackage.kt +33 -0
- package/ios/Middleware.h +5 -0
- package/ios/Middleware.mm +21 -0
- package/lib/module/NativeMiddleware.js +3 -0
- package/lib/module/NativeMiddleware.js.map +1 -0
- package/lib/module/context/BLEContext.js +390 -0
- package/lib/module/context/BLEContext.js.map +1 -0
- package/lib/module/hooks/index.js +2 -0
- package/lib/module/hooks/index.js.map +1 -0
- package/lib/module/hooks/useBLE.js +167 -0
- package/lib/module/hooks/useBLE.js.map +1 -0
- package/lib/module/index.js +12 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/services/BLEMiddleware.js +252 -0
- package/lib/module/services/BLEMiddleware.js.map +1 -0
- package/lib/module/services/MockBLEManager.js +94 -0
- package/lib/module/services/MockBLEManager.js.map +1 -0
- package/lib/module/services/index.js +2 -0
- package/lib/module/services/index.js.map +1 -0
- package/lib/module/types/index.js +44 -0
- package/lib/module/types/index.js.map +1 -0
- package/package.json +179 -0
- package/react-native.config.js +8 -0
- package/src/NativeMiddleware.ts +7 -0
- package/src/context/BLEContext.tsx +487 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useBLE.ts +190 -0
- package/src/index.tsx +21 -0
- package/src/services/BLEMiddleware.ts +291 -0
- package/src/services/MockBLEManager.ts +96 -0
- package/src/services/index.ts +1 -0
- package/src/types/index.ts +97 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ananthu07
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "Middleware"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/ananthu07/react-native-middleware.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm,cpp}"
|
|
17
|
+
s.private_header_files = "ios/**/*.h"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
install_modules_dependencies(s)
|
|
21
|
+
end
|
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# react-native-middleware
|
|
2
|
+
|
|
3
|
+
A React Native middleware library that provides a simplified, context-based API for BLE (Bluetooth Low Energy) operations. This middleware wraps `react-native-sdk-ble` and handles all the complexity of BLE communication, so your demo app only needs to install this middleware.
|
|
4
|
+
|
|
5
|
+
## ✨ Features
|
|
6
|
+
|
|
7
|
+
- 🔌 **Simplified BLE Operations** - Easy-to-use API for scanning, connecting, and managing BLE devices
|
|
8
|
+
- 📡 **Automatic Event Handling** - Built-in state management for devices, connections, and notifications
|
|
9
|
+
- 🎣 **React Hooks** - Custom hooks for seamless integration with React components
|
|
10
|
+
- 🔔 **Notification Support** - Automatic handling of characteristic notifications (FF21, FF31, FF41, FF02)
|
|
11
|
+
- � **Infusion Control** - Built-in methods for controlling infusion devices (start/stop commands)
|
|
12
|
+
- �🛡️ **Type-Safe** - Full TypeScript support with comprehensive type definitions
|
|
13
|
+
- 📱 **Cross-Platform** - Works on both Android and iOS
|
|
14
|
+
|
|
15
|
+
## 📦 Installation
|
|
16
|
+
|
|
17
|
+
### In Your Demo App
|
|
18
|
+
|
|
19
|
+
**Step 1:** Install the middleware package
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install react-native-middleware
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
or
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
yarn add react-native-middleware
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**Step 2:** Install the required peer dependency
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm install react-native-sdk-ble
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
or
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
yarn add react-native-sdk-ble
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Step 3:** Link native dependencies (if not using auto-linking)
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
npx pod-install # iOS only
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
> **Note:** `react-native-sdk-ble` is a peer dependency. You need to install it in your demo app so that the native modules are properly linked to your app's binary.
|
|
50
|
+
|
|
51
|
+
### In the Middleware Package (For Development)
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
cd react-native-middleware
|
|
55
|
+
npm install
|
|
56
|
+
npm run prepare
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 🚀 Quick Start
|
|
60
|
+
|
|
61
|
+
### 1. Wrap Your App with BLEProvider
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { BLEProvider } from 'react-native-middleware';
|
|
65
|
+
|
|
66
|
+
export default function App() {
|
|
67
|
+
return (
|
|
68
|
+
<BLEProvider>
|
|
69
|
+
<YourApp />
|
|
70
|
+
</BLEProvider>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 2. Use BLE Hooks in Your Components
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
import { useBLE } from 'react-native-middleware';
|
|
79
|
+
|
|
80
|
+
function DeviceScanner() {
|
|
81
|
+
const {
|
|
82
|
+
bluetoothEnabled,
|
|
83
|
+
isScanning,
|
|
84
|
+
discoveredDevices,
|
|
85
|
+
connectedDeviceId,
|
|
86
|
+
startScan,
|
|
87
|
+
stopScan,
|
|
88
|
+
connectToDevice,
|
|
89
|
+
} = useBLE();
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<View>
|
|
93
|
+
<Button
|
|
94
|
+
title={isScanning ? 'Stop Scan' : 'Start Scan'}
|
|
95
|
+
onPress={isScanning ? stopScan : startScan}
|
|
96
|
+
/>
|
|
97
|
+
|
|
98
|
+
{discoveredDevices.map((device) => (
|
|
99
|
+
<TouchableOpacity
|
|
100
|
+
key={device.id}
|
|
101
|
+
onPress={() => connectToDevice(device)}
|
|
102
|
+
>
|
|
103
|
+
<Text>{device.name || 'Unknown Device'}</Text>
|
|
104
|
+
</TouchableOpacity>
|
|
105
|
+
))}
|
|
106
|
+
</View>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## 📚 Documentation
|
|
112
|
+
|
|
113
|
+
- [Installation Guide](INSTALLATION.md) - Detailed installation instructions and troubleshooting
|
|
114
|
+
- [Migration Guide](MIGRATION_GUIDE.md) - Guide for migrating to peer dependency setup
|
|
115
|
+
- [Quick Start Guide](QUICK_START.md) - Step-by-step setup instructions
|
|
116
|
+
- [API Documentation](MIDDLEWARE_USAGE.md) - Complete API reference and usage examples
|
|
117
|
+
- [Infusion Control Guide](INFUSION_USAGE.md) - Guide for using infusion start/stop functions
|
|
118
|
+
- [Example App](example/) - Full working example application
|
|
119
|
+
|
|
120
|
+
## 🎯 Architecture
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
┌─────────────────┐
|
|
124
|
+
│ Demo App │ ← Only installs react-native-middleware
|
|
125
|
+
│ │
|
|
126
|
+
└────────┬────────┘
|
|
127
|
+
│ Uses
|
|
128
|
+
▼
|
|
129
|
+
┌─────────────────┐
|
|
130
|
+
│ Middleware │ ← Provides BLEProvider, hooks, and utilities
|
|
131
|
+
│ │
|
|
132
|
+
└────────┬────────┘
|
|
133
|
+
│ Wraps
|
|
134
|
+
▼
|
|
135
|
+
┌─────────────────┐
|
|
136
|
+
│ react-native- │ ← Only installed in middleware package
|
|
137
|
+
│ sdk-ble │
|
|
138
|
+
└─────────────────┘
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## 🔧 Available Hooks
|
|
142
|
+
|
|
143
|
+
### `useBLE()`
|
|
144
|
+
|
|
145
|
+
Complete BLE functionality with all state and operations, including infusion control.
|
|
146
|
+
|
|
147
|
+
### `useBLEDevices()`
|
|
148
|
+
|
|
149
|
+
Device scanning and discovery functionality.
|
|
150
|
+
|
|
151
|
+
### `useBLEConnection()`
|
|
152
|
+
|
|
153
|
+
Connection status and disconnect operation.
|
|
154
|
+
|
|
155
|
+
### `useBLENotifications()`
|
|
156
|
+
|
|
157
|
+
Access to characteristic notifications.
|
|
158
|
+
|
|
159
|
+
### `useBLEInfusion()`
|
|
160
|
+
|
|
161
|
+
Infusion control operations (enableBluetooth, startInfusion, stopInfusion).
|
|
162
|
+
|
|
163
|
+
## 📱 Supported Platforms
|
|
164
|
+
|
|
165
|
+
- ✅ iOS 13.0+
|
|
166
|
+
- ✅ Android SDK 21+
|
|
167
|
+
|
|
168
|
+
## 🛠️ Requirements
|
|
169
|
+
|
|
170
|
+
- React Native >= 0.70
|
|
171
|
+
- React >= 18.0
|
|
172
|
+
|
|
173
|
+
## 📄 License
|
|
174
|
+
|
|
175
|
+
MIT
|
|
176
|
+
|
|
177
|
+
## 🤝 Contributing
|
|
178
|
+
|
|
179
|
+
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
|
|
180
|
+
|
|
181
|
+
## 🐛 Issues
|
|
182
|
+
|
|
183
|
+
If you find a bug or have a feature request, please open an issue on [GitHub](https://github.com/ananthu07/react-native-middleware/issues).
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.getExtOrDefault = {name ->
|
|
3
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['Middleware_' + name]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
repositories {
|
|
7
|
+
google()
|
|
8
|
+
mavenCentral()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
dependencies {
|
|
12
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
13
|
+
// noinspection DifferentKotlinGradleVersion
|
|
14
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
apply plugin: "com.android.library"
|
|
20
|
+
apply plugin: "kotlin-android"
|
|
21
|
+
|
|
22
|
+
apply plugin: "com.facebook.react"
|
|
23
|
+
|
|
24
|
+
def getExtOrIntegerDefault(name) {
|
|
25
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Middleware_" + name]).toInteger()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
android {
|
|
29
|
+
namespace "com.middleware"
|
|
30
|
+
|
|
31
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
32
|
+
|
|
33
|
+
defaultConfig {
|
|
34
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
35
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
buildFeatures {
|
|
39
|
+
buildConfig true
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
buildTypes {
|
|
43
|
+
release {
|
|
44
|
+
minifyEnabled false
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
lintOptions {
|
|
49
|
+
disable "GradleCompatible"
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
compileOptions {
|
|
53
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
54
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
sourceSets {
|
|
58
|
+
main {
|
|
59
|
+
java.srcDirs += [
|
|
60
|
+
"generated/java",
|
|
61
|
+
"generated/jni"
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
repositories {
|
|
68
|
+
mavenCentral()
|
|
69
|
+
google()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
73
|
+
|
|
74
|
+
dependencies {
|
|
75
|
+
implementation "com.facebook.react:react-android"
|
|
76
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
77
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package com.middleware
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
4
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
5
|
+
|
|
6
|
+
@ReactModule(name = MiddlewareModule.NAME)
|
|
7
|
+
class MiddlewareModule(reactContext: ReactApplicationContext) :
|
|
8
|
+
NativeMiddlewareSpec(reactContext) {
|
|
9
|
+
|
|
10
|
+
override fun getName(): String {
|
|
11
|
+
return NAME
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Example method
|
|
15
|
+
// See https://reactnative.dev/docs/native-modules-android
|
|
16
|
+
override fun multiply(a: Double, b: Double): Double {
|
|
17
|
+
return a * b
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
companion object {
|
|
21
|
+
const val NAME = "Middleware"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
package com.middleware
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
import java.util.HashMap
|
|
9
|
+
|
|
10
|
+
class MiddlewarePackage : BaseReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
12
|
+
return if (name == MiddlewareModule.NAME) {
|
|
13
|
+
MiddlewareModule(reactContext)
|
|
14
|
+
} else {
|
|
15
|
+
null
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
20
|
+
return ReactModuleInfoProvider {
|
|
21
|
+
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
|
|
22
|
+
moduleInfos[MiddlewareModule.NAME] = ReactModuleInfo(
|
|
23
|
+
MiddlewareModule.NAME,
|
|
24
|
+
MiddlewareModule.NAME,
|
|
25
|
+
false, // canOverrideExistingModule
|
|
26
|
+
false, // needsEagerInit
|
|
27
|
+
false, // isCxxModule
|
|
28
|
+
true // isTurboModule
|
|
29
|
+
)
|
|
30
|
+
moduleInfos
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
package/ios/Middleware.h
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#import "Middleware.h"
|
|
2
|
+
|
|
3
|
+
@implementation Middleware
|
|
4
|
+
- (NSNumber *)multiply:(double)a b:(double)b {
|
|
5
|
+
NSNumber *result = @(a * b);
|
|
6
|
+
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
11
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
12
|
+
{
|
|
13
|
+
return std::make_shared<facebook::react::NativeMiddlewareSpecJSI>(params);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
+ (NSString *)moduleName
|
|
17
|
+
{
|
|
18
|
+
return @"Middleware";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sources":["NativeMiddleware.ts"],"sourcesContent":["import { TurboModuleRegistry, type TurboModule } from 'react-native';\n\nexport interface Spec extends TurboModule {\n multiply(a: number, b: number): number;\n}\n\nexport default TurboModuleRegistry.getEnforcing<Spec>('Middleware');\n"],"mappings":"AAAA,SAASA,mBAAmB,QAA0B,cAAc;AAMpE,eAAeA,mBAAmB,CAACC,YAAY,CAAO,YAAY,CAAC","ignoreList":[]}
|