An open API service indexing awesome lists of open source software.

https://github.com/techplexengineer/bionic-bt-serial

capacitorjs bluetooth serial plugin
https://github.com/techplexengineer/bionic-bt-serial

Last synced: 9 days ago
JSON representation

capacitorjs bluetooth serial plugin

Awesome Lists containing this project

README

          

# bionic-bt-serial

Send data between bluetooth devices using a serial like interface

## Install

```bash
npm install bionic-bt-serial
npx cap sync
```

## API

* [`addListener(PluginEvents, ...)`](#addlistenerpluginevents)
* [`addListener('discoveryState', ...)`](#addlistenerdiscoverystate)
* [`addListener('discovered', ...)`](#addlistenerdiscovered)
* [`addListener('rawData', ...)`](#addlistenerrawdata)
* [`addListener('connected', ...)`](#addlistenerconnected)
* [`addListener('connectionFailed', ...)`](#addlistenerconnectionfailed)
* [`addListener('connectionLost', ...)`](#addlistenerconnectionlost)
* [`getBondedDevices()`](#getbondeddevices)
* [`startListening(...)`](#startlistening)
* [`stopListening()`](#stoplistening)
* [`isListening()`](#islistening)
* [`connect(...)`](#connect)
* [`isConnected(...)`](#isconnected)
* [`getConnectedDevices()`](#getconnecteddevices)
* [`disconnect(...)`](#disconnect)
* [`disconnectAll()`](#disconnectall)
* [`write(...)`](#write)
* [`isEnabled()`](#isenabled)
* [`enableAdapter()`](#enableadapter)
* [`disableAdapter()`](#disableadapter)
* [`showBluetoothSettings()`](#showbluetoothsettings)
* [`startDiscovery(...)`](#startdiscovery)
* [`cancelDiscovery()`](#canceldiscovery)
* [`setName(...)`](#setname)
* [`getName()`](#getname)
* [`setDiscoverable(...)`](#setdiscoverable)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)
* [Enums](#enums)

### addListener(PluginEvents, ...)

```typescript
addListener(eventName: PluginEvents, callback: EventCallback) => Promise
```

| Param | Type |
| --------------- | ------------------------------------------------------- |
| **`eventName`** | PluginEvents |
| **`callback`** | EventCallback |

**Returns:** Promise<PluginListenerHandle>

--------------------

### addListener('discoveryState', ...)

```typescript
addListener(eventName: 'discoveryState', callback: (result: { starting?: boolean; completed?: boolean; }) => void) => Promise
```

| Param | Type |
| --------------- | ------------------------------------------------------------------------------ |
| **`eventName`** | 'discoveryState' |
| **`callback`** | (result: { starting?: boolean; completed?: boolean; }) => void |

**Returns:** Promise<PluginListenerHandle>

--------------------

### addListener('discovered', ...)

```typescript
addListener(eventName: 'discovered', callback: (result: BTDevice) => void) => Promise
```

| Param | Type |
| --------------- | ------------------------------------------------------------------ |
| **`eventName`** | 'discovered' |
| **`callback`** | (result: BTDevice) => void |

**Returns:** Promise<PluginListenerHandle>

--------------------

### addListener('rawData', ...)

```typescript
addListener(eventName: 'rawData', callback: (result: RawDataResult) => void) => Promise
```

| Param | Type |
| --------------- | ---------------------------------------------------------------------------- |
| **`eventName`** | 'rawData' |
| **`callback`** | (result: RawDataResult) => void |

**Returns:** Promise<PluginListenerHandle>

--------------------

### addListener('connected', ...)

```typescript
addListener(eventName: 'connected', callback: (result: BTDevice) => void) => Promise
```

| Param | Type |
| --------------- | ------------------------------------------------------------------ |
| **`eventName`** | 'connected' |
| **`callback`** | (result: BTDevice) => void |

**Returns:** Promise<PluginListenerHandle>

--------------------

### addListener('connectionFailed', ...)

```typescript
addListener(eventName: 'connectionFailed', callback: (result: BTDevice) => void) => Promise
```

| Param | Type |
| --------------- | ------------------------------------------------------------------ |
| **`eventName`** | 'connectionFailed' |
| **`callback`** | (result: BTDevice) => void |

**Returns:** Promise<PluginListenerHandle>

--------------------

### addListener('connectionLost', ...)

```typescript
addListener(eventName: 'connectionLost', callback: (result: BTDevice) => void) => Promise
```

| Param | Type |
| --------------- | ------------------------------------------------------------------ |
| **`eventName`** | 'connectionLost' |
| **`callback`** | (result: BTDevice) => void |

**Returns:** Promise<PluginListenerHandle>

--------------------

### getBondedDevices()

```typescript
getBondedDevices() => Promise<{ result: BTDevice[]; }>
```

Gets a list of the bonded (paired) devices.

**Returns:** Promise<{ result: BTDevice[]; }>

--------------------

### startListening(...)

```typescript
startListening(_options: {}, callback: ListenCallback) => Promise
```

Start listening for incoming connections

| Param | Type |
| -------------- | --------------------------------------------------------- |
| **`_options`** | {} |
| **`callback`** | ListenCallback |

--------------------

### stopListening()

```typescript
stopListening() => Promise
```

Stops listening for incoming connections.

--------------------

### isListening()

```typescript
isListening() => Promise<{ result: boolean; }>
```

True if listening for an accepting incoming connections.
A device acting as a server should be listening.

**Returns:** Promise<{ result: boolean; }>

--------------------

### connect(...)

```typescript
connect(options: { macAddress: string; }) => Promise
```

Connect to another device acting as a server that is in listening mode.
Should already be paired?

| Param | Type |
| ------------- | ------------------------------------ |
| **`options`** | { macAddress: string; } |

--------------------

### isConnected(...)

```typescript
isConnected(options: { macAddress: string; }) => Promise<{ result: boolean; }>
```

True if there is an active connection to the provided macAddress false otherwise.

| Param | Type |
| ------------- | ------------------------------------ |
| **`options`** | { macAddress: string; } |

**Returns:** Promise<{ result: boolean; }>

--------------------

### getConnectedDevices()

```typescript
getConnectedDevices() => Promise<{ result: BTDevice[]; }>
```

Gets a list of the connected devices.

**Returns:** Promise<{ result: BTDevice[]; }>

--------------------

### disconnect(...)

```typescript
disconnect(options: { macAddress: string; }) => Promise<{ result: boolean; }>
```

Disconnects specified connection.

| Param | Type |
| ------------- | ------------------------------------ |
| **`options`** | { macAddress: string; } |

**Returns:** Promise<{ result: boolean; }>

--------------------

### disconnectAll()

```typescript
disconnectAll() => Promise
```

Disconnects all connections (incomming and outgoing).

--------------------

### write(...)

```typescript
write(options: { macAddress: string; data: number[]; }) => Promise<{ result: boolean; }>
```

Write data to specified macAddress

| Param | Type |
| ------------- | ---------------------------------------------------- |
| **`options`** | { macAddress: string; data: number[]; } |

**Returns:** Promise<{ result: boolean; }>

--------------------

### isEnabled()

```typescript
isEnabled() => Promise<{ result: boolean; }>
```

True if device has bluetooth enabled, false otherwise

**Returns:** Promise<{ result: boolean; }>

--------------------

### enableAdapter()

```typescript
enableAdapter() => Promise
```

Prompt the user to enable bluetooth.
Resolved if bluetooth is enabled, rejects otherwise.

--------------------

### disableAdapter()

```typescript
disableAdapter() => Promise
```

Prompt the user to enable bluetooth.
Resolved if bluetooth is enabled, rejects otherwise.

--------------------

### showBluetoothSettings()

```typescript
showBluetoothSettings() => Promise
```

Open the bluetooth settings screen for the user.

--------------------

### startDiscovery(...)

```typescript
startDiscovery(_options: {}, callback: DiscoveryCallback) => Promise
```

Starts discovery process, sends info about found devices to the callback.
Scans for about 12 seconds.

| Param | Type |
| -------------- | --------------------------------------------------------------- |
| **`_options`** | {} |
| **`callback`** | DiscoveryCallback |

--------------------

### cancelDiscovery()

```typescript
cancelDiscovery() => Promise
```

Stops any running discovery process.

--------------------

### setName(...)

```typescript
setName(options: { name: string; }) => Promise
```

Sets the name of the bluetooth adapter. Name is what paired devices will see when they connect.

| Param | Type |
| ------------- | ------------------------------ |
| **`options`** | { name: string; } |

--------------------

### getName()

```typescript
getName() => Promise<{ result: string; }>
```

Gets the name of the bluetooth adapter.

**Returns:** Promise<{ result: string; }>

--------------------

### setDiscoverable(...)

```typescript
setDiscoverable(options: { durationSec?: number; }) => Promise
```

Ensure bluetooth is enabled and the device is discoverable to remote scanners.
Default durationSec is 120 is not provided. Max is 300 seconds.

| Param | Type |
| ------------- | -------------------------------------- |
| **`options`** | { durationSec?: number; } |

--------------------

### Interfaces

#### PluginListenerHandle

| Prop | Type |
| ------------ | ----------------------------------------- |
| **`remove`** | () => Promise<void> |

### Type Aliases

#### EventCallback

(info: any, err?: any): void

#### BTDevice

{name:string, macAddress:string, class:number}

#### RawDataResult

{bytes:number[], from:BTDevice}

#### ListenCallback

(device: BTDevice, err?: any): void

#### DiscoveryCallback

(device: BTDevice, err?: any): void

### Enums

#### PluginEvents

| Members | Value |
| ---------------------- | ------------------------------- |
| **`discovered`** | "discovered" |
| **`discoveryState`** | "discoveryState" |
| **`rawData`** | "rawData" |
| **`connected`** | "connected" |
| **`connectionFailed`** | "connectionFailed" |
| **`connectionLost`** | "connectionLost" |