Git Product home page Git Product logo

react-native-google-nearby-connection's Issues

Unable to Retry Nearby.saveFile() after initial permission failure (granting after initial failure)

Hey Butch, first need to say amazing library!

I have a question regarding the ability to call Nearby.saveFile(serviceId,endpointId,payloadId) after an initial permission failure. Currently, as long as a user has granted permissions to READ/WRITE external storage, calling Nearby.saveFile() has the promise resolve successfully. If permissions are not granted or are revoked before calling saveFile, one encounters a promise rejection. Until here things are still behaving as expected.

However, in my case I am trying to offer the user the ability to retry executing saveFile() at a later point by storing the serviceId, endpointId, and payloadId before initially calling saveFile. This way, once saveFile() is called and it fails, the user can be prompted to grant permissions within their settings or elsewhere and try calling it again. The problem is that when I allow the user to retry using the same serviceId, endpointId, and payloadId which previously failed due to permissions not being granted, the promise still rejects.

When I adb logcat I see it is failing with the message 03-24 14:31:03.806 24644 26856 V NearbyConnection: Cannot convert to file., which is line #1501 here

public void saveFile(final String serviceId, final String endpointId, final String payloadId, final Promise promise) {
logV("saveFile from service "+serviceId+" and endpoint " + endpointId + " and payload "+payloadId);
final Payload payload = mReceivedPayloads.get(serviceId+"_"+endpointId+"_"+payloadId);
if (payload == null) {
logV("Cannot find payload.");
promise.reject("");
return;
}
if (payload.getType() != Payload.Type.FILE) {
logV("Cannot save, not a file.");
promise.reject("");
return;
}
final String payloadFileData = mPayloadFileData.get(payloadId);
int colonIndex = payloadFileData.indexOf(':');
final String payloadFilename = payloadFileData.substring(0, colonIndex);
final String payloadMetadata = payloadFileData.substring(colonIndex+1);
if (payloadFilename == null) {
logV("Cannot find filename for "+payloadId);
promise.reject("");
return;
}
final Activity activity = getCurrentActivity();
permissionsCheck(activity, Arrays.asList(getRequiredStoragePermissions()), new Callable<Void>() {
@Override
public Void call() throws Exception {
File payloadFile = payload.asFile().asJavaFile();
if (payloadFile == null) {
logV("Cannot convert to file.");
promise.reject("");
}
else {
File destPath = payloadFile.getParentFile();
String extension = NearbyConnectionModule.getFileExtension(payloadFilename);
File destFile = NearbyConnectionModule.getUniqueFile(destPath, extension);
payloadFile.renameTo(destFile);
String destFilePath = destFile.getPath();
logV("file saved to "+destFilePath);
WritableMap out = Arguments.createMap();
out.putString("path", destFilePath);
out.putString("originalFilename", payloadFilename);
out.putString("metadata", payloadMetadata);
promise.resolve(out);
}
return null;
}
});
}

In other words it seems that
File payloadFile = payload.asFile().asJavaFile(); is null. I decided to log every payload variable until permissionsCheck fires and this is the below output:

03-24 14:31:03.805 24644 26856 V NearbyConnection: saveFile from service com.google.myApp.v1 and endpoint hT4- and payload -6821529802993021226
03-24 14:31:03.805 24644 26856 V NearbyConnection: Payload com.google.android.gms.nearby.connection.Payload@16edba2
03-24 14:31:03.805 24644 26856 V NearbyConnection: payload.getType() 2
03-24 14:31:03.805 24644 26856 V NearbyConnection: payloadFileData  awesomePhoto.WEBP:{"description":"a flower"}
03-24 14:31:03.805 24644 26856 V NearbyConnection: payloadFilename awesomePhoto.WEBP
03-24 14:31:03.805 24644 26856 V NearbyConnection: payloadMetadata {"description":"a flower"}
03-24 14:31:03.806 24644 26856 V NearbyConnection: Cannot convert to file.

Question/s: Any idea why payload.asFile().asJavaFile() returns null when retrying saveFile? Is it that the initial permissions failure causes the payload content to somehow be destroyed within the NearbyConnection cache so there is nothing to call payload.asFile().asJavaFile() on? Although I find it strange that calling payload.getType() works and other metadata still exists on the payload but asFile() fails. Any thoughts here would be amazing.

Assumption/Hypothesis: After the final payload is delivered, Nearby Connections attempts to write the file which, if the permissions are not given, fails. Therefore, regardless if permissions are then granted after, that final payload will still not have a file associated and, as such, will forever fail. If this is correct then there is no other option than to resend the file I'm guessing.

Stackoverflow link: https://stackoverflow.com/questions/55328027/nearbyconnection-payload-asfile-asjavafile-is-null-when-retrying-to-savefil

Project support

Wanted to check if the package is still being supported and worked on?

TypeError: null is not an object (evaluating '..NearbyConnection.endpoints')

Hi, butchmarshall

I tried to run your example into more recent google nearby. I spent a couple of days to tackle this and updated most of the Gradle and SDK versions to the most recent versions and it is building the app successfully except for this runtime error. I am using "react-native": "0.61.5".

Nearby_issue

renderApplication.js (this file is part of React-Native and hasn't been modified in any way)

Screenshot 2020-02-02 20 58 45

index.js

class NearbyConnection {
	// Get all the endpoints
	static endpoints() {
		return NativeModules.NearbyConnection.endpoints();  // <- issue is here
	}

App.js

componentWillMount() {
   Nearby.endpoints().then(endpoints => {
     console.log('endpoints', endpoints);
   });
 }

Could you please take a quick look and point me in the right direction? Thanks!

my fork is in https://github.com/staminna/react-native-google-nearby-connection

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.