kenjdavidson / react-native-bluetooth-classic

⚛ Bluetooth classic Android(Bluetooth)/IOS(ExternalAccessory) module for serial communication
https://kenjdavidson.github.io/react-native-bluetooth-classic
MIT License
250 stars 93 forks source link

Cannot get "onStateChanged" working #162

Closed PietroGranati closed 1 year ago

PietroGranati commented 2 years ago

Mobile Device Environment

Application Environment

Describe the bug I cannt made work the events "onStateChanged" "onBluetoothEnabled" "onBluetoothDisabled", the onDevice connect works perfectly instead anche i cannot figure out what is wrong

To Reproduce

useEffect(() => {
        const subscription = RNBluetoothClassic.onStateChanged((e) => { console.log(e.enabled) });

        return function cleanup() {
            subscription.remove();
        };
    }, []);
kenjdavidson commented 2 years ago

Are those even available on IOS? Can you check the documentation? The MFi and ExternalAccessory on IOS has limited functionality and those might not even be available.

kenjdavidson commented 2 years ago

I have some notes saying that I need to see if the CoreBluetooth library can be used for those types of things.

PietroGranati commented 2 years ago

The in the official documentation they're avaiable for both

PietroGranati commented 2 years ago

I'll investigate more

kenjdavidson commented 2 years ago

haha "official" of this library? Could be wrong - you're trusting me a whole bunch.

Looking at the IOS code, there is only registration for:

Looking at the available notifications: https://developer.apple.com/documentation/foundation/nsnotification/name there are only two that are used:

there are a couple others that maybe might work for Enabled/Disabled/State Change:

No idea really.

kenjdavidson commented 2 years ago

Feel free to play around with it and add those registered notifications to:

    /**
     Register with the NotificationCenter and add all appropriate Observers.  Currently the available
     notification types are:
     - .EAAccessoryDidConnect = BTEvent.BLUETOOTH_CONNECTED
     - .EAAccessoryDidDisconnect = BTEvent.BLUETOOTH_DISCONNECTED
     using the appropriate BTEvent type(s)
     */
    private func registerForLocalNotifications() {
        eaManager.registerForLocalNotifications()
        notificationCenter.addObserver(self,
                                       selector: #selector(accessoryDidConnect),
                                       name: .EAAccessoryDidConnect,
                                       object: nil)
        notificationCenter.addObserver(self,
                                       selector: #selector(accessoryDidDisconnect),
                                       name: .EAAccessoryDidDisconnect,
                                       object: nil)
    }

And throw in a pull request, i'd be happy to accept it if you say it works.

PietroGranati commented 2 years ago

Ahah yes of this library, it's a great module however! I'm not so good at IOS coding but I'll give it a try in these days.

Thank you!

kenjdavidson commented 2 years ago

I'm not so good at IOS coding but I'll give it a try in these days.

Join the club!!

I would play around with something like:

private func registerForLocalNotifications() {
        eaManager.registerForLocalNotifications()
        notificationCenter.addObserver(self,
                                       selector: #selector(accessoryDidConnect),
                                       name: .EAAccessoryDidConnect,
                                       object: nil)
        notificationCenter.addObserver(self,
                                       selector: #selector(accessoryDidDisconnect),
                                       name: .EAAccessoryDidDisconnect,
                                       object: nil)

        notificationCenter.addObserver(self,
                                       selector: #selector(bluetoothPoweredOn),
                                       name: .IOBluetoothHostControllerPoweredOff,
                                       object: nil)
    }

@objc
    func  bluetoothPoweredOn(_ notification:Notification) {
            sendEvent(EventType.BLUETOOTH_ENABLED.name,
                      body: (simulate whatever Android sends now)
            sendEvent(EventType.STATE_CHANGED.name,
                      body: (simulate whatever Android sends now)
    }