react-native-bluetooth-classic icon indicating copy to clipboard operation
react-native-bluetooth-classic copied to clipboard

Cannot get "onStateChanged" working

Open PietroGranati opened this issue 2 years ago • 8 comments

Mobile Device Environment

  • Device: [iPad mini]

Application Environment

  • RN Bluetooth Classic version: 1.60.0-rc.20]

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();
        };
    }, []);

PietroGranati avatar Jan 10 '22 16:01 PietroGranati

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 avatar Jan 10 '22 16:01 kenjdavidson

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

kenjdavidson avatar Jan 10 '22 16:01 kenjdavidson

The in the official documentation they're avaiable for both

PietroGranati avatar Jan 10 '22 16:01 PietroGranati

I'll investigate more

PietroGranati avatar Jan 10 '22 16:01 PietroGranati

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:

  • BTEvent.BLUETOOTH_CONNECTED
  • BTEvent.BLUETOOTH_DISCONNECTED

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

  • EAAccesoryDidConnect
  • EAAccessoryDiddisconnect

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

  • IOBluetoothHoseControllerPoweredOff??
  • IOBluetoothHostControllerPoweredON??

No idea really.

kenjdavidson avatar Jan 10 '22 16:01 kenjdavidson

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.

kenjdavidson avatar Jan 10 '22 16:01 kenjdavidson

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!

PietroGranati avatar Jan 10 '22 16:01 PietroGranati

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)
    }

kenjdavidson avatar Jan 10 '22 16:01 kenjdavidson