Undefined symbol "vibeData" detected.

Former Member
Former Member

Working on a widget for alerting something, but I'm hitting a wall here, I'm taking the cod snippet direct from the API docs but I keep getting this error "Undefined symbol "vibeData" detected." Code below.

if (Attention has :vibrate) {
		    vibeData =
		    [
		        new Attention.VibeProfile(25, 2000),
		        new Attention.VibeProfile(50, 2000),
		        new Attention.VibeProfile(100, 2000)
		    ];
}
Attention.vibrate(vibeData);

Apologies if this is a noob question but I really cant find any resources out there to help.

  • That is not really a very good code snippet. It doesn't work because the compiler requires that you've declared vibeData prior to using it, and that code doesn't declare a variable named vibeData.

    The following would be a more reasonable way to do this.

    if (Attention has :vibrate) {
        var vibeData = [
            new Attention.VibeProfile(25, 2000),
            new Attention.VibeProfile(50, 2000),
            new Attention.VibeProfile(100, 2000)
        ];
        Attention.vibrate(vibeData);
    }
    

    This is better because it declares vibeData and it doesn't attempt to call Attention.vibrate() if the Attention module doesn't have a function named vibrate. The former error will prevent the code from compiling (as you've noticed), and the latter one will cause a symbol not found error if you run it on devices that don't have vibrate support.

  • Former Member
    Former Member over 6 years ago in reply to Travis.ConnectIQ

    Ty, it turned out it was needed the var before it, but not there, had to declare it outside the if statement for it to work. 

  • Yes, declaring vibeData outside of the if in the original block will get it to build and run for some devices, but it will break if you try to run that code on a device that doesn't have support for the Attention.vibrate() method. Here is a breakdown explaining why.

    // this is the declaration you added to avoid the compile error
    var vibeData;
    
    // this line checks to see if the device supports the Attention.vibrate() function. if
    // the functionality is not available, the if block will be skipped, and vibeData will
    // not get initialized to point to an array of VibeProfile objects
    if (Attention has :vibrate) {
        vibeData = [
            new Attention.VibeProfile(25, 2000),
            new Attention.VibeProfile(50, 2000),
            new Attention.VibeProfile(100, 2000)
        ];
    }
    
    // this line calls Attention.vibrate() regardless of whether or not the current device
    // has support. if you run this code on such a device, you'll get a 'symbol not found'
    // error when this call is made and your app will crash.
    Attention.vibrate(vibeData);

    The appropriate fix is to put the call to Attention.vibrate() inside the if block that checks to see if the symbol is available or not. You don't necessarily need to declare vibeData in that block, but it seems silly to do so elsewhere since VibeProfile will also not be available on devices that don't have support for vibrate.

  • Yes, you want to call Attention.vibrate() inside the block.

    While calling it outside the block will work on all watches (they all have vibrate), it will cause a crash on all Edge devices (none of them have vibrate)