Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added typescript definition, corrected README #10

Merged
merged 4 commits into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
.env
coverage
*.log
package-lock.json
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const {bluetooth, destroy} = createBluetooth()
## `Bluetooth`
| Method | Description |
| --- | --- |
| `String[] adapters()` | List of available adapters |
| `Promise<String[]> adapters()` | List of available adapters |
| `Promise<Adapter> defaultAdapter()` | Get an available adapter |
| `Promise<Adapter> getAdapter(String adapter)` | Get a specific adapter (one of available in `adapters()`)|

Expand Down Expand Up @@ -148,7 +148,7 @@ const {bluetooth, destroy} = createBluetooth()
| Method | Description |
| --- | --- |
| `Promise<String[]> services()` | List of available services |
| `Promise<GattService[]> getPrimaryService(String uuid)` | Returns a specific Primary Service |
| `Promise<GattService> getPrimaryService(String uuid)` | Returns a specific Primary Service |

## `GattService`
| Method | Description |
Expand All @@ -166,7 +166,7 @@ const {bluetooth, destroy} = createBluetooth()
| `Promise<String[]> getFlags()` | Defines how the characteristic value can be used. |
| `Promise<bool> isNotifying()` | True, if notifications or indications on this characteristic are currently enabled. |
| `Promise<Buffer> readValue(Number offset = 0)` | Issues a request to read the value of the characteristic and returns the value if the operation was successful. |
| `Promise<Buffer> writeValue(Buffer buffer, Number offset = 0)` | Issues a request to write the value of the characteristic. |
| `Promise<void> writeValue(Buffer buffer, Number offset = 0)` | Issues a request to write the value of the characteristic. |
| `Promise<void> startNotifications()` | Starts a notification session from this characteristic if it supports value notifications or indications. |
| `Promise<void> stopNotifications()` | This method will cancel any previous StartNotify transaction. |
| `Promise<String> toString()` | User friendly characteristic name. |
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"author": "chrvadala",
"license": "MIT",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"files": [
"*.md",
"src",
Expand All @@ -28,7 +29,8 @@
"coverage": "jest --testPathIgnorePatterns=e2e.spec.js --coverage",
"test:e2e": "jest",
"standard": "standard",
"ci": "npm-run-all standard coverage",
"ci": "npm-run-all standard coverage typescript",
"typescript": "tsc --strict src/index.d.ts",
"build": "exit 0"
},
"dependencies": {
Expand All @@ -38,10 +40,12 @@
"@babel/plugin-proposal-class-properties": "^7.10.1",
"@babel/plugin-proposal-decorators": "^7.10.1",
"@types/jest": "^25.2.3",
"@types/node": "^14.0.13",
"dotenv": "^8.2.0",
"jest": "^26.0.1",
"npm-run-all": "^4.1.5",
"standard": "^14.3.4"
"standard": "^14.3.4",
"typescript": "^3.9.5"
},
"standard": {
"ignore": [
Expand Down
81 changes: 81 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import events = require('events');

declare namespace NodeBle {
interface GattCharacteristic extends events.EventEmitter {
getUUID(): Promise<string>;
getFlags(): Promise<string[]>;
isNotifying(): Promise<boolean>;
readValue(offset?: number): Promise<Buffer>;
writeValue(buffer: Buffer, offset?: number): Promise<void>;
startNotifications(): Promise<void>;
stopNotifications(): Promise<void>;
toString(): Promise<string>;

on(event: 'valuechanged', listener: (buffer: Buffer) => void): this;
}

interface GattService {
isPrimary(): Promise<Boolean>;
getUUID(): Promise<string>;
characteristics(): Promise<string[]>;
toString(): Promise<string>;
getCharacteristic(uuid: string): Promise<GattCharacteristic>;
}

interface GattServer {
services(): Promise<string[]>;
getPrimaryService(uuid: string): Promise<GattService>;
}

interface ConnectionState {
connected: boolean;
}

interface Device extends events.EventEmitter {
getName(): Promise<string>;
getAddress(): Promise<string>;
getAddressType(): Promise<string>;
getAlias(): Promise<string>;
getRSSI(): Promise<string>;
isPaired(): Promise<string>;
isConnected(): Promise<string>;
pair(): Promise<void>;
cancelPair(): Promise<void>;
connect(): Promise<void>;
disconnect(): Promise<void>;
gatt(): Promise<GattServer>;
toString(): Promise<string>;

on(event: 'connect', listener: (state: ConnectionState) => void): this;
on(event: 'disconnect', listener: (state: ConnectionState) => void): this;
}

interface Adapter {
getAddress(): Promise<string>;
getAddressType(): Promise<string>;
getName(): Promise<string>;
getAlias(): Promise<string>;
isPowered(): Promise<boolean>;
isDiscovering(): Promise<boolean>;
startDiscovery(): Promise<void>;
stopDiscovery(): Promise<void>;
devices(): Promise<string[]>;
getDevice(uuid: string): Promise<Device>;
waitDevice(uuid: string): Promise<Device>;
toString(): Promise<string>;
}

interface Bluetooth {
adapters(): Promise<string[]>;
defaultAdapter(): Promise<Adapter>;
getAdapter(adapter: string): Promise<Adapter>;
}

function createBluetooth(): {
destroy(): void;
bluetooth: Bluetooth;
};
}

export = NodeBle;