I've been attempting to build comprehensive Bluetooth functionality, using Swift. I'm currently stuck on how to send events back to React Native from Swift. I attempted this option, but self.bridge
is always nil
. I'm hoping to build an interface file that will contain everything needed to bridge the functionality I need into Obj-C, and then JS (since I can't make the functions in the Bridge module listeners to NSNotification
events or otherwise call them directly in Swift (I think).
BTInterface.swift
@objc(BTInterface)
class BTInterface: NSObject {
var bridge: RCTBridge!
@objc func shouldScanForPeripheral(shouldScan: Bool) {
log.debug("Should scan: \(shouldScan)")
}
@objc func testEvent(eventName: String) {
// self.bridge is always nil and crashes
self.bridge.eventDispatcher.sendAppEventWithName( eventName, body: "Hello" )
}
}
BluetoothBridge.m
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
@interface RCT_EXTERN_MODULE(BTInterface, NSObject)
RCT_EXTERN_METHOD(shouldScanForPeripheral:(BOOL *)shouldScan);
RCT_EXTERN_METHOD(testEvent:(NSString *)eventName)
@end
then I would like to call this from my Bluetooth logic like so:
BTInterface().testEvent("TestEvent")
Exploring some other modules that do similar things, I tried adding the following, to no avail:
added to BTInterface.swift
@objc func initWithBridge(_bridge: RCTBridge) {
self.bridge = _bridge
}