# react-native-middleware

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-v3` and handles all the complexity of BLE communication, so your demo app only needs to install this middleware.

## ✨ Features

- 🔌 **Simplified BLE Operations** - Easy-to-use API for scanning, connecting, and managing BLE devices
- 📡 **Automatic Event Handling** - Built-in state management for devices, connections, and notifications
- 🎣 **React Hooks** - Custom hooks for seamless integration with React components
- 🔔 **Notification Support** - Automatic handling of characteristic notifications (FF21, FF31, FF41, FF02)
- � **Infusion Control** - Built-in methods for controlling infusion devices (start/stop commands)
- �🛡️ **Type-Safe** - Full TypeScript support with comprehensive type definitions
- 📱 **Cross-Platform** - Works on both Android and iOS

## 📦 Installation

### In Your Demo App

**Step 1:** Install the middleware package

```sh
npm install react-native-middleware
```

or

```sh
yarn add react-native-middleware
```

**Step 2:** Install the required peer dependency

```sh
npm install react-native-sdk-ble-v3
```

or

```sh
yarn add react-native-sdk-ble-v3
```

**Step 3:** Link native dependencies (if not using auto-linking)

```sh
npx pod-install  # iOS only
```

> **Note:** `react-native-sdk-ble-v3` 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.

### In the Middleware Package (For Development)

```sh
cd react-native-middleware
npm install
npm run prepare
```

## 🚀 Quick Start

### 1. Wrap Your App with BLEProvider

```tsx
import { BLEProvider } from 'react-native-middleware';

export default function App() {
  return (
    <BLEProvider>
      <YourApp />
    </BLEProvider>
  );
}
```

### 2. Use BLE Hooks in Your Components

```tsx
import { useBLE } from 'react-native-middleware';

function DeviceScanner() {
  const {
    bluetoothEnabled,
    isScanning,
    discoveredDevices,
    connectedDeviceId,
    startScan,
    stopScan,
    connectToDevice,
  } = useBLE();

  return (
    <View>
      <Button
        title={isScanning ? 'Stop Scan' : 'Start Scan'}
        onPress={isScanning ? stopScan : startScan}
      />

      {discoveredDevices.map((device) => (
        <TouchableOpacity
          key={device.id}
          onPress={() => connectToDevice(device)}
        >
          <Text>{device.name || 'Unknown Device'}</Text>
        </TouchableOpacity>
      ))}
    </View>
  );
}
```

## 📚 Documentation

- [Installation Guide](INSTALLATION.md) - Detailed installation instructions and troubleshooting
- [Migration Guide](MIGRATION_GUIDE.md) - Guide for migrating to peer dependency setup
- [Quick Start Guide](QUICK_START.md) - Step-by-step setup instructions
- [API Documentation](MIDDLEWARE_USAGE.md) - Complete API reference and usage examples
- [Infusion Control Guide](INFUSION_USAGE.md) - Guide for using infusion start/stop functions
- [Example App](example/) - Full working example application

## 🎯 Architecture

```
┌─────────────────┐
│   Demo App      │  ← Only installs react-native-middleware
│                 │
└────────┬────────┘
         │ Uses
         ▼
┌─────────────────┐
│   Middleware    │  ← Provides BLEProvider, hooks, and utilities
│                 │
└────────┬────────┘
         │ Wraps
         ▼
┌─────────────────┐
│ react-native-   │  ← Only installed in middleware package
│   sdk-ble       │
└─────────────────┘
```

## 🔧 Available Hooks

### `useBLE()`

Complete BLE functionality with all state and operations, including infusion control.

### `useBLEDevices()`

Device scanning and discovery functionality.

### `useBLEConnection()`

Connection status and disconnect operation.

### `useBLENotifications()`

Access to characteristic notifications.

### `useBLEInfusion()`

Infusion control operations (enableBluetooth, startInfusion, stopInfusion).

## 📱 Supported Platforms

- ✅ iOS 13.0+
- ✅ Android SDK 21+

## 🛠️ Requirements

- React Native >= 0.70
- React >= 18.0

## 📄 License

MIT

## 🤝 Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

## 🐛 Issues

If you find a bug or have a feature request, please open an issue on [GitHub](https://github.com/ananthu07/react-native-middleware/issues).

---

Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
