react-native-ble-manager 8.7.0 → 9.0.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/README.md +96 -47
- package/android/.gradle/7.4/checksums/checksums.lock +0 -0
- package/android/.gradle/7.4/dependencies-accessors/dependencies-accessors.lock +0 -0
- package/android/.gradle/7.4/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/7.4/executionHistory/executionHistory.bin +0 -0
- package/android/.gradle/7.4/executionHistory/executionHistory.lock +0 -0
- package/android/.gradle/7.4/fileChanges/last-build.bin +0 -0
- package/android/.gradle/7.4/fileHashes/fileHashes.bin +0 -0
- package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.4/fileHashes/resourceHashesCache.bin +0 -0
- package/android/.gradle/7.4/gc.properties +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
- package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
- package/android/.gradle/file-system.probe +0 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/android/.idea/.name +1 -0
- package/android/.idea/compiler.xml +6 -0
- package/android/.idea/gradle.xml +18 -0
- package/android/.idea/jarRepositories.xml +40 -0
- package/android/.idea/misc.xml +10 -0
- package/android/.idea/vcs.xml +6 -0
- package/android/build.gradle +17 -17
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
- package/android/gradle.properties +3 -1
- package/android/gradlew +234 -0
- package/android/gradlew.bat +89 -0
- package/android/local.properties +8 -0
- package/android/src/main/java/it/innove/BleManager.java +33 -17
- package/android/src/main/java/it/innove/LegacyScanManager.java +33 -28
- package/android/src/main/java/it/innove/LollipopPeripheral.java +66 -65
- package/android/src/main/java/it/innove/LollipopScanManager.java +52 -25
- package/android/src/main/java/it/innove/Peripheral.java +889 -837
- package/android/src/main/java/it/innove/ScanManager.java +24 -23
- package/dist/cjs/index.d.ts +147 -0
- package/dist/cjs/index.js +487 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types.d.ts +311 -0
- package/dist/cjs/types.js +110 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/index.d.ts +147 -0
- package/dist/esm/index.js +471 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/types.d.ts +311 -0
- package/dist/esm/types.js +107 -0
- package/dist/esm/types.js.map +1 -0
- package/ios/BleManager.m +87 -110
- package/ios/BleManager.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/package.json +31 -4
- package/src/index.ts +569 -0
- package/src/types.ts +336 -0
- package/.github/FUNDING.yml +0 -3
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- package/BleManager.js +0 -427
- package/index.d.ts +0 -168
|
@@ -3,6 +3,7 @@ package it.innove;
|
|
|
3
3
|
|
|
4
4
|
import android.bluetooth.BluetoothAdapter;
|
|
5
5
|
import android.content.Context;
|
|
6
|
+
|
|
6
7
|
import com.facebook.react.bridge.Callback;
|
|
7
8
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
8
9
|
import com.facebook.react.bridge.ReactContext;
|
|
@@ -13,27 +14,27 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
13
14
|
|
|
14
15
|
public abstract class ScanManager {
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
17
|
+
protected BluetoothAdapter bluetoothAdapter;
|
|
18
|
+
protected Context context;
|
|
19
|
+
protected ReactContext reactContext;
|
|
20
|
+
protected BleManager bleManager;
|
|
21
|
+
protected AtomicInteger scanSessionId = new AtomicInteger();
|
|
22
|
+
|
|
23
|
+
public ScanManager(ReactApplicationContext reactContext, BleManager bleManager) {
|
|
24
|
+
context = reactContext;
|
|
25
|
+
this.reactContext = reactContext;
|
|
26
|
+
this.bleManager = bleManager;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
protected BluetoothAdapter getBluetoothAdapter() {
|
|
30
|
+
if (bluetoothAdapter == null) {
|
|
31
|
+
android.bluetooth.BluetoothManager manager = (android.bluetooth.BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
|
|
32
|
+
bluetoothAdapter = manager.getAdapter();
|
|
33
|
+
}
|
|
34
|
+
return bluetoothAdapter;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public abstract void stopScan(Callback callback);
|
|
38
|
+
|
|
39
|
+
public abstract void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options, Callback callback);
|
|
39
40
|
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { BleState, ConnectionPriority, Peripheral, PeripheralInfo, ScanOptions, StartOptions } from './types';
|
|
2
|
+
export * from './types';
|
|
3
|
+
declare class BleManager {
|
|
4
|
+
constructor();
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param peripheralId
|
|
8
|
+
* @param serviceUUID
|
|
9
|
+
* @param characteristicUUID
|
|
10
|
+
* @returns data as an array of numbers (which can be converted back to a Uint8Array (ByteArray) using something like [Buffer.from()](https://github.com/feross/buffer))
|
|
11
|
+
*/
|
|
12
|
+
read(peripheralId: string, serviceUUID: string, characteristicUUID: string): Promise<number[]>;
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param peripheralId
|
|
16
|
+
* @returns a promise resolving with the updated RSSI (`number`) if it succeeds.
|
|
17
|
+
*/
|
|
18
|
+
readRSSI(peripheralId: string): Promise<number>;
|
|
19
|
+
/**
|
|
20
|
+
* [Android only]
|
|
21
|
+
* @param peripheralId
|
|
22
|
+
* @returns a promise that resolves to a boolean indicating if gatt was successfully refreshed or not.
|
|
23
|
+
*/
|
|
24
|
+
refreshCache(peripheralId: string): Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @param peripheralId
|
|
28
|
+
* @param serviceUUIDs [iOS only] optional filter of services to retrieve.
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
retrieveServices(peripheralId: string, serviceUUIDs?: string[]): Promise<PeripheralInfo>;
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* @param peripheralId
|
|
35
|
+
* @param serviceUUID
|
|
36
|
+
* @param characteristicUUID
|
|
37
|
+
* @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
|
|
38
|
+
* @param maxByteSize optional, defaults to 20
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
write(peripheralId: string, serviceUUID: string, characteristicUUID: string, data: number[], maxByteSize?: number): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param peripheralId
|
|
45
|
+
* @param serviceUUID
|
|
46
|
+
* @param characteristicUUID
|
|
47
|
+
* @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
|
|
48
|
+
* @param maxByteSize optional, defaults to 20
|
|
49
|
+
* @param queueSleepTime optional, defaults to 10. Only useful if data length is greater than maxByteSize.
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
writeWithoutResponse(peripheralId: string, serviceUUID: string, characteristicUUID: string, data: number[], maxByteSize?: number, queueSleepTime?: number): Promise<void>;
|
|
53
|
+
connect(peripheralId: string): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* [Android only]
|
|
56
|
+
* @param peripheralId
|
|
57
|
+
* @param peripheralPin optional. will be used to auto-bond if possible.
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
createBond(peripheralId: string, peripheralPin?: string | null): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* [Android only]
|
|
63
|
+
* @param peripheralId
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
removeBond(peripheralId: string): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
*
|
|
69
|
+
* @param peripheralId
|
|
70
|
+
* @param force [Android only] defaults to true.
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
disconnect(peripheralId: string, force?: boolean): Promise<void>;
|
|
74
|
+
startNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* [Android only]
|
|
77
|
+
* @param peripheralId
|
|
78
|
+
* @param serviceUUID
|
|
79
|
+
* @param characteristicUUID
|
|
80
|
+
* @param buffer
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
startNotificationUseBuffer(peripheralId: string, serviceUUID: string, characteristicUUID: string, buffer: number): Promise<void>;
|
|
84
|
+
stopNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string): Promise<void>;
|
|
85
|
+
checkState(): Promise<BleState>;
|
|
86
|
+
start(options?: StartOptions): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
*
|
|
89
|
+
* @param serviceUUIDs
|
|
90
|
+
* @param seconds amount of seconds to scan. if set to 0 or less, will scan until you call stopScan() or the OS stops the scan (background etc).
|
|
91
|
+
* @param allowDuplicates [iOS only]
|
|
92
|
+
* @param scanningOptions [Android only] optional map of properties to fine-tune scan behavior on android, see README.
|
|
93
|
+
* @returns
|
|
94
|
+
*/
|
|
95
|
+
scan(serviceUUIDs: string[], seconds: number, allowDuplicates?: boolean, scanningOptions?: ScanOptions): Promise<void>;
|
|
96
|
+
stopScan(): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* [Android only] triggers an ENABLE_REQUEST intent to the end-user to enable bluetooth.
|
|
99
|
+
* @returns
|
|
100
|
+
*/
|
|
101
|
+
enableBluetooth(): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
*
|
|
104
|
+
* @param serviceUUIDs [optional] not used on android, optional on ios.
|
|
105
|
+
* @returns
|
|
106
|
+
*/
|
|
107
|
+
getConnectedPeripherals(serviceUUIDs?: string[]): Promise<Peripheral[]>;
|
|
108
|
+
/**
|
|
109
|
+
* [Android only]
|
|
110
|
+
* @returns
|
|
111
|
+
*/
|
|
112
|
+
getBondedPeripherals(): Promise<Peripheral[]>;
|
|
113
|
+
getDiscoveredPeripherals(): Promise<Peripheral[]>;
|
|
114
|
+
/**
|
|
115
|
+
* [Android only]
|
|
116
|
+
* @param peripheralId
|
|
117
|
+
* @returns
|
|
118
|
+
*/
|
|
119
|
+
removePeripheral(peripheralId: string): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* @param peripheralId
|
|
122
|
+
* @param serviceUUIDs [optional] not used on android, optional on ios.
|
|
123
|
+
* @returns
|
|
124
|
+
*/
|
|
125
|
+
isPeripheralConnected(peripheralId: string, serviceUUIDs?: string[]): Promise<boolean>;
|
|
126
|
+
/**
|
|
127
|
+
* [Android only, API 21+]
|
|
128
|
+
* @param peripheralId
|
|
129
|
+
* @param connectionPriority
|
|
130
|
+
* @returns a promise that resolves with a boolean indicating of the connection priority was changed successfully, or rejects with an error message.
|
|
131
|
+
*/
|
|
132
|
+
requestConnectionPriority(peripheralId: string, connectionPriority: ConnectionPriority): Promise<boolean>;
|
|
133
|
+
/**
|
|
134
|
+
* [Android only, API 21+]
|
|
135
|
+
* @param peripheralId
|
|
136
|
+
* @param mtu size to be requested, in bytes.
|
|
137
|
+
* @returns a promise resolving with the negotiated MTU if it succeeded. Beware that it might not be the one requested due to device's BLE limitations on both side of the negotiation.
|
|
138
|
+
*/
|
|
139
|
+
requestMTU(peripheralId: string, mtu: number): Promise<number>;
|
|
140
|
+
/**
|
|
141
|
+
* [Android only]
|
|
142
|
+
* @param name
|
|
143
|
+
*/
|
|
144
|
+
setName(name: string): void;
|
|
145
|
+
}
|
|
146
|
+
declare const _default: BleManager;
|
|
147
|
+
export default _default;
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
const react_native_1 = require("react-native");
|
|
18
|
+
const types_1 = require("./types");
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
20
|
+
var bleManager = react_native_1.NativeModules.BleManager;
|
|
21
|
+
class BleManager {
|
|
22
|
+
constructor() {
|
|
23
|
+
this.isPeripheralConnected = this.isPeripheralConnected.bind(this);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @param peripheralId
|
|
28
|
+
* @param serviceUUID
|
|
29
|
+
* @param characteristicUUID
|
|
30
|
+
* @returns data as an array of numbers (which can be converted back to a Uint8Array (ByteArray) using something like [Buffer.from()](https://github.com/feross/buffer))
|
|
31
|
+
*/
|
|
32
|
+
read(peripheralId, serviceUUID, characteristicUUID) {
|
|
33
|
+
return new Promise((fulfill, reject) => {
|
|
34
|
+
bleManager.read(peripheralId, serviceUUID, characteristicUUID, (error, data) => {
|
|
35
|
+
if (error) {
|
|
36
|
+
reject(error);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
fulfill(data);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
*
|
|
46
|
+
* @param peripheralId
|
|
47
|
+
* @returns a promise resolving with the updated RSSI (`number`) if it succeeds.
|
|
48
|
+
*/
|
|
49
|
+
readRSSI(peripheralId) {
|
|
50
|
+
return new Promise((fulfill, reject) => {
|
|
51
|
+
bleManager.readRSSI(peripheralId, (error, rssi) => {
|
|
52
|
+
if (error) {
|
|
53
|
+
reject(error);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
fulfill(rssi);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* [Android only]
|
|
63
|
+
* @param peripheralId
|
|
64
|
+
* @returns a promise that resolves to a boolean indicating if gatt was successfully refreshed or not.
|
|
65
|
+
*/
|
|
66
|
+
refreshCache(peripheralId) {
|
|
67
|
+
return new Promise((fulfill, reject) => {
|
|
68
|
+
bleManager.refreshCache(peripheralId, (error, result) => {
|
|
69
|
+
if (error) {
|
|
70
|
+
reject(error);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
fulfill(result);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
*
|
|
80
|
+
* @param peripheralId
|
|
81
|
+
* @param serviceUUIDs [iOS only] optional filter of services to retrieve.
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
retrieveServices(peripheralId, serviceUUIDs = []) {
|
|
85
|
+
return new Promise((fulfill, reject) => {
|
|
86
|
+
bleManager.retrieveServices(peripheralId, serviceUUIDs, (error, peripheral) => {
|
|
87
|
+
if (error) {
|
|
88
|
+
reject(error);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
fulfill(peripheral);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
*
|
|
98
|
+
* @param peripheralId
|
|
99
|
+
* @param serviceUUID
|
|
100
|
+
* @param characteristicUUID
|
|
101
|
+
* @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
|
|
102
|
+
* @param maxByteSize optional, defaults to 20
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
write(peripheralId, serviceUUID, characteristicUUID, data, maxByteSize = 20) {
|
|
106
|
+
return new Promise((fulfill, reject) => {
|
|
107
|
+
bleManager.write(peripheralId, serviceUUID, characteristicUUID, data, maxByteSize, (error) => {
|
|
108
|
+
if (error) {
|
|
109
|
+
reject(error);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
fulfill();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
*
|
|
119
|
+
* @param peripheralId
|
|
120
|
+
* @param serviceUUID
|
|
121
|
+
* @param characteristicUUID
|
|
122
|
+
* @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
|
|
123
|
+
* @param maxByteSize optional, defaults to 20
|
|
124
|
+
* @param queueSleepTime optional, defaults to 10. Only useful if data length is greater than maxByteSize.
|
|
125
|
+
* @returns
|
|
126
|
+
*/
|
|
127
|
+
writeWithoutResponse(peripheralId, serviceUUID, characteristicUUID, data, maxByteSize = 20, queueSleepTime = 10) {
|
|
128
|
+
return new Promise((fulfill, reject) => {
|
|
129
|
+
bleManager.writeWithoutResponse(peripheralId, serviceUUID, characteristicUUID, data, maxByteSize, queueSleepTime, (error) => {
|
|
130
|
+
if (error) {
|
|
131
|
+
reject(error);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
fulfill();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
connect(peripheralId) {
|
|
140
|
+
return new Promise((fulfill, reject) => {
|
|
141
|
+
bleManager.connect(peripheralId, (error) => {
|
|
142
|
+
if (error) {
|
|
143
|
+
reject(error);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
fulfill();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* [Android only]
|
|
153
|
+
* @param peripheralId
|
|
154
|
+
* @param peripheralPin optional. will be used to auto-bond if possible.
|
|
155
|
+
* @returns
|
|
156
|
+
*/
|
|
157
|
+
createBond(peripheralId, peripheralPin = null) {
|
|
158
|
+
return new Promise((fulfill, reject) => {
|
|
159
|
+
bleManager.createBond(peripheralId, peripheralPin, (error) => {
|
|
160
|
+
if (error) {
|
|
161
|
+
reject(error);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
fulfill();
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* [Android only]
|
|
171
|
+
* @param peripheralId
|
|
172
|
+
* @returns
|
|
173
|
+
*/
|
|
174
|
+
removeBond(peripheralId) {
|
|
175
|
+
return new Promise((fulfill, reject) => {
|
|
176
|
+
bleManager.removeBond(peripheralId, (error) => {
|
|
177
|
+
if (error) {
|
|
178
|
+
reject(error);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
fulfill();
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
*
|
|
188
|
+
* @param peripheralId
|
|
189
|
+
* @param force [Android only] defaults to true.
|
|
190
|
+
* @returns
|
|
191
|
+
*/
|
|
192
|
+
disconnect(peripheralId, force = true) {
|
|
193
|
+
return new Promise((fulfill, reject) => {
|
|
194
|
+
bleManager.disconnect(peripheralId, force, (error) => {
|
|
195
|
+
if (error) {
|
|
196
|
+
reject(error);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
fulfill();
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
startNotification(peripheralId, serviceUUID, characteristicUUID) {
|
|
205
|
+
return new Promise((fulfill, reject) => {
|
|
206
|
+
bleManager.startNotification(peripheralId, serviceUUID, characteristicUUID, (error) => {
|
|
207
|
+
if (error) {
|
|
208
|
+
reject(error);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
fulfill();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* [Android only]
|
|
218
|
+
* @param peripheralId
|
|
219
|
+
* @param serviceUUID
|
|
220
|
+
* @param characteristicUUID
|
|
221
|
+
* @param buffer
|
|
222
|
+
* @returns
|
|
223
|
+
*/
|
|
224
|
+
startNotificationUseBuffer(peripheralId, serviceUUID, characteristicUUID, buffer) {
|
|
225
|
+
return new Promise((fulfill, reject) => {
|
|
226
|
+
bleManager.startNotificationUseBuffer(peripheralId, serviceUUID, characteristicUUID, buffer, (error) => {
|
|
227
|
+
if (error) {
|
|
228
|
+
reject(error);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
fulfill();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
stopNotification(peripheralId, serviceUUID, characteristicUUID) {
|
|
237
|
+
return new Promise((fulfill, reject) => {
|
|
238
|
+
bleManager.stopNotification(peripheralId, serviceUUID, characteristicUUID, (error) => {
|
|
239
|
+
if (error) {
|
|
240
|
+
reject(error);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
fulfill();
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
checkState() {
|
|
249
|
+
return new Promise((fulfill, _) => {
|
|
250
|
+
bleManager.checkState((state) => {
|
|
251
|
+
fulfill(state);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
start(options) {
|
|
256
|
+
return new Promise((fulfill, reject) => {
|
|
257
|
+
if (options == null) {
|
|
258
|
+
options = {};
|
|
259
|
+
}
|
|
260
|
+
bleManager.start(options, (error) => {
|
|
261
|
+
if (error) {
|
|
262
|
+
reject(error);
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
fulfill();
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
*
|
|
272
|
+
* @param serviceUUIDs
|
|
273
|
+
* @param seconds amount of seconds to scan. if set to 0 or less, will scan until you call stopScan() or the OS stops the scan (background etc).
|
|
274
|
+
* @param allowDuplicates [iOS only]
|
|
275
|
+
* @param scanningOptions [Android only] optional map of properties to fine-tune scan behavior on android, see README.
|
|
276
|
+
* @returns
|
|
277
|
+
*/
|
|
278
|
+
scan(serviceUUIDs, seconds, allowDuplicates, scanningOptions = {}) {
|
|
279
|
+
return new Promise((fulfill, reject) => {
|
|
280
|
+
if (allowDuplicates == null) {
|
|
281
|
+
allowDuplicates = false;
|
|
282
|
+
}
|
|
283
|
+
// (ANDROID) Match as many advertisement per filter as hw could allow
|
|
284
|
+
// depends on current capability and availability of the resources in hw.
|
|
285
|
+
if (scanningOptions.numberOfMatches == null) {
|
|
286
|
+
scanningOptions.numberOfMatches = types_1.BleScanMatchCount.MaxAdvertisements;
|
|
287
|
+
}
|
|
288
|
+
// (ANDROID) Defaults to MATCH_MODE_AGGRESSIVE
|
|
289
|
+
if (scanningOptions.matchMode == null) {
|
|
290
|
+
scanningOptions.matchMode = types_1.BleScanMatchMode.Aggressive;
|
|
291
|
+
}
|
|
292
|
+
// (ANDROID) Defaults to SCAN_MODE_LOW_POWER
|
|
293
|
+
if (scanningOptions.scanMode == null) {
|
|
294
|
+
scanningOptions.scanMode = types_1.BleScanMode.LowPower;
|
|
295
|
+
}
|
|
296
|
+
// (ANDROID) Defaults to CALLBACK_TYPE_ALL_MATCHES
|
|
297
|
+
// WARN: sometimes, setting a scanSetting instead of leaving it untouched might result in unexpected behaviors.
|
|
298
|
+
// https://github.com/dariuszseweryn/RxAndroidBle/issues/462
|
|
299
|
+
if (scanningOptions.callbackType == null) {
|
|
300
|
+
scanningOptions.callbackType = types_1.BleScanCallbackType.AllMatches;
|
|
301
|
+
}
|
|
302
|
+
// (ANDROID) Defaults to 0ms (report results immediately).
|
|
303
|
+
if (scanningOptions.reportDelay == null) {
|
|
304
|
+
scanningOptions.reportDelay = 0;
|
|
305
|
+
}
|
|
306
|
+
// (ANDROID) ScanFilter used to restrict search to devices with a specific advertising name.
|
|
307
|
+
// https://developer.android.com/reference/android/bluetooth/le/ScanFilter.Builder#setDeviceName(java.lang.String)
|
|
308
|
+
if (!scanningOptions.exactAdvertisingName
|
|
309
|
+
|| typeof scanningOptions.exactAdvertisingName !== 'string') {
|
|
310
|
+
delete scanningOptions.exactAdvertisingName;
|
|
311
|
+
}
|
|
312
|
+
bleManager.scan(serviceUUIDs, seconds, allowDuplicates, scanningOptions, (error) => {
|
|
313
|
+
if (error) {
|
|
314
|
+
reject(error);
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
fulfill();
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
stopScan() {
|
|
323
|
+
return new Promise((fulfill, reject) => {
|
|
324
|
+
bleManager.stopScan((error) => {
|
|
325
|
+
if (error) {
|
|
326
|
+
reject(error);
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
fulfill();
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* [Android only] triggers an ENABLE_REQUEST intent to the end-user to enable bluetooth.
|
|
336
|
+
* @returns
|
|
337
|
+
*/
|
|
338
|
+
enableBluetooth() {
|
|
339
|
+
return new Promise((fulfill, reject) => {
|
|
340
|
+
bleManager.enableBluetooth((error) => {
|
|
341
|
+
if (error) {
|
|
342
|
+
reject(error);
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
fulfill();
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
*
|
|
352
|
+
* @param serviceUUIDs [optional] not used on android, optional on ios.
|
|
353
|
+
* @returns
|
|
354
|
+
*/
|
|
355
|
+
getConnectedPeripherals(serviceUUIDs = []) {
|
|
356
|
+
return new Promise((fulfill, reject) => {
|
|
357
|
+
bleManager.getConnectedPeripherals(serviceUUIDs, (error, result) => {
|
|
358
|
+
if (error) {
|
|
359
|
+
reject(error);
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
if (result) {
|
|
363
|
+
fulfill(result);
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
fulfill([]);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* [Android only]
|
|
374
|
+
* @returns
|
|
375
|
+
*/
|
|
376
|
+
getBondedPeripherals() {
|
|
377
|
+
return new Promise((fulfill, reject) => {
|
|
378
|
+
bleManager.getBondedPeripherals((error, result) => {
|
|
379
|
+
if (error) {
|
|
380
|
+
reject(error);
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
if (result) {
|
|
384
|
+
fulfill(result);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
fulfill([]);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
getDiscoveredPeripherals() {
|
|
394
|
+
return new Promise((fulfill, reject) => {
|
|
395
|
+
bleManager.getDiscoveredPeripherals((error, result) => {
|
|
396
|
+
if (error) {
|
|
397
|
+
reject(error);
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
if (result) {
|
|
401
|
+
fulfill(result);
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
fulfill([]);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* [Android only]
|
|
412
|
+
* @param peripheralId
|
|
413
|
+
* @returns
|
|
414
|
+
*/
|
|
415
|
+
removePeripheral(peripheralId) {
|
|
416
|
+
return new Promise((fulfill, reject) => {
|
|
417
|
+
bleManager.removePeripheral(peripheralId, (error) => {
|
|
418
|
+
if (error) {
|
|
419
|
+
reject(error);
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
fulfill();
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* @param peripheralId
|
|
429
|
+
* @param serviceUUIDs [optional] not used on android, optional on ios.
|
|
430
|
+
* @returns
|
|
431
|
+
*/
|
|
432
|
+
isPeripheralConnected(peripheralId, serviceUUIDs = []) {
|
|
433
|
+
return this.getConnectedPeripherals(serviceUUIDs).then(result => {
|
|
434
|
+
if (result.find(p => p.id === peripheralId)) {
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* [Android only, API 21+]
|
|
444
|
+
* @param peripheralId
|
|
445
|
+
* @param connectionPriority
|
|
446
|
+
* @returns a promise that resolves with a boolean indicating of the connection priority was changed successfully, or rejects with an error message.
|
|
447
|
+
*/
|
|
448
|
+
requestConnectionPriority(peripheralId, connectionPriority) {
|
|
449
|
+
return new Promise((fulfill, reject) => {
|
|
450
|
+
bleManager.requestConnectionPriority(peripheralId, connectionPriority, (error, status) => {
|
|
451
|
+
if (error) {
|
|
452
|
+
reject(error);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
fulfill(status);
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* [Android only, API 21+]
|
|
462
|
+
* @param peripheralId
|
|
463
|
+
* @param mtu size to be requested, in bytes.
|
|
464
|
+
* @returns a promise resolving with the negotiated MTU if it succeeded. Beware that it might not be the one requested due to device's BLE limitations on both side of the negotiation.
|
|
465
|
+
*/
|
|
466
|
+
requestMTU(peripheralId, mtu) {
|
|
467
|
+
return new Promise((fulfill, reject) => {
|
|
468
|
+
bleManager.requestMTU(peripheralId, mtu, (error, mtu) => {
|
|
469
|
+
if (error) {
|
|
470
|
+
reject(error);
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
fulfill(mtu);
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* [Android only]
|
|
480
|
+
* @param name
|
|
481
|
+
*/
|
|
482
|
+
setName(name) {
|
|
483
|
+
bleManager.setName(name);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
exports.default = new BleManager();
|
|
487
|
+
//# sourceMappingURL=index.js.map
|