New developer looking for help on adding data fields to watch output (Bluetooth Status, Heart Rate and Alarm Count)

Hey all,

I'm a brand new developer (as in this is the first thing I've ever coded and I want to release an open faced watch face for beginners. So far I've been able to add a background, the time, date, step count and battery percent count but now I'm stumped. Whenever I try to add Bluetooth Status, Heart Rate and Alarm Count I'm able to save the watch face without error but upon running it I get the IQ Error.

Can anyone point me in the right direction on the code I should be using to fill in the 3 blank areas below:

import Toybox.Graphics;
import Toybox.Lang;
import Toybox.System;
import Toybox.WatchUi;
import Toybox.ActivityMonitor;
import Toybox.Activity;
import Toybox.Time.Gregorian;

class NGE15View extends WatchUi.WatchFace {

function initialize() {
WatchFace.initialize();
}

// Load your resources here
function onLayout(dc as Dc) as Void {
setLayout(Rez.Layouts.WatchFace(dc));
}

// Called when this View is brought to the foreground. Restore
// the state of this View and prepare it to be shown. This includes
// loading resources into memory.
function onShow() as Void {
}

// Update the view
function onUpdate(dc as Dc) as Void {
// Get and show the current time
var clockTime = System.getClockTime();
var timeString = Lang.format("$1$:$2$", [clockTime.hour, clockTime.min.format("%02d")]);
var view = View.findDrawableById("TimeLabel") as Text;
view.setText(timeString);

//Get and show Battery Display
var battery = System.getSystemStats().battery;
var batteryDisplay = View.findDrawableById("BatteryDisplay");
batteryDisplay.setText(battery.format("%d")+"%");

//Get and show Notification Count
{
var notificationAmount = System.getDeviceSettings().notificationCount;
var formattedNotificationAmount = "";
if(notificationAmount > 999) {
formattedNotificationAmount = "999+";
}
else {
formattedNotificationAmount = notificationAmount.format("%d");
}
var notificationCountDisplay = View.findDrawableById("NotificationCountDisplay");
notificationCountDisplay.setText(formattedNotificationAmount);
}

//Get and show Stepcount
var StepCountDisplay = ActivityMonitor.getInfo().steps.toString();
var stepCountDisplay = View.findDrawableById("StepCountDisplay");
stepCountDisplay.setText(StepCountDisplay);

//Get and show DateDisplay
var now = Time.now();
var date = Time.Gregorian.info(now, Time.FORMAT_LONG);
var dateString = Lang.format("$1$, $2$ $3$", [date.day_of_week, date.month, date.day]);
var dateDisplay = View.findDrawableById("DateDisplay");
dateDisplay.setText(dateString);

//Get and show Heart Rate Amount

//Get and show Bluetooth

//Get and show Notification Count

// Call the parent onUpdate function to redraw the layout
View.onUpdate(dc);
}

// Called when this View is removed from the screen. Save the
// state of this View here. This includes freeing resources from
// memory.
function onHide() as Void {
}

// The user has just looked at their watch. Timers and animations may be started here.
function onExitSleep() as Void {
}

// Terminate any active timers and prepare for slow updates.
function onEnterSleep() as Void {
}

}

  • What is the error you get, and what is in the stack trace? (it should indicate a specific line of code) I don't see where you are trying to get the BT status or the HR.

  • Hey Jim, I tried to do notification count like this:

    //Get and show Notification Count
    var NotificationCount = System.getDeviceSettings().notificationCount;
    var NotificationCountDisplay = View.findDrawableById("NotificationCountDisplay");
    stepCountDisplay.setText(NotificationCountDisplay);

    and I get this error:

    Error: Unhandled Exception
    Exception: UnexpectedTypeException: Expected Number/Float/Boolean/Long/Double, given Object
    Stack:
    - setText() at C:\grmn\prj\DI\toolchain\mbsimulator\submodules\technology\monkeybrains\virtual-machine\api\WatchUi.mb:4668 0x300044b6
    - onUpdate() at C:\Users\\eclipse-workspace\NGE 15\source\NGE15View.mc:73 0x1000038f

    For Bluetooth and Heartrate I don't even know where to begin with the code.

  • var NotificationCountDisplay = View.findDrawableById("NotificationCountDisplay");
    stepCountDisplay.setText(NotificationCountDisplay);

    On the second line use NotifcationCountDisplay instead of setCountDisplay

    For BT, see System.getSystemSettings().phoneConnected.  Heart rate, there a a couple different ways depending on how often you want to see updates.

  • I'm Dense! and thank you for replying Jim! I've been reading all of your replies over the last 6 years while I've tried to learn!

    I already have Notifications working and I'm actually trying to get Alarms up and running (Apologies!):

    There's no error when I compile the error only appears when I Run As the watch:

    //Get and show Alarm Count
    var alarmCount = System.getDeviceSettings().alarmCount;
    var AlarmCountDisplay = View.findDrawableById("AlarmCountDisplay");
    AlarmCountDisplay.setText(AlarmCountDisplay);

    Exception: UnexpectedTypeException: Expected Number/Float/Boolean/Long/Double, given Object
    Stack:
    - setText() at C:\grmn\prj\DI\toolchain\mbsimulator\submodules\technology\monkeybrains\virtual-machine\api\WatchUi.mb:4668 0x300044b6
    - onUpdate() at C:\Users\\eclipse-workspace\NGE 15\source\NGE15View.mc:73 0x1000038f

    In the Layout.xml I have this:

    <label id="AlarmCountDisplay"
    text="1"
    x="34%" y="64%"
    font="Gfx.FONT_XTINY"
    justification="Gfx.TEXT_JUSTIFY_CENTER"
    color="Gfx.COLOR_ORANGE" />

  • Displaying the alarm count should be just like displaying the notification count, but in a different spot in the layout and getting the value from a different place.

    What do you have for line 73 in NGE15View.mc?

  • Line 73:  AlarmCountDisplay.setText(AlarmCountDisplay);

    And I figured it out by looking at Line 73: It should have been:  AlarmCountDisplay.setText(alarmCount); !

  • Look at how you do notification counts and alarm counts as there's got to be something different (maybe a typo some place)

  • Corrected at Line 73 and Alarms are now working!

    For Bluetooth I attempted the following:

    / var Bluetooth = System.getDeviceSettings().phoneConnected.toString();
    var BluetoothConnectionDisplay = View.findDrawableById("BluetoothConnectionDisplay");
    BluetoothConnectionDisplay.setText(Bluetooth);

    Which works correctly but it shows "True" or "False"

    Is there a way I can make it Say "Yes" or " " (Blank?)

  • phoneConnected is a boolean.

    try

    var Bluetoodth= (System.getDeviceSettings().phoneConnected) ? "Yes" : "";

  • That worked!

    Final Question (I Promise( as I'm trying to get the Heart Rate to work:

    Heart Rate Code:

    //Get and show Heart Rate Amount
    var heartRate = Activity.getActivityInfo().currentHeartRate;
    if (heartRate == null)
    {
    if (ActivityMonitor has :getHeartRateHistory)
    {
    var hrHist = ActivityMonitor.getHeartRateHistory(1, true);
    heartRate = hrHist.next().heartRate;
    }
    }
    if (heartRate == null || heartRate == 255 /* ActivityMonitor.INVALID_HR_SAMPLE */) {
    heartRate = "--"; }
    var heartrateDisplay = View.findDrawableById("heartrateDisplay");
    heartrateDisplay.setText(heartRate);

    Is trigging the error:


    Error: Unexpected Type Error
    Details: Failed invoking <symbol>
    Stack:
    - onUpdate() at C:\Users\\eclipse-workspace\NGE 15\source\NGE15View.mc:83 0x10000436

    Line 83 is:  heartrateDisplay.setText(heartRate);