質問編集履歴

1

書式変更,質問も変更させていただきました.

2019/08/24 15:14

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,10 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 加速度センサと地磁気センサを利用してデバイスの方位を取得し,画面表示するアプリを作成したいですが,値ではなく”TextView”と表示されてしまいます
3
+ 加速度センサと地磁気センサを利用してデバイスの方位を取得し,ログを表示したいです.
4
+
4
-
5
+ しかしログには方位角がずっと0.0のままになってしまっています・
6
+
5
- 問題点が分からず困っています.
7
+ エラーメッセージがでなく,問題点が分からず困っています.
6
8
 
7
9
  初心者ですがよろしくお願いいたします.
8
10
 
@@ -18,51 +20,35 @@
18
20
 
19
21
 
20
22
 
21
- import androidx.appcompat.app.AppCompatActivity;
22
-
23
23
  import android.app.Activity;
24
24
 
25
- import android.content.pm.ActivityInfo;
26
-
27
- import android.graphics.Color;
28
-
29
25
  import android.hardware.Sensor;
30
26
 
31
27
  import android.hardware.SensorEvent;
32
28
 
33
29
  import android.hardware.SensorEventListener;
34
30
 
35
- import android.hardware.SensorListener;
36
-
37
31
  import android.hardware.SensorManager;
38
32
 
39
- import android.net.sip.SipSession;
40
-
41
33
  import android.os.Bundle;
42
34
 
43
35
  import android.util.Log;
44
36
 
45
- import android.view.View;
46
-
47
- import android.widget.Button;
48
-
49
37
  import android.widget.TextView;
50
38
 
51
39
 
52
40
 
53
-
54
-
55
- public class MainActivity extends Activity {
41
+ public class MainActivity extends Activity
42
+
43
+ implements SensorEventListener {
56
44
 
57
45
  private SensorManager sensorManager;
58
46
 
59
- private TextView TextView;
47
+ private TextView textview;
60
-
48
+
61
- private SensorEventListener Listener;
49
+ private SensorEventListener listener;
62
-
63
- private float[] faccel = null;
50
+
64
-
65
- private float[] fmagnetic = null;
51
+
66
52
 
67
53
 
68
54
 
@@ -76,123 +62,163 @@
76
62
 
77
63
  sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
78
64
 
79
- TextView = (TextView)findViewById(R.id.text_view);
80
-
81
- }
82
-
83
-
84
-
85
- @Override
86
-
87
- protected void onResume() {
88
-
89
- super.onResume();
90
-
91
- //センサー登録
92
-
93
- sensorManager.registerListener (
94
-
95
- Listener,
96
-
97
- sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
98
-
99
- SensorManager.SENSOR_DELAY_FASTEST);
100
-
101
-
102
-
103
- sensorManager.registerListener (
104
-
105
- Listener,
106
-
107
- sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
108
-
109
- SensorManager.SENSOR_DELAY_FASTEST);
110
-
111
-
65
+
66
+
67
+ }
68
+
69
+
70
+
71
+ @Override
72
+
73
+ protected void onResume() {
74
+
75
+ super.onResume();
76
+
77
+ //センサー登録
78
+
79
+ sensorManager.registerListener(
80
+
81
+ this,
82
+
83
+ sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
84
+
85
+ SensorManager.SENSOR_DELAY_UI);
86
+
87
+
88
+
89
+ sensorManager.registerListener(
90
+
91
+ this,
92
+
93
+ sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
94
+
95
+ SensorManager.SENSOR_DELAY_UI);
96
+
97
+
98
+
99
+ }
100
+
101
+
102
+
103
+ @Override
104
+
105
+ protected void onPause() {
106
+
107
+ // 解除
108
+
109
+ sensorManager.unregisterListener((SensorEventListener) this);
110
+
111
+ super.onPause();
112
+
113
+ }
114
+
115
+
116
+
117
+ public void onSensorChanged(SensorEvent event) {
118
+
119
+ float[]faccel;
120
+
121
+ float[]fmagnetic;
122
+
123
+ faccel=new float[3];
124
+
125
+ fmagnetic=new float[3];
126
+
127
+
128
+
129
+ switch (event.sensor.getType()) {
130
+
131
+ //加速度センサーの値取得
132
+
133
+ case Sensor.TYPE_ACCELEROMETER:
134
+
135
+ faccel = event.values.clone();
136
+
137
+ break;
138
+
139
+ //地磁気センサーの値取得
140
+
141
+ case Sensor.TYPE_MAGNETIC_FIELD:
142
+
143
+ fmagnetic = event.values.clone();
144
+
145
+ break;
112
146
 
113
147
  }
114
148
 
149
+
150
+
151
+ if (fmagnetic != null && faccel != null) {
152
+
153
+
154
+
155
+ float[] orientation = new float[3];
156
+
157
+ float R[] = new float[16];
158
+
159
+ float I[] = new float[16];
160
+
161
+
162
+
163
+ //加速度センサー、地磁気センサーの値を元に、回転行列を計算する
164
+
165
+ SensorManager.getRotationMatrix(R, I, faccel, fmagnetic);
166
+
167
+
168
+
169
+ //デバイスの向きに応じて回転行列を計算する
170
+
171
+ SensorManager.getOrientation(R, orientation);
172
+
173
+
174
+
175
+ //ラジアンから角度へ変換
176
+
177
+ float angle = (float) Math.floor(Math.toDegrees(orientation[0]));
178
+
179
+
180
+
181
+ //角度の範囲を0~360度へ調整
182
+
183
+ if (angle >= 0) {
184
+
185
+
186
+
115
- @Override
187
+ orientation[0] = angle;
116
-
188
+
117
- protected void onPause() {
189
+ } else if (angle < 0) {
190
+
118
-
191
+ orientation[0] = 360 + angle;
192
+
193
+ }
194
+
195
+ //得られた角度を画面へ表示
196
+
119
- // 解除
197
+ //角度を画面に表示
198
+
120
-
199
+ //String buf =
200
+
201
+ //String.format("方位角\n\t%f\n",orientation[0]);
202
+
203
+
204
+
121
- sensorManager.unregisterListener(Listener);
205
+ //textview.setText(buf);
122
-
206
+
123
- super.onPause();
207
+ Log.d("**orientationDegrees","方位角:"+orientation[0]);
124
208
 
125
209
  }
126
210
 
127
211
 
128
212
 
129
- public void onSensorChanged(SensorEvent event) {
130
-
131
- switch(event.sensor.getType()){
132
-
133
- case Sensor.TYPE_ACCELEROMETER:
134
-
135
- faccel = event.values.clone();
136
-
137
- break;
138
-
139
- case Sensor.TYPE_MAGNETIC_FIELD:
140
-
141
- fmagnetic = event.values.clone();
142
-
143
- break;
144
-
145
- }
213
+ }
214
+
215
+
216
+
146
-
217
+ @Override
147
-
148
-
218
+
149
- if (faccel != null && fmagnetic != null){
219
+ public void onAccuracyChanged(Sensor sensor, int i) {
150
-
151
- float[]orientation = new float[3];
220
+
152
-
153
- float[]R = new float[16];
221
+
154
-
155
- float[]I = new float[16];
156
-
157
- //回転行列の計算
158
-
159
- SensorManager.getRotationMatrix(R,I,faccel,fmagnetic);
160
-
161
- //向きに応じて回転行列を計算
162
-
163
- SensorManager.getOrientation(R,orientation);
164
-
165
- //ラジアンから角度に変換
166
-
167
- float angle = (float)Math.floor(Math.toDegrees(orientation[0]));
168
-
169
- //角度の範囲を0~360度に
170
-
171
- if(angle >=0){
172
-
173
- orientation[0] = angle;
174
-
175
- }
176
-
177
- else if (angle < 0){
178
-
179
- orientation[0] = 360 + angle;
180
-
181
- }
182
-
183
- //角度を画面に表示
184
-
185
- String buf =
186
-
187
- String.format("方位角\n\t%f\n",orientation[0]);
188
-
189
-
190
-
191
- TextView.setText(buf);
192
-
193
-
194
-
195
- }
196
222
 
197
223
  }
198
224