munim-bluetooth 0.3.4 → 0.3.6
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/ios/HybridMunimBluetooth.swift +27 -27
- package/package.json +1 -1
|
@@ -10,7 +10,7 @@ import CoreBluetooth
|
|
|
10
10
|
import NitroModules
|
|
11
11
|
import React
|
|
12
12
|
|
|
13
|
-
class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
13
|
+
class HybridMunimBluetooth: NSObject, HybridMunimBluetoothSpec, CBPeripheralManagerDelegate, CBCentralManagerDelegate, CBPeripheralDelegate {
|
|
14
14
|
// Peripheral Manager
|
|
15
15
|
private var peripheralManager: CBPeripheralManager?
|
|
16
16
|
private var peripheralServices: [CBMutableService] = []
|
|
@@ -36,7 +36,7 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
36
36
|
|
|
37
37
|
// MARK: - Peripheral Features
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
func startAdvertising(options: AdvertisingOptions) throws {
|
|
40
40
|
guard let peripheralManager = peripheralManager,
|
|
41
41
|
peripheralManager.state == .poweredOn else {
|
|
42
42
|
throw NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Bluetooth is not powered on"])
|
|
@@ -70,7 +70,7 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
70
70
|
peripheralManager.startAdvertising(advertisingData as? [String: Any])
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
func updateAdvertisingData(advertisingData: AdvertisingDataTypes) throws {
|
|
74
74
|
guard let peripheralManager = peripheralManager,
|
|
75
75
|
peripheralManager.state == .poweredOn else {
|
|
76
76
|
throw NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Bluetooth is not powered on"])
|
|
@@ -85,18 +85,18 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
85
85
|
peripheralManager.startAdvertising(newAdvertisingData as? [String: Any])
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
func getAdvertisingData() throws -> Promise<AdvertisingDataTypes> {
|
|
89
89
|
return Promise { resolver in
|
|
90
90
|
resolver.resolve(self.currentAdvertisingData ?? AdvertisingDataTypes())
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
func stopAdvertising() throws {
|
|
95
95
|
peripheralManager?.stopAdvertising()
|
|
96
96
|
currentAdvertisingData = nil
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
func setServices(services: [GATTService]) throws {
|
|
100
100
|
peripheralServices.removeAll()
|
|
101
101
|
|
|
102
102
|
for service in services {
|
|
@@ -157,21 +157,21 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
157
157
|
|
|
158
158
|
// MARK: - Central/Manager Features
|
|
159
159
|
|
|
160
|
-
|
|
160
|
+
func isBluetoothEnabled() throws -> Promise<Bool> {
|
|
161
161
|
return Promise { resolver in
|
|
162
162
|
let isEnabled = self.centralManager?.state == .poweredOn
|
|
163
163
|
resolver.resolve(isEnabled ?? false)
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
|
|
167
|
+
func requestBluetoothPermission() throws -> Promise<Bool> {
|
|
168
168
|
return Promise { resolver in
|
|
169
169
|
// In iOS, permissions are handled by CBPeripheralManager/CBCentralManager
|
|
170
170
|
resolver.resolve(true)
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
|
|
174
|
+
func startScan(options: ScanOptions?) throws {
|
|
175
175
|
guard let centralManager = centralManager,
|
|
176
176
|
centralManager.state == .poweredOn else {
|
|
177
177
|
throw NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Bluetooth is not powered on"])
|
|
@@ -188,12 +188,12 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
188
188
|
centralManager.scanForPeripherals(withServices: nil, options: scanOptions as [String : Any])
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
func stopScan() throws {
|
|
192
192
|
centralManager?.stopScan()
|
|
193
193
|
isScanning = false
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
|
|
196
|
+
func connect(deviceId: String) throws -> Promise<Void> {
|
|
197
197
|
return Promise { resolver in
|
|
198
198
|
guard let peripheral = self.discoveredPeripherals[deviceId] else {
|
|
199
199
|
resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Device not found"]))
|
|
@@ -206,13 +206,13 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
|
|
209
|
+
func disconnect(deviceId: String) throws {
|
|
210
210
|
guard let peripheral = connectedPeripherals[deviceId] else { return }
|
|
211
211
|
centralManager?.cancelPeripheralConnection(peripheral)
|
|
212
212
|
connectedPeripherals.removeValue(forKey: deviceId)
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
-
|
|
215
|
+
func discoverServices(deviceId: String) throws -> Promise<[GATTService]> {
|
|
216
216
|
return Promise { resolver in
|
|
217
217
|
guard let peripheral = self.connectedPeripherals[deviceId] else {
|
|
218
218
|
resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Device not connected"]))
|
|
@@ -224,33 +224,33 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
-
|
|
227
|
+
func readCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws -> Promise<CharacteristicValue> {
|
|
228
228
|
return Promise { resolver in
|
|
229
229
|
resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Not implemented"]))
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
-
|
|
233
|
+
func writeCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String, value: String, writeType: WriteType?) throws -> Promise<Void> {
|
|
234
234
|
return Promise { resolver in
|
|
235
235
|
resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Not implemented"]))
|
|
236
236
|
}
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
func subscribeToCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws {
|
|
240
240
|
// Not implemented
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
-
|
|
243
|
+
func unsubscribeFromCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws {
|
|
244
244
|
// Not implemented
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
-
|
|
247
|
+
func getConnectedDevices() throws -> Promise<[String]> {
|
|
248
248
|
return Promise { resolver in
|
|
249
249
|
resolver.resolve(Array(self.connectedPeripherals.keys))
|
|
250
250
|
}
|
|
251
251
|
}
|
|
252
252
|
|
|
253
|
-
|
|
253
|
+
func readRSSI(deviceId: String) throws -> Promise<Double> {
|
|
254
254
|
return Promise { resolver in
|
|
255
255
|
guard let peripheral = self.connectedPeripherals[deviceId] else {
|
|
256
256
|
resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Device not connected"]))
|
|
@@ -262,11 +262,11 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
264
|
|
|
265
|
-
|
|
265
|
+
func addListener(eventName: String) throws {
|
|
266
266
|
// Event management
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
-
|
|
269
|
+
func removeListeners(count: Double) throws {
|
|
270
270
|
// Event management
|
|
271
271
|
}
|
|
272
272
|
|
|
@@ -305,8 +305,8 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
-
// MARK: - CBPeripheralManagerDelegate
|
|
309
|
-
extension HybridMunimBluetooth
|
|
308
|
+
// MARK: - CBPeripheralManagerDelegate Implementation
|
|
309
|
+
extension HybridMunimBluetooth {
|
|
310
310
|
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
|
|
311
311
|
// Handle state updates
|
|
312
312
|
}
|
|
@@ -328,8 +328,8 @@ extension HybridMunimBluetooth: CBPeripheralManagerDelegate {
|
|
|
328
328
|
}
|
|
329
329
|
}
|
|
330
330
|
|
|
331
|
-
// MARK: - CBCentralManagerDelegate
|
|
332
|
-
extension HybridMunimBluetooth
|
|
331
|
+
// MARK: - CBCentralManagerDelegate Implementation
|
|
332
|
+
extension HybridMunimBluetooth {
|
|
333
333
|
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
|
334
334
|
let state = central.state
|
|
335
335
|
eventEmitter?.emit("bluetoothStateChanged", ["state": state.rawValue])
|
|
@@ -371,8 +371,8 @@ extension HybridMunimBluetooth: CBCentralManagerDelegate {
|
|
|
371
371
|
}
|
|
372
372
|
}
|
|
373
373
|
|
|
374
|
-
// MARK: - CBPeripheralDelegate
|
|
375
|
-
extension HybridMunimBluetooth
|
|
374
|
+
// MARK: - CBPeripheralDelegate Implementation
|
|
375
|
+
extension HybridMunimBluetooth {
|
|
376
376
|
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
|
377
377
|
let deviceId = peripheral.identifier.uuidString
|
|
378
378
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "munim-bluetooth",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "A comprehensive React Native library for all your Bluetooth Low Energy (BLE) needs, supporting both peripheral and central roles with Expo support",
|
|
5
5
|
"main": "./lib/commonjs/index.js",
|
|
6
6
|
"module": "./lib/module/index.js",
|