Android Wear Watchface with Sensor Data -
apologies lack of code examples, i've tried far has been complete mess.
i've tried searching information online, maybe i'm not searching correct terms.
i can create watch face using samples in android studio & i've created application retrieve data sensors. can't combine two.
what i'm trying create android wear watchface can display various pieces of data collected sensors on board watch.
for example, accelerometer on in order enable wrist gestures.
can point me in right direction how go solving this, without posting link android api, i've exhausted that.
regards,
emmett
ok, after 3 days of playing around , frustrating errors, i've managed solve it, if else needs figure out:
first implement sensoreventlistener:
private class engine extends canvaswatchfaceservice.engine implements sensoreventlistener
next, within onvisibilitychanged, registered 2 methods i've created:
@override public void onvisibilitychanged(boolean visible) { super.onvisibilitychanged(visible); if (visible) { registerreceiver(); registeraccelerometer(); mtime.clear(timezone.getdefault().getid()); mtime.settonow(); } else { unregisterreceiver(); unregisteraccelerometer(); } updatetimer(); }
and 2 methods:
private void registeraccelerometer() { sensormanager = (sensormanager) getsystemservice(sensor_service); sensormanager.registerlistener(this, sensormanager.getdefaultsensor(sensor.type_accelerometer), sensormanager.sensor_delay_normal); } private void unregisteraccelerometer() { sensormanager.unregisterlistener(this); }
then, within onsensorchanged, listen accelerometer changes:
@override public void onsensorchanged(sensorevent sensorevent) { ...... // calculate here } sensorx = (int)linear_acceleration[0]; sensory = (int)linear_acceleration[1]; sensorz = (int)linear_acceleration[2]; }
assigning these 3 variables, before ondraw() method called:
@override public void ondraw(canvas canvas, rect bounds) { // current time mtime.settonow(); // set current accelerometer readings. accelerometerx.settext("x : " + string.valueof(sensorx)); accelerometery.settext("y : " + string.valueof(sensory)); accelerometerz.settext("z : " + string.valueof(sensorz)); accelerometerrateofchange.settext("t: " + string.valueof(totaldisplacement)); }
in theory, should same sensors, not accelerometer.
it worth noting created using android studio's sample apps.
Comments
Post a Comment