kirillzyusko / react-native-wifi-p2p

Library that provide access for working with wi-fi direct (p2p) module in android.
164 stars 32 forks source link

startDiscoveringPeers() always rejects with code 0 (ERROR) #17

Closed NoamDev closed 4 years ago

NoamDev commented 4 years ago

Yes, I request the ACCESS_FINE_LOCATION permission. What else could cause that? thanks.

kirillzyusko commented 4 years ago

Hi, @NoamDev It might be helpful: https://github.com/kirillzyusko/react-native-wifi-p2p/issues/13 After calling startDiscoveringPeers() you always get and error in catch?

NoamDev commented 4 years ago

Yes, it always results with a catch. Thats my code:

    wifiP2P.initialize();
    const status = await wifiP2P.isSuccessfulInitialize();
    if (!status) {
      console.error('wifip2p initialization was not successful');
    } else {
      console.log('wifip2p initialized');
    }
    await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
    );
    try {
      await wifiP2P.startDiscoveringPeers();
      console.log('Discovering peers started');
    } catch (e) {
      console.log('Could not start peers discovery');
      console.log(e.code);
    }

dependecies:

"dependencies": {
    "react": "16.11.0",
    "react-native": "0.62.2",
    "react-native-wifi-p2p": "^1.0.2"
  },
kirillzyusko commented 4 years ago

Does your wifi turned on? And phone supports p2p mode?

kirillzyusko commented 4 years ago

Also did you try to run your code on another device? I can't say anything about your case, since you receive general error. You can read about this: https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager#ERROR I think it's not an issue of the library. And last clarification question: do you use a real device? Not simulator?

NoamDev commented 4 years ago

it worked on another device, thank you!

NoamDev commented 4 years ago

OH! It turned out that on new android versions the location should be on too! Now it works on the pixel as well.

NoamDev commented 4 years ago

Thank for your time, you've got my star:)

kirillzyusko commented 4 years ago

It turned out that on new android versions the location should be on too!

@NoamDev Nice to know :) Maybe should we change README file? In order to avoid duplicate issues in future? Could you provide your working code, please?

NoamDev commented 4 years ago

The only things I've changed is:

  1. "COARSE_LOCATION" permission --> "FINE_LOCATION" permission (since android Q, but it'll work for prevoius versions too.)

  2. both wifi and location should be enabled, but that has nothing to do with my code (although I'll probably use something like react-native-android-location-enabler). this is required since android oreo according to a stackoverflow's answer