SDK 3.1 - feature compatibility

A particularly interesting feature for my Data Fields for endurance cycling is the ANT manufacturer page access.

1. anyone know if data fields can access these?

2. I want to use this to get the battery status of things like a power meter, speed sensor, HR strap, bontrager head and tail light, etc.

3. does this require a background process like we need to grab a device’s temperature? I can do that from a data field but life is easier if we can just grab a device system structure and see the variables anytime.

  • While not obvious from the documentation, it only takes a few minutes to build a test app and try this out. Here is some sample code that you should be able to try on your device.

    using Toybox.WatchUi;
    using Toybox.AntPlus;
    using Toybox.Graphics;
    
    class AntPlusBatteryStatusView extends WatchUi.DataField {
    
        hidden var _devices;
    
        function initialize() {
            DataField.initialize();
            
            _devices = [];
            
            if (AntPlus has :BikeCadence) {
                _devices.add({ :name => "cadence", :device => new AntPlus.BikeCadence(null) });
            }
            
            if (AntPlus has :BikeSpeed) {
                _devices.add({ :name => "speed", :device => new AntPlus.BikeSpeed(null) });
            }
            
            if (AntPlus has :LightNetwork) {
                _devices.add({ :name => "light", :device => new AntPlus.LightNetwork(null) });
            }
        }
        
        hidden const _FONTS = [
            Graphics.FONT_MEDIUM,
            Graphics.FONT_SMALL,
            Graphics.FONT_TINY,
            Graphics.FONT_XTINY,
        ];
        
        hidden const _BATTERY_STATUS = [
            "NEW",
            "GOOD",
            "OKAY",
            "LOW",
            "CRITICAL",
            "INVALID",
        ];
        
        hidden function getBatteryStatus(device, component) {
            var status = device.getBatteryStatus(component);
            if (status == null) {
                return _BATTERY_STATUS[5];
            }
            else if (_BATTERY_STATUS.size() < status || status < 0) {
                return _BATTERY_STATUS[5];
            }
            else {
                return _BATTERY_STATUS[status.batteryStatus];
            }
        }
    
        function onUpdate(dc) {
            
            var lines = [];
            for (var i = 0; i < _devices.size(); ++i) {
                var entry = _devices[i];
                
                var name   = entry[:name];
                var device = entry[:device];
                
                var deviceState = device.getDeviceState();
                if (deviceState.state == AntPlus.DEVICE_STATE_DEAD) {
                    lines.add(Lang.format("$1$: dead", [ name ]));
                }
                else if (deviceState.state == AntPlus.DEVICE_STATE_CLOSED) {
                    lines.add(Lang.format("$1$: closed", [ name ]));
                }
                else if (deviceState.state == AntPlus.DEVICE_STATE_SEARCHING) {
                    lines.add(Lang.format("$1$: searching", [ name ]));
                }
                else if (deviceState.state == AntPlus.DEVICE_STATE_TRACKING) {
    	            var components = device.getComponentIdentifiers();
    	            if (components.size() == 1) {
    	                var batteryStatus = getBatteryStatus(device, components[0]);
    	                lines.add(Lang.format("$1$: $2$", [ name, batteryStatus ]));
    	            }
    	            else {
    	                for (var j = 0; j < components.size(); ++j) {
    	                    var batteryStatus = getBatteryStatus(device, components[i]);
    	                    lines.add(Lang.format("$1$-$2$: $3$", [ name, i, batteryStatus ]));
    	                }
    	            }
                }
            }
            
            var height = dc.getHeight();
            var font = null;
            var line_height = null;
            
            for (var i = 0; i < _FONTS.size(); ++i) {
                font = _FONTS[i];
                line_height = dc.getFontHeight(font);
                
                if (lines.size() * line_height < height) {
                    break;
                }
            }
            
            dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_BLACK);
            dc.clear();
            
            var justification = Graphics.TEXT_JUSTIFY_CENTER;
    
            var cx = dc.getWidth() / 2;
            var cy = height / 2;
            
            cy -= (lines.size() * line_height) / 2;
    
            for (var i = 0; i < lines.size(); ++i) {
                dc.drawText(cx, cy, font, lines[i], justification);
                cy += line_height;
            }
         }
     }