WearOS returning null on getContext()

Hello,

I’m trying to access the vibrator sensor of my TicWatch Pro 3 (Wear OS). I followed the tutorial, added the permissions “VIBRATE” and “WAKE_LOCK” under “Sketch Permissions”.

Here is my code:

import android.os.Vibrator;
import android.content.Context;
import android.os.VibrationEffect;

Vibrator vibrator;
Context context;

void setup() {
  fullScreen();
  frameRate(1);
  textFont(createFont("SansSerif", 30 * displayDensity));
  fill(255);
  context = getContext();
  vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
}

void draw() {
  background(0);
  translate(0, +wearInsets().bottom/2);
  if (!wearAmbient()) {
    String str = hour() + ":" + minute() + ":" + second();
    if(second()%10 == 0){
      vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
    }
    float w = textWidth(str);
    text(str, (width - w)/2, height/2 + 24);     
  }
}

I’m getting the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

I know that the “context” variable is null, however, I can’t get the getContext() method to return a non-null value.

What am I missing?

Hi Lascasas,

I have the exact same smartwatch and I’ve managed to make the vibrator in the watch work.
Maybe these little changes could help (sample code from one of my Watchfaces):

At the beginning, don’t initialize the context.
In setup(), change ‘context = getContext()’ to ‘Context context = getContext()’.

import android.os.Vibrator;
import android.content.Context;
  
Vibrator vibrator;
circle c;
color[] palette = {#ffffff, #00ff00, #ff0000, #0000ff, #ffff00};

ArrayList<Draggie> Draggies;
void setup() {
  fullScreen();
  frameRate(60);
  rectMode(CENTER);
  Context context = getContext();
  vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  
  c = new circle(width/2);
  Draggies = new ArrayList<Draggie>();
  fill(#000000, 255);
  ellipse(width/2, height/2, width, height);
}

void draw() {
    fill(#000000, 60);
    ellipse(width/2, height/2, width, height);
    fill(#ffffff);    
    c.update();
    c.show();
    if (c.y > height) {
      c.reset();
    vibrator.vibrate(300);
    }
    
    for (int i = Draggies.size() -1; i >=0; i--) {
      Draggies.get(i).show();
      Draggies.get(i).life-=10;
      if (Draggies.get(i).life <= 0) {
        Draggies.remove(i);
      }
    }   
} 

void mouseDragged() {
  Draggies.add(new Draggie(mouseX, mouseY));
}

This works for me without even having to give Sketch Permissions (i.e. VIBRATE). Hope this resolves the issue!

P.S. Furthermore, I’ve noticed that uploading Processing code to Wear OS is very fragile and finnicky. I’ve also made a post, where I describe that trivial things don’t work (e.g. the random() function, PVector(), building own functions outside of draw() or setup() (except in separate classes, those work fine), etc.). Maybe it’s only the Ticwatch itself, I don’t know. Do you experience the same issues? Maybe we could help each other out.