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

Can't open peerSocket

Hi All, 
 
I am trying to make an companion app that can send some commands, but I do not succeed making any connection. Here is my code to just make the first connection:
 
app/index.js:
// Import the messaging module
import * as messaging from "messaging";

// Check peerSocket values
console.log("app messaging.peerSocket.readyState: "+ messaging.peerSocket.readyState);
console.log("app messaging.peerSocket.OPEN: "+ messaging.peerSocket.OPEN);

// Request weather data from the companion
function fetchWeather() {
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
// Send a command to the companion
messaging.peerSocket.send({
command: 'weather'
});
}
}

// Listen for the onopen event
messaging.peerSocket.onopen = function() {
// Fetch weather when the connection opens
fetchWeather();
}

// Listen for messages from the companion
messaging.peerSocket.onmessage = function(evt) {
if (evt.data) {
console.log("Data found: "+evt.data);

}
}

// Listen for the onerror event
messaging.peerSocket.onerror = function(err) {
// Handle any errors
console.log("Connection error: " + err.code + " - " + err.message);
}

// Fetch the weather every 30 minutes
setInterval(fetchWeather, 30 * 1000 * 60);
 
Companion/index.js
// Import the messaging module
import * as messaging from "messaging";

// Send the weather data to the device
function returnWeatherData(data) {
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
// Send a command to the device
messaging.peerSocket.send("hello from companion");
} else {
console.log("Error: Connection is not open");
}
}

// Listen for messages from the device
messaging.peerSocket.onmessage = function(evt) {
if (evt.data && evt.data.command == "weather") {
// The device requested weather data
console.log("The device requested weather data");
}
}

// Listen for the onerror event
messaging.peerSocket.onerror = function(err) {
// Handle any errors
console.log("Connection error: " + err.code + " - " + err.message);
}
 
Console:
[11:55:42 AM]App Started
app/index.js:4,1[11:55:42 AM]app starts
app/index.js:5,1[11:55:42 AM]app messaging.peerSocket.readyState: 1
app/index.js:6,1[11:55:42 AM]app messaging.peerSocket.OPEN: 0
[HOST][11:55:42 AM]Launch complete - durations: foregrounding(942ms), first paint(24ms), total(969ms).
 
Build output:
This project is being built without a settings component. Create a file named settings/index.tsx, settings/index.ts, settings/index.jsx or settings/index.js to add a settings component to your project.
(It this required?)
 
 
 It's mystery to me, why the peerSocket doesn't open. I also have tried the BART example, Bluetooth is on, dev bridge is connected, I restarted the app, and also read the other two topics about this problem. The suggestions in these two topics doesnt help me, because I cant get peerSocket ready at all. 
 
I am looking forward to your replies!
 
Jesper
 
 
 
Best Answer
0 Votes
3 REPLIES 3

Here's a minimal example, check it's working for you:

device/index.js

import * as messaging from "messaging";

messaging.peerSocket.onopen = function() {
  console.log(`Device OPEN`);
  sendMessage();
}

messaging.peerSocket.onerror = function(err) {
  console.log(`Device ERROR: ${err.code} ${err.message}`);
}

function sendMessage() {
  console.log(`Device SEND`);
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    messaging.peerSocket.send({cmd: "ping!"});
  } else {
    console.log("Dude, where's my peerSocket?");
  }
}

companion/index.js

import * as messaging from "messaging";

messaging.peerSocket.onopen = function() {
  console.log(`Companion OPEN`);
}

messaging.peerSocket.onerror = function(err) {
  console.log(`Companion ERROR: ${err.code} ${err.message}`);
}

messaging.peerSocket.onmessage = function(evt) {
  console.log(`Companion MESSAGE: ${JSON.stringify(evt.data)}`);
}
Best Answer
0 Votes

Actually I have the same problem.

When connecting to the simulator, your minimal example works.

However, using my Versa 2, it no longer works. The onopen isn't even called.

Best Answer

Remark to my own post: I solved the issue. 'Solved', since I removed the dev apps, restarted my iPhone and reinstalled the app again. Then it works.

 

Something to look at for the fitibit devs! 😉

 

Best Answer
0 Votes