munim-bluetooth 0.3.14 → 0.3.16
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 +50 -1
- package/ios/MunimBluetoothEventEmitter.m +20 -0
- package/ios/MunimBluetoothEventEmitter.swift +38 -0
- package/lib/commonjs/index.js +55 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/index.js +53 -0
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/index.d.ts +17 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +56 -0
|
@@ -30,6 +30,52 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
30
30
|
centralManager = CBCentralManager(delegate: self, queue: nil)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
// MARK: - Event Emission
|
|
34
|
+
private func emitDeviceFound(device: CBPeripheral, advertisementData: [String: Any], rssi: NSNumber) {
|
|
35
|
+
// Build device data dictionary
|
|
36
|
+
var deviceData: [String: Any] = [
|
|
37
|
+
"id": device.identifier.uuidString,
|
|
38
|
+
"rssi": rssi.intValue
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
// Add device name if available
|
|
42
|
+
if let name = device.name {
|
|
43
|
+
deviceData["name"] = name
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Extract and add advertising data
|
|
47
|
+
if let localName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
|
|
48
|
+
deviceData["localName"] = localName
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if let serviceUUIDs = advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [CBUUID] {
|
|
52
|
+
deviceData["serviceUUIDs"] = serviceUUIDs.map { $0.uuidString }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if let manufacturerData = advertisementData[CBAdvertisementDataManufacturerDataKey] as? Data {
|
|
56
|
+
deviceData["manufacturerData"] = manufacturerData.map { String(format: "%02x", $0) }.joined()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if let txPowerLevel = advertisementData[CBAdvertisementDataTxPowerLevelKey] as? NSNumber {
|
|
60
|
+
deviceData["txPowerLevel"] = txPowerLevel.intValue
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if let isConnectable = advertisementData[CBAdvertisementDataIsConnectable] as? NSNumber {
|
|
64
|
+
deviceData["isConnectable"] = isConnectable.boolValue
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Store advertising data
|
|
68
|
+
deviceData["advertisingData"] = advertisementData
|
|
69
|
+
|
|
70
|
+
// Emit event through the event emitter
|
|
71
|
+
if let emitter = MunimBluetoothEventEmitter.shared {
|
|
72
|
+
emitter.emitDeviceFound(deviceData)
|
|
73
|
+
NSLog("[MunimBluetooth] ✅ Device found event emitted: %@", device.identifier.uuidString)
|
|
74
|
+
} else {
|
|
75
|
+
NSLog("[MunimBluetooth] ⚠️ Event emitter not initialized!")
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
33
79
|
// MARK: - Peripheral Features
|
|
34
80
|
|
|
35
81
|
func startAdvertising(options: AdvertisingOptions) throws {
|
|
@@ -335,7 +381,10 @@ extension HybridMunimBluetooth: CBCentralManagerDelegate {
|
|
|
335
381
|
let deviceId = peripheral.identifier.uuidString
|
|
336
382
|
discoveredPeripherals[deviceId] = peripheral
|
|
337
383
|
|
|
338
|
-
|
|
384
|
+
// Emit the device found event
|
|
385
|
+
emitDeviceFound(device: peripheral, advertisementData: advertisementData, rssi: RSSI)
|
|
386
|
+
|
|
387
|
+
NSLog("Bluetooth: deviceFound - %@", deviceId)
|
|
339
388
|
}
|
|
340
389
|
|
|
341
390
|
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//
|
|
2
|
+
// MunimBluetoothEventEmitter.m
|
|
3
|
+
// munim-bluetooth
|
|
4
|
+
//
|
|
5
|
+
// Objective-C bridge for event emitter
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#import <React/RCTBridgeModule.h>
|
|
9
|
+
#import <React/RCTEventEmitter.h>
|
|
10
|
+
|
|
11
|
+
@interface RCT_EXTERN_MODULE(MunimBluetoothEventEmitter, RCTEventEmitter)
|
|
12
|
+
|
|
13
|
+
RCT_EXTERN_METHOD(supportedEvents)
|
|
14
|
+
|
|
15
|
+
+ (BOOL)requiresMainQueueSetup
|
|
16
|
+
{
|
|
17
|
+
return NO;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//
|
|
2
|
+
// MunimBluetoothEventEmitter.swift
|
|
3
|
+
// munim-bluetooth
|
|
4
|
+
//
|
|
5
|
+
// Event emitter for Bluetooth events
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import React
|
|
10
|
+
|
|
11
|
+
@objc(MunimBluetoothEventEmitter)
|
|
12
|
+
class MunimBluetoothEventEmitter: RCTEventEmitter {
|
|
13
|
+
|
|
14
|
+
public static var shared: MunimBluetoothEventEmitter?
|
|
15
|
+
|
|
16
|
+
override init() {
|
|
17
|
+
super.init()
|
|
18
|
+
MunimBluetoothEventEmitter.shared = self
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
override func supportedEvents() -> [String]! {
|
|
22
|
+
return [
|
|
23
|
+
"deviceFound",
|
|
24
|
+
"onDeviceFound",
|
|
25
|
+
"scanResult",
|
|
26
|
+
"connectionStateChanged",
|
|
27
|
+
"characteristicValueChanged"
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
override static func requiresMainQueueSetup() -> Bool {
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
func emitDeviceFound(_ deviceData: [String: Any]) {
|
|
36
|
+
sendEvent(withName: "deviceFound", body: deviceData)
|
|
37
|
+
}
|
|
38
|
+
}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.addDeviceFoundListener = addDeviceFoundListener;
|
|
7
|
+
exports.addEventListener = addEventListener;
|
|
6
8
|
exports.addListener = addListener;
|
|
7
9
|
exports.connect = connect;
|
|
8
10
|
exports.default = void 0;
|
|
@@ -25,8 +27,28 @@ exports.unsubscribeFromCharacteristic = unsubscribeFromCharacteristic;
|
|
|
25
27
|
exports.updateAdvertisingData = updateAdvertisingData;
|
|
26
28
|
exports.writeCharacteristic = writeCharacteristic;
|
|
27
29
|
var _reactNativeNitroModules = require("react-native-nitro-modules");
|
|
30
|
+
var _reactNative = require("react-native");
|
|
28
31
|
const MunimBluetooth = _reactNativeNitroModules.NitroModules.createHybridObject('MunimBluetooth');
|
|
29
32
|
|
|
33
|
+
// Event Emitter for Bluetooth events
|
|
34
|
+
const {
|
|
35
|
+
MunimBluetoothEventEmitter
|
|
36
|
+
} = _reactNative.NativeModules;
|
|
37
|
+
console.log('[munim-bluetooth] Checking for event emitter...', MunimBluetoothEventEmitter ? 'FOUND' : 'NOT FOUND');
|
|
38
|
+
console.log('[munim-bluetooth] Available NativeModules:', Object.keys(_reactNative.NativeModules).filter(key => key.includes('Bluetooth') || key.includes('Munim')));
|
|
39
|
+
let eventEmitter = null;
|
|
40
|
+
if (MunimBluetoothEventEmitter) {
|
|
41
|
+
try {
|
|
42
|
+
eventEmitter = new _reactNative.NativeEventEmitter(MunimBluetoothEventEmitter);
|
|
43
|
+
console.log('[munim-bluetooth] Event emitter initialized successfully');
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error('[munim-bluetooth] Failed to initialize event emitter:', error);
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
console.warn('[munim-bluetooth] Event emitter module not found in NativeModules - device discovery events will not work');
|
|
49
|
+
console.warn('[munim-bluetooth] This usually means the native module was not linked properly or needs a rebuild');
|
|
50
|
+
}
|
|
51
|
+
|
|
30
52
|
// ========== Peripheral Features ==========
|
|
31
53
|
|
|
32
54
|
/**
|
|
@@ -206,10 +228,41 @@ function readRSSI(deviceId) {
|
|
|
206
228
|
|
|
207
229
|
// ========== Event Management ==========
|
|
208
230
|
|
|
231
|
+
/**
|
|
232
|
+
* Add a device found event listener (for scanning).
|
|
233
|
+
*
|
|
234
|
+
* @param callback - Function to call when a device is found
|
|
235
|
+
* @returns A function to remove the listener
|
|
236
|
+
*/
|
|
237
|
+
function addDeviceFoundListener(callback) {
|
|
238
|
+
if (!eventEmitter) {
|
|
239
|
+
console.warn('[munim-bluetooth] Cannot add listener - event emitter not available');
|
|
240
|
+
return () => {};
|
|
241
|
+
}
|
|
242
|
+
const subscription = eventEmitter.addListener('deviceFound', callback);
|
|
243
|
+
return () => subscription.remove();
|
|
244
|
+
}
|
|
245
|
+
|
|
209
246
|
/**
|
|
210
247
|
* Add an event listener.
|
|
211
248
|
*
|
|
212
249
|
* @param eventName - The name of the event to listen for.
|
|
250
|
+
* @param callback - The callback to invoke when the event occurs.
|
|
251
|
+
* @returns A function to remove the listener
|
|
252
|
+
*/
|
|
253
|
+
function addEventListener(eventName, callback) {
|
|
254
|
+
if (!eventEmitter) {
|
|
255
|
+
console.warn('[munim-bluetooth] Cannot add listener - event emitter not available');
|
|
256
|
+
return () => {};
|
|
257
|
+
}
|
|
258
|
+
const subscription = eventEmitter.addListener(eventName, callback);
|
|
259
|
+
return () => subscription.remove();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Add an event listener (legacy method).
|
|
264
|
+
*
|
|
265
|
+
* @param eventName - The name of the event to listen for.
|
|
213
266
|
*/
|
|
214
267
|
function addListener(eventName) {
|
|
215
268
|
return MunimBluetooth.addListener(eventName);
|
|
@@ -248,6 +301,8 @@ var _default = exports.default = {
|
|
|
248
301
|
getConnectedDevices,
|
|
249
302
|
readRSSI,
|
|
250
303
|
// Events
|
|
304
|
+
addDeviceFoundListener,
|
|
305
|
+
addEventListener,
|
|
251
306
|
addListener,
|
|
252
307
|
removeListeners
|
|
253
308
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_reactNativeNitroModules","require","MunimBluetooth","NitroModules","createHybridObject","startAdvertising","options","updateAdvertisingData","advertisingData","getAdvertisingData","stopAdvertising","setServices","services","isBluetoothEnabled","requestBluetoothPermission","startScan","stopScan","connect","deviceId","disconnect","discoverServices","readCharacteristic","serviceUUID","characteristicUUID","writeCharacteristic","value","writeType","subscribeToCharacteristic","unsubscribeFromCharacteristic","getConnectedDevices","readRSSI","addListener","eventName","removeListeners","count","_default","exports","default"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"names":["_reactNativeNitroModules","require","_reactNative","MunimBluetooth","NitroModules","createHybridObject","MunimBluetoothEventEmitter","NativeModules","console","log","Object","keys","filter","key","includes","eventEmitter","NativeEventEmitter","error","warn","startAdvertising","options","updateAdvertisingData","advertisingData","getAdvertisingData","stopAdvertising","setServices","services","isBluetoothEnabled","requestBluetoothPermission","startScan","stopScan","connect","deviceId","disconnect","discoverServices","readCharacteristic","serviceUUID","characteristicUUID","writeCharacteristic","value","writeType","subscribeToCharacteristic","unsubscribeFromCharacteristic","getConnectedDevices","readRSSI","addDeviceFoundListener","callback","subscription","addListener","remove","addEventListener","eventName","removeListeners","count","_default","exports","default"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,wBAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AAUA,MAAME,cAAc,GAClBC,qCAAY,CAACC,kBAAkB,CAAqB,gBAAgB,CAAC;;AAEvE;AACA,MAAM;EAAEC;AAA2B,CAAC,GAAGC,0BAAa;AAEpDC,OAAO,CAACC,GAAG,CAAC,iDAAiD,EAAEH,0BAA0B,GAAG,OAA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AAC;EACjB;EAEA,MAAM6B,YAAY,GAAGhC,YAAY,CAACiC,WAAW,CAACG,SAAS,EAAEL,QAAQ,CAAC;EAClE,OAAO,MAAMC,YAAY,CAACE,MAAM,CAAC,CAAC;AACpC;;AAEA;AACA;AACA;AACA;AACA;AACO,SAASD,WAAWA,CAACG,SAAiB,EAAQ;EACnD,OAAOhD,cAAc,CAAC6C,WAAW,CAACG,SAAS,CAAC;AAC9C;;AAEA;AACA;AACA;AACA;AACA;AACO,SAASC,eAAeA,CAACC,KAAa,EAAQ;EACnD,OAAOlD,cAAc,CAACiD,eAAe,CAACC,KAAK,CAAC;AAC9C;;AAEA;AAUA;AAAA,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GACe;EACb;EACArC,gBAAgB;EAChBK,eAAe;EACfH,qBAAqB;EACrBE,kBAAkB;EAClBE,WAAW;EACX;EACAE,kBAAkB;EAClBC,0BAA0B;EAC1BC,SAAS;EACTC,QAAQ;EACRC,OAAO;EACPE,UAAU;EACVC,gBAAgB;EAChBC,kBAAkB;EAClBG,mBAAmB;EACnBG,yBAAyB;EACzBC,6BAA6B;EAC7BC,mBAAmB;EACnBC,QAAQ;EACR;EACAC,sBAAsB;EACtBK,gBAAgB;EAChBF,WAAW;EACXI;AACF,CAAC","ignoreList":[]}
|
package/lib/module/index.js
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
4
|
+
import { NativeEventEmitter, NativeModules } from 'react-native';
|
|
4
5
|
const MunimBluetooth = NitroModules.createHybridObject('MunimBluetooth');
|
|
5
6
|
|
|
7
|
+
// Event Emitter for Bluetooth events
|
|
8
|
+
const {
|
|
9
|
+
MunimBluetoothEventEmitter
|
|
10
|
+
} = NativeModules;
|
|
11
|
+
console.log('[munim-bluetooth] Checking for event emitter...', MunimBluetoothEventEmitter ? 'FOUND' : 'NOT FOUND');
|
|
12
|
+
console.log('[munim-bluetooth] Available NativeModules:', Object.keys(NativeModules).filter(key => key.includes('Bluetooth') || key.includes('Munim')));
|
|
13
|
+
let eventEmitter = null;
|
|
14
|
+
if (MunimBluetoothEventEmitter) {
|
|
15
|
+
try {
|
|
16
|
+
eventEmitter = new NativeEventEmitter(MunimBluetoothEventEmitter);
|
|
17
|
+
console.log('[munim-bluetooth] Event emitter initialized successfully');
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error('[munim-bluetooth] Failed to initialize event emitter:', error);
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
console.warn('[munim-bluetooth] Event emitter module not found in NativeModules - device discovery events will not work');
|
|
23
|
+
console.warn('[munim-bluetooth] This usually means the native module was not linked properly or needs a rebuild');
|
|
24
|
+
}
|
|
25
|
+
|
|
6
26
|
// ========== Peripheral Features ==========
|
|
7
27
|
|
|
8
28
|
/**
|
|
@@ -182,10 +202,41 @@ export function readRSSI(deviceId) {
|
|
|
182
202
|
|
|
183
203
|
// ========== Event Management ==========
|
|
184
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Add a device found event listener (for scanning).
|
|
207
|
+
*
|
|
208
|
+
* @param callback - Function to call when a device is found
|
|
209
|
+
* @returns A function to remove the listener
|
|
210
|
+
*/
|
|
211
|
+
export function addDeviceFoundListener(callback) {
|
|
212
|
+
if (!eventEmitter) {
|
|
213
|
+
console.warn('[munim-bluetooth] Cannot add listener - event emitter not available');
|
|
214
|
+
return () => {};
|
|
215
|
+
}
|
|
216
|
+
const subscription = eventEmitter.addListener('deviceFound', callback);
|
|
217
|
+
return () => subscription.remove();
|
|
218
|
+
}
|
|
219
|
+
|
|
185
220
|
/**
|
|
186
221
|
* Add an event listener.
|
|
187
222
|
*
|
|
188
223
|
* @param eventName - The name of the event to listen for.
|
|
224
|
+
* @param callback - The callback to invoke when the event occurs.
|
|
225
|
+
* @returns A function to remove the listener
|
|
226
|
+
*/
|
|
227
|
+
export function addEventListener(eventName, callback) {
|
|
228
|
+
if (!eventEmitter) {
|
|
229
|
+
console.warn('[munim-bluetooth] Cannot add listener - event emitter not available');
|
|
230
|
+
return () => {};
|
|
231
|
+
}
|
|
232
|
+
const subscription = eventEmitter.addListener(eventName, callback);
|
|
233
|
+
return () => subscription.remove();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Add an event listener (legacy method).
|
|
238
|
+
*
|
|
239
|
+
* @param eventName - The name of the event to listen for.
|
|
189
240
|
*/
|
|
190
241
|
export function addListener(eventName) {
|
|
191
242
|
return MunimBluetooth.addListener(eventName);
|
|
@@ -225,6 +276,8 @@ export default {
|
|
|
225
276
|
getConnectedDevices,
|
|
226
277
|
readRSSI,
|
|
227
278
|
// Events
|
|
279
|
+
addDeviceFoundListener,
|
|
280
|
+
addEventListener,
|
|
228
281
|
addListener,
|
|
229
282
|
removeListeners
|
|
230
283
|
};
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NitroModules","MunimBluetooth","createHybridObject","startAdvertising","options","updateAdvertisingData","advertisingData","getAdvertisingData","stopAdvertising","setServices","services","isBluetoothEnabled","requestBluetoothPermission","startScan","stopScan","connect","deviceId","disconnect","discoverServices","readCharacteristic","serviceUUID","characteristicUUID","writeCharacteristic","value","writeType","subscribeToCharacteristic","unsubscribeFromCharacteristic","getConnectedDevices","readRSSI","addListener","eventName","removeListeners","count"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,4BAA4B;
|
|
1
|
+
{"version":3,"names":["NitroModules","NativeEventEmitter","NativeModules","MunimBluetooth","createHybridObject","MunimBluetoothEventEmitter","console","log","Object","keys","filter","key","includes","eventEmitter","error","warn","startAdvertising","options","updateAdvertisingData","advertisingData","getAdvertisingData","stopAdvertising","setServices","services","isBluetoothEnabled","requestBluetoothPermission","startScan","stopScan","connect","deviceId","disconnect","discoverServices","readCharacteristic","serviceUUID","characteristicUUID","writeCharacteristic","value","writeType","subscribeToCharacteristic","unsubscribeFromCharacteristic","getConnectedDevices","readRSSI","addDeviceFoundListener","callback","subscription","addListener","remove","addEventListener","eventName","removeListeners","count"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,4BAA4B;AACzD,SAASC,kBAAkB,EAAEC,aAAa,QAAQ,cAAc;AAUhE,MAAMC,cAAc,GAClBH,YAAY,CAACI,kBAAkB,CAAqB,gBAAgB,CAAC;;AAEvE;AACA,MAAM;EAAEC;AAA2B,CAAC,GAAGH,aAAa;AAEpDI,OAAO,CAACC,GAAG,CAAC,iDAAiD,EAAEF,0BAA0B,GAAG,OAAO,GAAG,WAAW,CAAC;AAClHC,OAAO,CAACC,GAAG,CAAC,4CAA4C,EAAEC,MAAM,CAACC,IAAI,CAACP,aAAa,CAAC,CAACQ,MAAM,CAACC,GAAG,IAAIA,GAAG,CAACC,QAAQ,CAAC,WAAW,CAAC,IAAID,GAAG,CAACC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;AAEvJ,IAAIC,YAAuC,GAAG,IAAI;AAElD,IAAIR,0BAA0B,EAAE;EAC9B,IAAI;IACFQ,YAAY,GAAG,IAAIZ,kBAAkB,CAACI,0BAA0B,CAAC;IACjEC,OAAO,CAACC,GAAG,CAAC,0DAA0D,CAAC;EACzE,CAAC,CAAC,OAAOO,KAAK,EAAE;IACdR,OAAO,CAACQ,KAAK,CAAC,uDAAuD,EAAEA,KAAK,CAAC;EAC/E;AACF,CAAC,MAAM;EACLR,OAAO,CAACS,IAAI,CAAC,2GAA2G,CAAC;EACzHT,OAAO,CAACS,IAAI,CAAC,mGAAmG,CAAC;AACnH;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAACC,OAKhC,EAAQ;EACP,OAAOd,cAAc,CAACa,gBAAgB,CAACC,OAAO,CAAC;AACjD;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,qBAAqBA,CACnCC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EACR;EACAC,sBAAsB;EACtBK,gBAAgB;EAChBF,WAAW;EACXI;AACF,CAAC","ignoreList":[]}
|
|
@@ -123,10 +123,25 @@ export declare function getConnectedDevices(): Promise<string[]>;
|
|
|
123
123
|
* @returns Promise resolving to RSSI value in dBm.
|
|
124
124
|
*/
|
|
125
125
|
export declare function readRSSI(deviceId: string): Promise<number>;
|
|
126
|
+
/**
|
|
127
|
+
* Add a device found event listener (for scanning).
|
|
128
|
+
*
|
|
129
|
+
* @param callback - Function to call when a device is found
|
|
130
|
+
* @returns A function to remove the listener
|
|
131
|
+
*/
|
|
132
|
+
export declare function addDeviceFoundListener(callback: (device: BLEDevice) => void): () => void;
|
|
126
133
|
/**
|
|
127
134
|
* Add an event listener.
|
|
128
135
|
*
|
|
129
136
|
* @param eventName - The name of the event to listen for.
|
|
137
|
+
* @param callback - The callback to invoke when the event occurs.
|
|
138
|
+
* @returns A function to remove the listener
|
|
139
|
+
*/
|
|
140
|
+
export declare function addEventListener(eventName: string, callback: (data: any) => void): () => void;
|
|
141
|
+
/**
|
|
142
|
+
* Add an event listener (legacy method).
|
|
143
|
+
*
|
|
144
|
+
* @param eventName - The name of the event to listen for.
|
|
130
145
|
*/
|
|
131
146
|
export declare function addListener(eventName: string): void;
|
|
132
147
|
/**
|
|
@@ -155,6 +170,8 @@ declare const _default: {
|
|
|
155
170
|
unsubscribeFromCharacteristic: typeof unsubscribeFromCharacteristic;
|
|
156
171
|
getConnectedDevices: typeof getConnectedDevices;
|
|
157
172
|
readRSSI: typeof readRSSI;
|
|
173
|
+
addDeviceFoundListener: typeof addDeviceFoundListener;
|
|
174
|
+
addEventListener: typeof addEventListener;
|
|
158
175
|
addListener: typeof addListener;
|
|
159
176
|
removeListeners: typeof removeListeners;
|
|
160
177
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sour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D;AAID,YAAY,EACV,oBAAoB,EACpB,SAAS,EACT,WAAW,EACX,WAAW,EACX,mBAAmB,GACpB,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;AAGD,wBA0BC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "munim-bluetooth",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.16",
|
|
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",
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NitroModules } from 'react-native-nitro-modules'
|
|
2
|
+
import { NativeEventEmitter, NativeModules } from 'react-native'
|
|
2
3
|
import type {
|
|
3
4
|
MunimBluetooth as MunimBluetoothSpec,
|
|
4
5
|
AdvertisingDataTypes,
|
|
@@ -11,6 +12,26 @@ import type {
|
|
|
11
12
|
const MunimBluetooth =
|
|
12
13
|
NitroModules.createHybridObject<MunimBluetoothSpec>('MunimBluetooth')
|
|
13
14
|
|
|
15
|
+
// Event Emitter for Bluetooth events
|
|
16
|
+
const { MunimBluetoothEventEmitter } = NativeModules
|
|
17
|
+
|
|
18
|
+
console.log('[munim-bluetooth] Checking for event emitter...', MunimBluetoothEventEmitter ? 'FOUND' : 'NOT FOUND')
|
|
19
|
+
console.log('[munim-bluetooth] Available NativeModules:', Object.keys(NativeModules).filter(key => key.includes('Bluetooth') || key.includes('Munim')))
|
|
20
|
+
|
|
21
|
+
let eventEmitter: NativeEventEmitter | null = null
|
|
22
|
+
|
|
23
|
+
if (MunimBluetoothEventEmitter) {
|
|
24
|
+
try {
|
|
25
|
+
eventEmitter = new NativeEventEmitter(MunimBluetoothEventEmitter)
|
|
26
|
+
console.log('[munim-bluetooth] Event emitter initialized successfully')
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.error('[munim-bluetooth] Failed to initialize event emitter:', error)
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
console.warn('[munim-bluetooth] Event emitter module not found in NativeModules - device discovery events will not work')
|
|
32
|
+
console.warn('[munim-bluetooth] This usually means the native module was not linked properly or needs a rebuild')
|
|
33
|
+
}
|
|
34
|
+
|
|
14
35
|
// ========== Peripheral Features ==========
|
|
15
36
|
|
|
16
37
|
/**
|
|
@@ -233,10 +254,43 @@ export function readRSSI(deviceId: string): Promise<number> {
|
|
|
233
254
|
|
|
234
255
|
// ========== Event Management ==========
|
|
235
256
|
|
|
257
|
+
/**
|
|
258
|
+
* Add a device found event listener (for scanning).
|
|
259
|
+
*
|
|
260
|
+
* @param callback - Function to call when a device is found
|
|
261
|
+
* @returns A function to remove the listener
|
|
262
|
+
*/
|
|
263
|
+
export function addDeviceFoundListener(callback: (device: BLEDevice) => void): () => void {
|
|
264
|
+
if (!eventEmitter) {
|
|
265
|
+
console.warn('[munim-bluetooth] Cannot add listener - event emitter not available')
|
|
266
|
+
return () => {}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const subscription = eventEmitter.addListener('deviceFound', callback)
|
|
270
|
+
return () => subscription.remove()
|
|
271
|
+
}
|
|
272
|
+
|
|
236
273
|
/**
|
|
237
274
|
* Add an event listener.
|
|
238
275
|
*
|
|
239
276
|
* @param eventName - The name of the event to listen for.
|
|
277
|
+
* @param callback - The callback to invoke when the event occurs.
|
|
278
|
+
* @returns A function to remove the listener
|
|
279
|
+
*/
|
|
280
|
+
export function addEventListener(eventName: string, callback: (data: any) => void): () => void {
|
|
281
|
+
if (!eventEmitter) {
|
|
282
|
+
console.warn('[munim-bluetooth] Cannot add listener - event emitter not available')
|
|
283
|
+
return () => {}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const subscription = eventEmitter.addListener(eventName, callback)
|
|
287
|
+
return () => subscription.remove()
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Add an event listener (legacy method).
|
|
292
|
+
*
|
|
293
|
+
* @param eventName - The name of the event to listen for.
|
|
240
294
|
*/
|
|
241
295
|
export function addListener(eventName: string): void {
|
|
242
296
|
return MunimBluetooth.addListener(eventName)
|
|
@@ -284,6 +338,8 @@ export default {
|
|
|
284
338
|
getConnectedDevices,
|
|
285
339
|
readRSSI,
|
|
286
340
|
// Events
|
|
341
|
+
addDeviceFoundListener,
|
|
342
|
+
addEventListener,
|
|
287
343
|
addListener,
|
|
288
344
|
removeListeners,
|
|
289
345
|
}
|