# @siva7170/ble-connection

> Node.js Bluetooth SPP communication for Windows

Latest version **3.0.0** (published 2025-05-03) · ISC license · 0 weekly downloads

## Install

```sh
npm install @siva7170/ble-connection
pnpm add @siva7170/ble-connection
yarn add @siva7170/ble-connection
bun add @siva7170/ble-connection
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2025-05-03 |
| First published | 2024-03-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 4.9 MB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 1 |
| Author | Siva V |
| Maintainers | siva7170 |
| Keywords | bluetooth, serial port profile, bluetooth spp, rfcomm, windows |

## Links

- npm: https://www.npmjs.com/package/@siva7170/ble-connection
- Repository: https://github.com/siva7170/ble-connection
- Issues: https://github.com/siva7170/ble-connection/issues
- npm.io page: https://npm.io/package/@siva7170/ble-connection

## Dependencies (3)

- [nan](https://npm.io/package/nan.md) ^2.18.0
- [bindings](https://npm.io/package/bindings.md) ^1.5.0
- [node-addon-api](https://npm.io/package/node-addon-api.md) ^7.1.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2025-05-03
- 2.0.3 — 2024-07-05
- 2.0.2 — 2024-07-05
- 2.0.1 — 2024-07-05
- 2.0.0 — 2024-07-04
- 1.1.5 — 2024-06-30
- 1.1.4 — 2024-06-03
- 1.1.3 — 2024-06-03
- 1.1.2 — 2024-06-03
- 1.1.1 — 2024-06-03
- 1.1.0 — 2024-06-03
- 1.0.9 — 2024-06-03
- 1.0.7 — 2024-06-03
- 1.0.6 — 2024-06-03
- 1.0.5 — 2024-06-03
- … 5 more at https://npm.io/package/@siva7170/ble-connection/versions

## README

# Bluetooth SPP

This is a node addon api package used for connecting other bluetooth through Bluethooth SPP. IMPORTANT! It supports for Windows platform. Now, we can use it as server and client.

## Getting Started

This library is developed in C++ and support for Node.js.

### Installation

Install this package into your project by below command

```
npm install @siva7170/ble-connection
```

## Usage

Below code is sample for how to use it. Please see methods and its functionalities below sections.

```javascript
const bleConnection = require('@siva7170/ble-connection');

const bleConnInstance = new bleConnection.BLEConnection();
```

## Client Methods

### Initiate(successCallback,failureCallback)

- **successCallback**:
  - Type: `Function`

- **failureCallback**:
  - Type: `Function`

It initializes the necessary things.

```javascript
bleConnInstance.Initiate(()=>{
	console.log("Initiated!");
	// rest of the code
},()=>{
	console.log("Failed to initiate!");
});
```

### Connect(bluetooth_addr, uuid, successCallback,failureCallback)

- **bluetooth_addr**:
  - Type: `String`

- **uuid**:
  - Type: `String`

- **successCallback**:
  - Type: `Function`

- **failureCallback**:
  - Type: `Function`

This method will try to connect to the given bluetooth address and uuid from bluetooth spp server.

```javascript
// please use valid bluetooth address and UUID
bleConnInstance.Connect("00:00:00:00:00:E0","aaaaaaaa-aaaa-4444-cccc-999888999888",()=>{
	console.log("Connected!");
    // rest of the code
},()=>{
	console.log("Failed to connect!");
});
```

### SendData(data, successCallback, failureCallback) (optional)

- **data**:
  - Type: `String`

- **successCallback**:
  - Type: `Function`

- **failureCallback**:
  - Type: `Function`
  
With this method, you can send data to client

```javascript
bleConnInstance.SendData('Hi server!',(res)=>{
	console.log("Data sent: "+sData);
}, (err)=>{

});
```

### OnReceiveData(onDataRecvCallback)

- **onDataRecvCallback**:
  - Type: `Function`

This method will be triggered when the data sent from bluetooth server

```javascript
bleConnInstance.OnReceiveData((data)=>{
	console.log("Data receivedd: "+data);
});
```

## Full Example
  
Please find full example of implementation

```javascript
const bleConnection = require('@siva7170/ble-connection');

const bleConnInstance = new bleConnection.BLEConnection();

try{
    bleConnInstance.Initiate(()=>{
        console.log("Initiated!");

        bleConnInstance.Connect("00:00:00:00:00:E0","aaaaaaaa-aaaa-4444-cccc-999888999888",()=>{
            console.log("Connected!");
            let sData="Hi client";
            bleConnInstance.SendData(sData,(res)=>{
                console.log("Data sent: "+sData);
            }, (err)=>{

            });

            bleConnInstance.OnReceiveData((data)=>{
                console.log("Data receivedd: "+data);
            });
            
            sData="How are you?";
            bleConnInstance.SendData(sData,(res)=>{
                console.log("Data sent: "+sData);
            }, (err)=>{

            });
        },()=>{
            console.log("Failed to connect!");
        });
    },()=>{
        console.log("Failed to initiate!");
    });
}catch(e){
    console.error(e.toString());
}
```


# Version 2


## Client Usage

Below code is sample for how to use it. Please see methods and its functionalities below sections.

```javascript
const bleConnection = require('@siva7170/ble-connection');

const bleConnInstance = new bleConnection.BLEConnection();
```

## Methods

### SetBtInfo(bluetooth_addr, uuid, timeout_for_reconnect, no_of_attempt_to_reconnect)

- **bluetooth_addr**:
  - Type: `String`

- **uuid**:
  - Type: `String`

- **timeout_for_reconnect**:
  - Type: `Integer`

- **no_of_attempt_to_reconnect**:
  - Type: `Integer`

It sets necessary things to BLE Connection before it initialize. 

```javascript
bleConnInstance.SetBtInfo("00:00:00:00:00:E0","aaaaaaaa-aaaa-4444-cccc-999888999888",5000,0);
```

### GetStatus(statusCallback) (optional)

- **statusCallback**:
  - Type: `Function`

This method will give current state of BLE Connection when it is changed from one to another.

```javascript
bleConnInstance.GetStatus((res)=>{
  console.log("Status: "+res);
});
```

### MakeConnection(successCallback, failureCallback) 

- **successCallback**:
  - Type: `Function`

- **failureCallback**:
  - Type: `Function`
  
It will make connection to device which is defined in SetBtInfo()

```javascript
bleConnInstance.MakeConnection(()=>{
    console.log("Connected...!");
},
()=>{
    console.log("Failed to connect!");
});
```

### IsConnected(successCallback, failureCallback) (optional)

- **successCallback**:
  - Type: `Function`

- **failureCallback**:
  - Type: `Function`

This method will be triggered when connection made to Bluetooth Server.

```javascript
    bleConnInstance.IsConnected(()=>{
        console.log("Connection status: Connected!");
    },
    ()=>{
        console.log("Failed to connect!");
    });
```

### SendDataToServer(data, successCallback, failureCallback) (optional)

- **data**:
  - Type: `String`

- **successCallback**:
  - Type: `Function`

- **failureCallback**:
  - Type: `Function`
  
With this method, you can send data to client

```javascript
bleConnInstance.SendDataToServer('Hi server!',(res)=>{
	console.log("Data sent: "+sData);
}, (err)=>{

});
```

### OnReceiveDataFromServer(onDataRecvCallback)

- **onDataRecvCallback**:
  - Type: `Function`

This method will be triggered when the data sent from bluetooth server

```javascript
bleConnInstance.OnReceiveDataFromServer((data)=>{
	console.log("Data receivedd: "+data);
});
```


## Full Example
  
Please find full example of implementation

```javascript
const bleConnection = require('@siva7170/ble-connection');

const bleConnInstance = new bleConnection.BLEConnection();

try{
    bleConnInstance.SetBtInfo("00:00:00:00:00:E0","aaaaaaaa-aaaa-4444-cccc-999888999888",5000,0);

    
    bleConnInstance.GetStatus((res)=>{
        console.log("Status: "+res);
    });


    bleConnInstance.MakeConnection(()=>{
        console.log("Connected...!");
    },
    ()=>{
        console.log("Failed to connect!");
    });

    bleConnInstance.IsConnected(()=>{
        console.log("Connection status: Connected!");

       // INITIATE
      bleConnInstance.SendDataToServer("INITIATE",(res)=>{
        console.log("Initiating...!");
      }, (err)=>{

      });

      bleConnInstance.OnReceiveDataFromServer((data)=>{
        bleConnInstance.SendDataToServer("READY_SCAN",(res)=>{
          console.log("Ready scan...!");
        }, (err)=>{
                    
        });
      });
    },
    ()=>{
        console.log("Failed to connect!");
    });

}catch(e){
    console.error(e.toString());
}
```


## Server Usage

Below code is sample for how to use it. Please see methods and its functionalities below sections.

```javascript
const bleConnection = require('@siva7170/ble-connection');

const bleServerInstance = new bleConnection.BLEServer();
```

## Methods

### Initiate()

It sets necessary things to BLE Server before it initialize. 

```javascript
bleServerInstance.Initiate();
```

### StartServer(serviceName)

- **serviceName**:
  - Type: `String`

This method will start the Bluetooth SPP server.

```javascript
bleServerInstance.StartServer(serviceName);
```

### StopServer()

This method will stop the Bluetooth SPP server.

```javascript
bleServerInstance.StopServer();
```

### OnData(dataCallback) 

- **dataCallback**:
  - Type: `Function`
  
It will call the data callback when new data received from client.

```javascript
bleServerInstance.OnData((data)=>{
    console.log("Data>",data);
});
```


### SendData(data) 

- **data**:
  - Type: `String`
  
It will send data to client.

```javascript
bleServerInstance.SendData("Hello!");
```

### OnClientConnected(callback) (optional)

- **callback**:
  - Type: `Function`

This method will be triggered when connection made to Bluetooth Client.

```javascript
    bleServerInstance.OnClientConnected(()=>{
        console.log("Connected!");
    });
```

### OnClientDisconnected(callback) (optional)

- **callback**:
  - Type: `Function`

This method will be triggered when connection lost with Bluetooth Client.

```javascript
    bleServerInstance.OnClientDisconnected(()=>{
        console.log("Disconnected!");
    });
```

## Full Example
  
Please find full example of implementation

```javascript
// Please refer  "./test/server.js"
```

## TODO

- [x] Bluetooth SPP Client (Windows)
- [ ] Bluetooth SPP Client (Other platform)
- [x] Bluetooth SPP Server (Windows)
- [ ] Bluetooth SPP Server (Other platform)
- [ ] Modify all code more efficient

## Contribution

I created this package for my own usage. I welcome contribution for this package improvement.

---
_Source: https://npm.io/package/@siva7170/ble-connection · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
