質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Wear

Android Wearとは、Googleが発表した腕時計型ウェアラブルデバイス(スマートウォッチ)向けのプラットフォームです。GoogleのAndroid OSをベースにしており、情報の入手・管理などを行うことができます。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

2355閲覧

スマートウォッチで心拍数の測定、リアルタイムで表示したい

snowdropAPP

総合スコア41

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Wear

Android Wearとは、Googleが発表した腕時計型ウェアラブルデバイス(スマートウォッチ)向けのプラットフォームです。GoogleのAndroid OSをベースにしており、情報の入手・管理などを行うことができます。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2021/04/09 05:04

編集2021/04/09 05:12

環境
androidstudio Ver4.1.2

スマートウォッチで心拍数を測定するアプリを作成してみようと思いました。
ビルドを通してアプリが立ち上がるところまで行きましたがどうもセンサーチェンジイベントが走らないのでなんでだろうと思いました。
デフォルトでアプリに心拍測定の健康アプリみたいなのが入っているため、センサー自体がないということはないと思います。
ちなみにパーミッションに

Java

1 <uses-permission android:name="android.permission.BODY_SENSORS" />

を追加しているので生体センサーが使えると思っています。
デバッグ環境は adb connnectコマンドを使用したため、wifiでデバッグできるようにして、センサーチェンジイベントにブレークポイントを張って腕につけてデバッグしました。
しかし、ブレークポイントに引っかかることはありませんでした。
別個所のイベントにもブレークポイントを張ってみましたが、handleMessageイベントだけ定期的にきます。
ステップ実行して進めていくと、textView.(""+(int)hb)の後の}から出た後にRuntimeExceptionに入るのでここが原因なのかと思っています。
当方、まだあまりandroidについて詳しくないので拙い文章ですがなにとぞヒント、ご回答等お待ちしております。

以下のコードをコピーしてきました
コピー元参考サイト

MainActivity

Java

1package yokohama.mio.heartbeat; 2import android.content.res.Resources; 3import android.graphics.Bitmap; 4import android.graphics.BitmapFactory; 5import android.graphics.Color; 6import android.graphics.Typeface; 7import android.hardware.Sensor; 8import android.hardware.SensorEvent; 9import android.hardware.SensorEventListener; 10import android.hardware.SensorManager; 11import android.os.Bundle; 12import android.os.Handler; 13import android.os.Message; 14import android.support.wearable.activity.WearableActivity; 15import android.util.Log; 16import android.view.View; 17import android.widget.ImageView; 18import android.widget.LinearLayout; 19import android.widget.TextView; 20import static android.graphics.Typeface.BOLD; 21import static android.graphics.Typeface.DEFAULT_BOLD; 22public class MainActivity extends WearableActivity implements SensorEventListener{ 23 private final String TAG = MainActivity.class.getName(); 24 private SensorManager mSensorManager; 25 public float hb=100.0f; 26 private TextView textView; 27 private TextView heartTextView; 28 public View backGround; 29 public boolean isDisp=true; 30 private LoopEngine loopEngine = new LoopEngine(); 31 public ImageView imageView; 32 boolean set = false; 33 @Override 34 protected void onStart(){ 35 super.onStart(); 36 } 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_main); 41 setAmbientEnabled(); 42 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 43 textView = (TextView) findViewById(R.id.text); 44 heartTextView = (TextView) findViewById(R.id.text_heart); 45 backGround = (View) findViewById(R.id.View); 46 textView.setTextSize(20.0f); 47 heartTextView.setTextSize(0.0f); 48 loopEngine.start(); 49 //heartTextView.setTextColor(Color.argb(80, 67, 135, 233)); 50 textView.setTextColor(Color.argb(255, 140, 140, 140)); 51 textView.setTypeface(Typeface.create(DEFAULT_BOLD, BOLD)); 52 } 53 private LinearLayout.LayoutParams createParam(int w, int h){ 54 return new LinearLayout.LayoutParams(w, h); 55 } 56 @Override 57 protected void onResume() { 58 super.onResume(); 59 Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE); 60 mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); 61 } 62 @Override 63 protected void onPause(){ 64 super.onPause(); 65 mSensorManager.unregisterListener(this); 66 } 67 @Override 68 public void onSensorChanged(SensorEvent event) { 69 //ここで変数宣言すると,起動中は破棄されずメモリリークする 70 if(set==false)textView.setTextSize(60.0f); 71 if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) { 72 hb = event.values[0]; 73 textView.setText(""+(int)hb); 74 set = true; 75 } 76 } 77 @Override 78 public void onAccuracyChanged(Sensor sensor, int accuracy) { 79 Log.d(TAG,"onAccuracyChanged!!"); 80 } 81 public void update(){ 82 if(set) { 83 if (isDisp) { 84 backGround.setBackgroundColor(Color.argb(80, 231, 232, 226)); 85 //heartTextView.setTextSize(100.0f); 86 textView.setTextSize(60.0f); 87 ImageView img = (ImageView) findViewById(R.id.imageView); 88 Resources res = getResources(); 89 Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.heart); 90 // bitmapの画像を250*250で作成する 91 Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, 250, 250, false); 92 img.setImageBitmap(bitmap2); 93 } else { 94 backGround.setBackgroundColor(Color.argb(10, 231, 232, 226)); 95 //heartTextView.setTextSize(800.0f); 96 textView.setTextSize(70.0f); 97 ImageView img = (ImageView) findViewById(R.id.imageView); 98 Resources res = getResources(); 99 Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.heart); 100 // bitmapの画像を300*300で作成する 101 Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, 300, 300, false); 102 img.setImageBitmap(bitmap2); 103 } 104 } 105 isDisp = !isDisp; 106 } 107 //一定時間後にupdateを呼ぶためのオブジェクト 108 class LoopEngine extends Handler { 109 private boolean isUpdate; 110 public void start(){ 111 this.isUpdate = true; 112 handleMessage(new Message()); 113 } 114 public void stop(){ 115 this.isUpdate = false; 116 } 117 @Override 118 public void handleMessage(Message msg) { 119 this.removeMessages(0);//既存のメッセージは削除 120 if(this.isUpdate){ 121 MainActivity.this.update();//自信が発したメッセージを取得してupdateを実行 122 sendMessageDelayed(obtainMessage(0), (long)(60/hb*1000));//鼓動の間隔でメッセージを出力 123 } 124 } 125 }; 126}

androidLayout

Java

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.wearable.view.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/container" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context="yokohama.mio.heartbeat.MainActivity" 9 tools:deviceIds="wear"> 10 <View 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_alignParentEnd="true" 14 android:id="@+id/View" 15 android:layout_alignParentBottom="true" 16 android:layout_gravity="center_vertical|center_horizontal"/> 17 <ImageView 18 android:id="@+id/imageView" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:src="@drawable/heart" 22 android:scaleType="center" 23 android:contentDescription="heart" 24 android:layout_gravity="center_vertical|center_horizontal" /> 25 <TextView 26 android:id="@+id/text_heart" 27 android:layout_width="match_parent" 28 android:layout_height="match_parent" 29 app:layout_box="all" 30 android:text="♥" 31 android:gravity="center_vertical|center_horizontal" /> 32 <TextView 33 android:id="@+id/text" 34 android:layout_width="match_parent" 35 android:layout_height="match_parent" 36 app:layout_box="all" 37 android:text="心拍数測定中\nちょっと待ってね" 38 android:textSize="40sp" 39 android:gravity="center_vertical|center_horizontal" 40 android:textColor="@color/black_54p"/> 41</android.support.wearable.view.BoxInsetLayout>

追記

今再びデバッグしていたところ、センサー部分を触ると、

@Override
public void handleMessage(Message msg) {     ←ここ this.removeMessages(0);//既存のメッセージは削除 if(this.isUpdate){ MainActivity.this.update();//自信が発したメッセージを取得してupdateを実行 sendMessageDelayed(obtainMessage(0), (long)(60/hb*1000));//鼓動の間隔でメッセージを出力 } }

ここと書いてある部分に張っているブレイクポイントにひっかかります。
センサー自体は生きていますが、センサーチェンジイベントは呼ばれないのでしょうか。
それとも、RunTimeExceptionがやはり原因なのでしょうか

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

Watch側でセンサーの使用の許可が必要だったのでセンサーを許可したところ動くようになりました。

投稿2021/04/14 06:29

snowdropAPP

総合スコア41

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問