react-native-ble-manager icon indicating copy to clipboard operation
react-native-ble-manager copied to clipboard

Re connecting issue

Open HarshalValanda opened this issue 7 years ago • 17 comments

Version

Tell us which versions you are using:

  • react-native-ble-manager: v6.2.9
  • react-native v0.55.3"
  • iOS v11.2.6

connect peripheral -> RetriveServices -> StartNotification -> Get data from peripheral -> disconnect its working 2 or 3 times and get proper response

after that connect peripheral it working but RetriveServices not working

my question: How can i connect and. disconnect peripheral? is there any number of times connection restriction in iOS?

HarshalValanda avatar Aug 08 '18 13:08 HarshalValanda

This is the same I'm experiencing, see also #366 @marcosinigaglia.

Did you find a way to go through this?

JJSrra avatar Oct 09 '18 09:10 JJSrra

Hi, I had no time to check this. I hope to have some spare time in the next weeks.

marcosinigaglia avatar Oct 09 '18 09:10 marcosinigaglia

The same thing happens to me. After a lot of testing, I have observed, that "disconnect" does not really work. I have peripheral, that stops advertising once a connection with the smartphone is established. If I restart the app while being connected, the android Bluetooth keeps maintaining the connection to the peripheral. Before my app attempts to connect again, it checks with getConnectedPeripherals for connected devices. And indeed – I get the list of connected peripheral id(s), and if I use connect(id) the connection is established, but if I try disconnect(id) I get "peripheral not found" error. Since I can not discover the peripheral, I'm confident that the phone still maintaining the connection to the peripheral. When I restart Bluetooth, everything works fine, but only until the next "disconnect" that I assume does not work. I also tried to refreshCache before disconnecting from the peripheral – no effect or sometimes I get "Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference".

NN01010101 avatar Oct 26 '18 23:10 NN01010101

I'm experiencing the same as @HarshalValanda using iOS, but perhaps you should also look into #366 @NN01010101, in case that helps you in any way. I just updated with my latest testing and retrieveServices is not working yet for me.

@marcosinigaglia Could you take a look at this? Thanks in advance!

JJSrra avatar Jan 22 '19 12:01 JJSrra

I changed in iOS native code and its working

You need to reinitialise BleManager object from library

There is a start function in BleManager.m

Add following line on top of the function [Self init];

HarshalValanda avatar Jan 22 '19 12:01 HarshalValanda

@HarshalValanda Sorry, could you specify the code snippet that I need to add? I'm not sure if by "start function" you mean the init function, and also pasting [self init] throws an error when compiling with Xcode, probably because I'm not doing it properly.

JJSrra avatar Jan 22 '19 12:01 JJSrra

Nevermind, I found the start function, sorry about that 😅

That worked!!! And I realized it's not in the latest BleManager.m file in the library. I think you should make a Pull Request for @marcosinigaglia to add it in the next release. Thanks a lot!

JJSrra avatar Jan 22 '19 12:01 JJSrra

@HarshalValanda In case you don't want to submit a Pull Request for any reason I can make it myself, but I prefer you to do it since you came up with the solution 🙂

JJSrra avatar Jan 22 '19 13:01 JJSrra

fix worked for me - can someone with more knowledge check if this is actually the right thing to do and update the module? thanks!

pkyeck avatar Jan 25 '19 14:01 pkyeck

Hi @pkyeck , so now you call start before connect? I'm trying to understand the problem, I'm not an expert in object-c.

marcosinigaglia avatar Jan 25 '19 14:01 marcosinigaglia

@marcosinigaglia I had the same problem that first connection worked perfectly but after disconnecting and reconnecting the BleManager.retrieveServices(peripheral.id) threw an error saying Peripheral not found or not connected.

I didn't change any JS code, just added the [self init] to the start method and reconnecting is working as expected.

see: https://github.com/pkyeck/react-native-ble-manager/blob/master/ios/BleManager.m#L283

pseudocode:

import BleManager from 'react-native-ble-manager';

class XX extends React.Component {
  constructor(props) {
    super(props);

    const BleManagerModule = NativeModules.BleManager;
    this.bleManagerEmitter = new NativeEventEmitter(BleManagerModule);
  }

  componentDidMount() {
    // add all this.bleManagerEmitter.addListener ...
    // finally start
    BleManager.start({ showAlert: false });
    BleManager.checkState();
  }

  async componentWillUnmount() {
    await BleManager.disconnect(selectedPeripheral.id);
    // remove all this.bleManagerEmitter.addListener
  }

  onBTChange(args: any) {
    if (args.state === 'on') {
      BleManager.scan([], 30, true);
    }
  }
  
  onDiscoverPeripheral = async (peripheral) => {
    try {
      if (peripheral.advertising.localName === 'xxx') {
        await BleManager.stopScan();

        // connect
        await BleManager.connect(peripheral.id);

        // THIS FAILS AFTER RECONNECTING TO THE SAME DEVICE
        await BleManager.retrieveServices(peripheral.id);

        await BleManager.startNotification(...)
      }
    } catch (err) {}
  };
}

pkyeck avatar Jan 25 '19 14:01 pkyeck

So the problem appear after you put the app in background? The start is called only in componentDidMount.

marcosinigaglia avatar Jan 25 '19 14:01 marcosinigaglia

ah sorry, should have mentioned that I do all this on an overlay -> so:

  1. show overlay -> (constructor) -> componentDidMount -> connect ...
  2. hide overlay -> componentWillUnmount -> disconnect ...
  3. show overlay -> componentDidMount -> connect ... => ERROR

pkyeck avatar Jan 25 '19 14:01 pkyeck

ah sorry, should have mentioned that I do all this on an overlay -> so:

  1. show overlay -> (constructor) -> componentDidMount -> connect ...
  2. hide overlay -> componentWillUnmount -> disconnect ...
  3. show overlay -> componentDidMount -> connect ... => ERROR

I have the same problem

pdbq21 avatar Apr 04 '19 23:04 pdbq21

I had the same problem and it is resolved thank you kudos @HarshalValanda @pkyeck

puneetkumarC avatar May 02 '19 11:05 puneetkumarC

Im trying the suggested solution with the [self init]; added to the start function. However xcode keeps telling me that expression result unused. I tried this proposed fix on a couple different versions of the library but always the same result and always same issue.

Moreover, the recent change in v6.6.4 here doesn't help either. This version and the previous release version both do not show the device upon "re-scan" after the device was turned off and on again.

cmcleese avatar May 10 '19 18:05 cmcleese

Im trying the suggested solution with the [self init]; added to the start function. However xcode keeps telling me that expression result unused. I tried this proposed fix on a couple different versions of the library but always the same result and always same issue.

Moreover, the recent change in v6.6.4 here doesn't help either. This version and the previous release version both do not show the device upon "re-scan" after the device was turned off and on again.

@cmcleese Did you find anything out about the 'expression result unused' message? Im having the same issue.

blendimaliqi avatar Jan 21 '22 12:01 blendimaliqi