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 94 forks source link

Cannot read property 'getBondedDevices' of null #275

Closed woflmao closed 1 year ago

woflmao commented 1 year ago

Mobile Device Environment Provide a list of operating systems on which this issue is relevant.

Application Environment Provide information about your development environment:

Describe the bug Android worked fine, when I switched Expo to build ios, I am getting "Cannot read property 'getBondedDevices' of null" when attempting to use RNBluetoothClassic.getBondedDevices(). Upon logging RNBluetoothClassic I get this result:

{"_eventEmitter": {"_nativeModule": {}}, "_nativeModule": null}`

As opposed to the same call on Android which produces this:

{"_eventEmitter": {"_nativeModule": {}},
 "_nativeModule": {"accept": [Function promiseMethodWrapper],
 "addListener": [Function nonPromiseMethodWrapper],
 "availableFromDevice": [Function promiseMethodWrapper],
 "cancelAccept": [Function promiseMethodWrapper],
 "cancelDiscovery": [Function promiseMethodWrapper],
 "clearFromDevice": [Function promiseMethodWrapper],
 "connectToDevice": [Function promiseMethodWrapper],
 "disconnectFromDevice": [Function promiseMethodWrapper],
 "getBondedDevices": [Function promiseMethodWrapper],
 "getConnectedDevice": [Function promiseMethodWrapper],
 "getConnectedDevices": [Function promiseMethodWrapper],
 "getConstants": [Function anonymous],
 "isBluetoothAvailable": [Function promiseMethodWrapper],
 "isBluetoothEnabled": [Function promiseMethodWrapper],
 "isDeviceConnected": [Function promiseMethodWrapper],
 "openBluetoothSettings": [Function nonPromiseMethodWrapper],
 "pairDevice": [Function promiseMethodWrapper],
 "readFromDevice": [Function promiseMethodWrapper],
 "readMultipleFromDevice": [Function promiseMethodWrapper],
 "readOneFromDevice": [Function promiseMethodWrapper],
 "removeAllListeners": [Function nonPromiseMethodWrapper],
 "removeListener": [Function nonPromiseMethodWrapper],
 "requestBluetoothEnabled": [Function promiseMethodWrapper],
 "setBluetoothAdapterName": [Function promiseMethodWrapper],
 "startDiscovery": [Function promiseMethodWrapper],
 "unpairDevice": [Function promiseMethodWrapper],
 "writeToDevice": [Function promiseMethodWrapper]}}

To Reproduce Steps to reproduce the behavior:

  1. Call RNBluetooth.getBondedDevices() on an ios device

Expected behavior Expect to return a list of bonded devices

kenjdavidson commented 1 year ago

From my understanding there are a bunch of issues related to Bluetooth and Expo. There was a secondary/addon library documented in order to get it working https://kenjdavidson.com/react-native-bluetooth-classic/expo/ you'll need to look into that. I don't do much Mobile development anymore, and I've never used Expo, so this is out of my realm.

Also, have you followed all the required functionality for IOS, ie. MFi protocol strings? This may be compiling on Expo but failing here instead of at the protocol setting portion.

woflmao commented 1 year ago

Yes I have the MFi protocol string in the expo plugins "with-rn-bluetooth-classic" section, thanks for the quick response though I will do some more research!

kenjdavidson commented 1 year ago

Can you email me the protocols? What's the device?

The error implies that the native module isn't loading. Have you tired ejecting and doing locally?

kenjdavidson commented 1 year ago

Following up for those that may come across this issue. It looks like the IOS side of things is not auto-linking the native module. @woflmao is going to attempt to:

Chances are that React Native changed how Auto linking of native modules works, I recently read something in 0.70 version about native modules being changed. If this is the case, I'll attempt to break off:

PRs will be appreciated if so.

woflmao commented 1 year ago

Update: The steps shared by @kenjdavidson above did work to solve this issue. Thanks again for all the help.

kenjdavidson commented 1 year ago

Glad you got it working, thanks!

ShaneZhengNZ commented 6 months ago

@woflmao I am having the same issue. I am using Expo managed workflow with dev client. Do you have an idea about what I need to do to fix the issue? I am actually experiencing the issue with Android as well.

kenjdavidson commented 6 months ago

If you're seeing the issue in both Android and IOS then you're most likely not using the with-rn-bluetooth-classic plugin appropriately and therefore the module/library isn't getting installed. You'll need to follow those steps to make sure the module is loaded correctly.