teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

文字修正

2020/03/12 12:42

投稿

kouki-12
kouki-12

スコア7

title CHANGED
File without changes
body CHANGED
@@ -5,15 +5,15 @@
5
5
 
6
6
 
7
7
  ```java
8
- 2020-03-09 00:42:55.726 10628-10628/com.example.ta7 D/Hoge: date Discovered!
8
+ 2020-03-09 00:42:55.726 10628-10628/com.example.t D/Hoge: date Discovered!
9
- 2020-03-09 00:42:55.727 10628-10628/com.example.ta7 E/com.example.ta: No package ID 31 found for ID 0x31303336.
9
+ 2020-03-09 00:42:55.727 10628-10628/com.example.t E/com.example.ta: No package ID 31 found for ID 0x31303336.
10
- 2020-03-09 00:42:55.727 10628-10628/com.example.ta7 D/AndroidRuntime: Shutting down VM
10
+ 2020-03-09 00:42:55.727 10628-10628/com.example.t D/AndroidRuntime: Shutting down VM
11
- 2020-03-09 00:42:55.732 10628-10628/com.example.ta7 E/AndroidRuntime: FATAL EXCEPTION: main
11
+ 2020-03-09 00:42:55.732 10628-10628/com.example.t E/AndroidRuntime: FATAL EXCEPTION: main
12
- Process: com.example.ta7, PID: 10628
12
+ Process: com.example.t, PID: 10628
13
13
  android.content.res.Resources$NotFoundException: String resource ID #0x31303336
14
14
  at android.content.res.Resources.getText(Resources.java:348)
15
15
  at android.widget.Toast.makeText(Toast.java:307)
16
- at com.example.ta7.MainActivity.onNewIntent(MainActivity.java:92)
16
+ at com.example.t.MainActivity.onNewIntent(MainActivity.java:92)
17
17
  at android.app.Activity.performNewIntent(Activity.java:7149)
18
18
  at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1367)
19
19
  at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1379)
@@ -30,14 +30,14 @@
30
30
  at java.lang.reflect.Method.invoke(Native Method)
31
31
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
32
32
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
33
- 2020-03-09 00:42:55.740 10628-10628/com.example.ta7 I/Process: Sending signal. PID: 10628 SIG: 9
33
+ 2020-03-09 00:42:55.740 10628-10628/com.example.ta I/Process: Sending signal. PID: 10628 SIG: 9
34
34
  ```
35
35
 
36
36
 
37
37
  ### 該当のソースコード
38
38
  以下のコードがMainActivity.javaです。
39
39
  ```JAVa
40
- package com.example.ta7;
40
+ package com.example.t;
41
41
 
42
42
 
43
43
  import androidx.appcompat.app.AppCompatActivity;
@@ -58,8 +58,8 @@
58
58
  public class MainActivity extends AppCompatActivity {
59
59
 
60
60
  // 定数
61
- private final byte[] TARGET_SYSTEM_CODE = new byte[]{(byte) 0x80, (byte) 0x27};
61
+ private final byte[] TARGET_SYSTEM_CODE = new byte[]{(byte) 0x80, (byte) 0x22};
62
- private final byte[] TARGET_SERVICE_CODE = new byte[]{(byte) 0x9B, (byte) 0x8B};
62
+ private final byte[] TARGET_SERVICE_CODE = new byte[]{(byte) 0x9B, (byte) 0x1B};
63
63
  private final int TARGET_SIZE = 4;
64
64
 
65
65
 
@@ -135,7 +135,7 @@
135
135
  ```
136
136
  以下がNfcReader.javaです。
137
137
  ```Java
138
- package com.example.ta7;
138
+ package com.example.t;
139
139
  import android.nfc.Tag;
140
140
  import android.nfc.tech.NfcF;
141
141
  import android.util.Log;

1

Nfcreaderのプログラムを追加しました。

2020/03/12 12:42

投稿

kouki-12
kouki-12

スコア7

title CHANGED
File without changes
body CHANGED
@@ -133,7 +133,136 @@
133
133
  }
134
134
 
135
135
  ```
136
+ 以下がNfcReader.javaです。
137
+ ```Java
138
+ package com.example.ta7;
139
+ import android.nfc.Tag;
140
+ import android.nfc.tech.NfcF;
141
+ import android.util.Log;
136
142
 
143
+ import java.io.ByteArrayOutputStream;
144
+ import java.io.IOException;
145
+ import java.util.Arrays;
146
+
147
+
148
+ public class NfcReader {
149
+
150
+ public byte[][] readTag(Tag tag, byte[] targetSystemCode , byte[] targetServiceCode, int size ) {
151
+ NfcF nfc = NfcF.get(tag);
152
+ if (nfc == null) {
153
+ return null;
154
+ }
155
+
156
+ try {
157
+ nfc.connect();
158
+
159
+
160
+ // polling コマンドを作成
161
+ byte[] polling = polling(targetSystemCode );
162
+
163
+ // コマンドを送信して結果を取得
164
+ byte[] pollingRes = nfc.transceive(polling);
165
+
166
+ // System 0 のIDmを取得(1バイト目はデータサイズ、2バイト目はレスポンスコード、IDmのサイズは8バイト)
167
+ byte[] targetIDm = Arrays.copyOfRange(pollingRes, 2, 10);
168
+
169
+ Log.d("Hoge","IDM Discovered!");
170
+
171
+ // Read Without Encryption コマンドを作成
172
+ byte[] req = readWithoutEncryption(targetIDm, size, targetServiceCode);
173
+
174
+ // コマンドを送信して結果を取得
175
+ byte[] res = nfc.transceive(req);
176
+ Log.d("Hoge","Tag covered!");
177
+ nfc.close();
178
+
179
+ // 結果をパースしてデータだけ取得
180
+ return parse(res);
181
+ } catch (Exception e) {
182
+ Log.e(String.valueOf(tag), e.getMessage() , e);
183
+ }
184
+ return null;
185
+ }
186
+ /*
187
+ * Pollingコマンドの取得。
188
+ * @param systemCode byte[] 指定するシステムコード
189
+ * @return Pollingコマンド
190
+ * @throws IOException
191
+ */
192
+
193
+ private byte[] polling(byte[] targetSystemCode ) {
194
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(6);
195
+
196
+ bout.write(0x00); // データ長バイトのダミー
197
+ bout.write(0x00); // コマンドコード:0X00
198
+ bout.write(targetSystemCode [0]); // systemCode
199
+ bout.write(targetSystemCode [1]); // systemCode
200
+ bout.write(0x01); // リクエストコード:01h(システムコード要求)
201
+ bout.write(0x00); // タイムスロット
202
+
203
+ byte[] msg = bout.toByteArray();
204
+ msg[0] = (byte) msg.length; // 先頭1バイトはデータ長
205
+ return msg;
206
+ }
207
+ /*
208
+ * Read Without Encryptionコマンドの取得。
209
+ * @param idm 指定するシステムのID
210
+ * @param size 取得するデータの数
211
+ * @return Read Without Encryptionコマンド
212
+ * @throws IOException
213
+ */
214
+ private byte[] readWithoutEncryption(byte[] targetIDm, int size, byte[] targetServiceCode) throws IOException {
215
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(100);
216
+
217
+ bout.write(0x00); // データ長バイトのダミー
218
+ bout.write(0x06); // コマンドコード
219
+ bout.write(targetIDm); // IDm 8byte
220
+ bout.write(1); // サービス数の長さ(以下2バイトがこの数分繰り返す)
221
+
222
+ // サービスコードの指定はリトルエンディアンなので、下位バイトから指定します。
223
+ bout.write( targetServiceCode[1]); // サービスコード下位バイト
224
+ bout.write( targetServiceCode[0]); // サービスコード上位バイト
225
+ bout.write(size); // ブロック数
226
+
227
+ // ブロック番号の指定
228
+ for (int i = 0; i < size; i++) {
229
+ bout.write(0x80); // ブロックエレメント上位バイト 「Felicaユーザマニュアル抜粋」の4.3項参照
230
+ bout.write(i); // ブロック番号
231
+ }
232
+
233
+ byte[] msg = bout.toByteArray();
234
+ msg[0] = (byte) msg.length; // 先頭1バイトはデータ長
235
+ return msg;
236
+ }
237
+
238
+ /*
239
+ * Read Without Encryption応答の解析。
240
+ * @param res byte[]
241
+ * @return 文字列表現
242
+ */
243
+ private byte[][] parse(byte[] res) {
244
+ // res[10] エラーコード。0x00の場合が正常
245
+ if (res[10] != 0x00)
246
+ throw new RuntimeException("Read Without Encryption Command Error");
247
+
248
+ // res[12] 応答ブロック数
249
+ // res[13 + n * 16] 実データ 16(byte/ブロック)の繰り返し
250
+ int size = res[12];
251
+ byte[][] data = new byte[size][16];
252
+ for (int i = 0; i < size; i++) {
253
+ byte[] tmp = new byte[16];
254
+ int offset = 13 + i * 16;
255
+ for (int j = 0; j < 16; j++) {
256
+ tmp[j] = res[offset + j];
257
+ }
258
+
259
+ data[i] = tmp;
260
+ }
261
+ return data;
262
+ }
263
+
264
+ }
265
+ ```
137
266
  ### 試したこと
138
267
 
139
268
  エラーメッセージより以下の文章が原因と考えているのですが、どのように対策したらよいか教えてほしいです。