Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

File transfer from Ionic back to phone

ANSWERED

As there is no file transfer API from app to companion, I test this code using messaging API.

The code works but with no buffer protection and buffer resetting (actually I don't know how, my MAX_MESSAGE_SIZE reported 1027 instead of 1024) .
And if conection lost during transfer, sending should be triggered manually cause I don't know if there is a kind of call as "send()"

I hope someone can correct me or add some advice, thank you!

 

import * as messaging from "messaging";
import { peerSocket, MessageSocket } from "messaging";
var stringArray = [];
var stringReady = false;
var count = 0;
var many = 0;

function manualStart() {
  console.log("ionic:Max message size=" + MessageSocket.MAX_MESSAGE_SIZE);

  //manual reset to stop and restart
  var count = 0;

  //let testString = fs.readFileSync("testText.txt", "UTF8");
  let testString = "START...";

  many = Math.floor(testString.length / 64) + 1;
  console.log("many = "+many);

  for (var i = 0; i <= many -1; i++) {
    stringArray[i] = testString.substring(i*64, i*64+64);
    console.log("array"+"-"+i+" = "+stringArray[i]);
  }

  //string is ready, go
  stringReady = true;
  continueSendingData();
}

//Listen for the onbufferedamountdecrease event
messaging.peerSocket.onbufferedamountdecrease = function() {
  console.log("stringReady = "+stringReady+" count = "+count);
  if ((stringReady)&&(count <= many)){
    continueSendingData();
  } 
}

//real sending
function continueSendingData() {
  console.log("sending No: " + count);
    
  // Send data only while the buffer contains less than 64 bytes
  while (messaging.peerSocket.bufferedAmount < 64) {

    let subS = stringArray[count];
    if (subS != null){
      messaging.peerSocket.send(subS);
    }
    
    count += 1;
    if (count > many){stringReady = false;}
  }
}

//Message socket opens
messaging.peerSocket.onopen = function() {
  console.log("Ionic Socket Open");
}

//Message socket closes
messaging.peerSocket.close = function() { 
  console.log("Companion Socket Closed"); 
}

//Listen for the onerror event
messaging.peerSocket.onerror = function(err) {
  console.log("Ionic Side: connection error: " + err.code + " - " + err.message);
}

 

Best Answer
1 BEST ANSWER

Accepted Solutions

I finally finished the debug of the code myself, now the code is steady and strong, the ONLY modification is this line:

if ((stringReady)&&(count <= many-1))

Yes, just that silly issue without "-1" made the socket unstable... because you pushed an empty to it!

and I am making this reply as the final solution, hope it helps for your project.

Good luck! 

 

View best answer in original post

Best Answer
2 REPLIES 2

I finally finished the debug of the code myself, now the code is steady and strong, the ONLY modification is this line:

if ((stringReady)&&(count <= many-1))

Yes, just that silly issue without "-1" made the socket unstable... because you pushed an empty to it!

and I am making this reply as the final solution, hope it helps for your project.

Good luck! 

 

Best Answer

Glad to see that someone has the same problem that I have.

Do you happen to still have the Companion App code of that? 

Best Answer
0 Votes