react-native-wifi-p2p sendFile() and receiveFile() is not working

124 Views Asked by At

I have created a react native app where initially the server creates a group, and then the client connects to the group.The server should send those files to the client. I tried sending files by calling receiveFiles() in the client and then sendFiles() in the server. In the server it displays as "You can use the storage" but the client doesn't receive the files. How can I send files from the server to the client?

We want to share data from one device to another device using wifi-direct. Here we are using react-native-wifi-p2p (https://www.npmjs.com/package/react-native-wifi-p2p)

 onSendFile = () => {
//const url = '/storage/sdcard0/Music/Rammstein:Amerika.mp3';
PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE
)
  .then((granted: any) => {
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('You can use the storage');
    } else {
      console.log('Storage permission denied');
    }
  })
  .then(() => {
    return PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
    );
  })
  .then(() => {
    DocumentPicker.pick({
      presentationStyle: 'fullScreen',
    })
      .then(async (file) => {
        // Send the selected file
        console.log(file[0].uri);
        const filePath = file[0].uri;
        RNFetchBlob.fs
          .readFile(filePath, 'base64')
          .then((data) => {
            sendFile(data);
          })
          .catch((err) => { console.log(err); });
      })
      .catch((err: any) => {
        // Error selecting file
        console.log(err);
      });
    // const response = await DocumentPicker.pick({
    //   presentationStyle: 'fullScreen',
    // });
    // console.log(response[0].uri);
    // return sendFile(response[0].uri)
    //   .then((metaInfo: any) => console.log('File sent successfully', metaInfo))
    //   .catch((err: any) => console.log('Error while file sending', err));
  })
  .catch((err: any) => console.log(err));

};

ReceiveFile

onReceiveFile = () => {
PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE
)
  .then((granted: any) => {
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('You can use the storage');
    } else {
      console.log('Storage permission denied');
    }
  })
  .then(() => {
    return PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
    );
  })
  .then(() => {

    console.log('tag 98');
    // eslint-disable-next-line no-undef
    return receiveFile("/storage/", "test.txt")
      .then((file) => {
        console.log('File received successfully');
        const filePath = `${folder}/${file.name}`
        RNFS.writeFile(filePath, file.data, 'base64')
          .then(() => {
            console.log('File received successfully');
          })
          .catch((err) => {
            console.log(err);
          })
      })
      .catch((err: any) => console.log('Error while file receiving', err));
  })
  .catch((err: any) => {
    console.log("Tag 78" + err);
  });

};

0

There are 0 best solutions below