I've just gotten started with Garmin app development so bear with me.
I'm trying to update a data field on my Edge 540 with data from a companion app. I've got it to work in the simulator with the following code:
import Toybox.Activity;
import Toybox.Lang;
import Toybox.Time;
import Toybox.WatchUi;
import Toybox.Communications;
class dexView extends WatchUi.SimpleDataField {
private var _current = -1;
function phoneMessageCallback(message as Message) as Void {
System.println("Got a messagee");
System.println(message);
self._current = message.data["current"];
}
function initialize() {
SimpleDataField.initialize();
Communications.registerForPhoneAppMessages(method(:phoneMessageCallback));
}
function compute(info as Activity.Info) as Numeric or Duration or String or Null {
return _current;
}
}In the simulator I can then use the "Phone App Message" to send an object {"current": 0.5} which then updates the data field.
With this working I moved on to trying to make it work on device, but no luck so far. I gotten the Connect IQ app onto the Edge 540, but I can't send a message from my companion app to the device. I've successfully listed the devices and got a hold of my device id. Then I took the app id from my Connect IQ app setup. When running the app it asked for Bluetooth permission which I granted, it logs "Sent" but then the message gets lost. Here is the source of the relevant parts of the companion app:
import SwiftUI
import ConnectIQ
struct Msg: Codable {
var current: Float
}
let device = IQDevice(id: UUID(uuidString: "0EC74F14-CFF4-46CF-0E7E-510DAD830A06"), modelName: "Edge 540", friendlyName: "Edge 540")
let uuid = "7d968ef6-d048-418e-85fd-dc9367623260"
let store = "7d968ef6-d048-418e-85fd-dc9367623261"
let app = IQApp(uuid: UUID(uuidString: uuid), store: UUID(uuidString: store), device: device)
struct ContentView: View {
var body: some View {
VStack {
Button("List devices") {
ConnectIQ.sharedInstance().showDeviceSelection()
}
Button("Send message") {
let msg = Msg(current: 2)
ConnectIQ.sharedInstance().sendMessage(msg, to: app) { x, y in
print("Sending...")
print(x)
print(y)
} completion: { res in
print("Sent!", msg)
print(res.rawValue)
}
}
}
.padding()
}
}
Is there something obvious that I'm missing? One thing that struck me that I need to run past you all is if it's even possible to update a data field from a companion app, or is it only for apps? Fnd some old threads in the forum, but perhaps things have changed over the last 8 years.