質問編集履歴

2

パケットキャプチャ内容とソースコード、実行結果を追加した。

2018/08/10 06:13

投稿

LS_Takao
LS_Takao

スコア13

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,34 @@
1
1
  Bluetooth Low Energyを用いてプログラムを開発しています。
2
2
 
3
- 送信側にてキャラクタリスティックを更新し、更新通知が受信側に届いていることをパケットキャプチャで確認しました。
3
+ 送信側にて64msec周期でキャラクタリスティックを更新し、更新通知が受信側に届いていることをパケットキャプチャ(nRF51 dongle + Wireshark)で確認しました。
4
+
5
+ ※受信間隔にバラツキはありますが、概ね64msec周期で受信できています。
6
+
7
+ ```
8
+
9
+ 2501 112.789743 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
10
+
11
+ 2503 112.898854 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
12
+
13
+ 2505 112.907883 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
14
+
15
+ 2507 113.017212 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
16
+
17
+ 2511 113.030233 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
18
+
19
+ 2513 113.139254 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
20
+
21
+ 2515 113.147382 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
22
+
23
+ 2519 113.266009 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
24
+
25
+ 2521 113.273321 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
26
+
27
+ 2523 113.382328 Slave_0x283eb820 Master_0x283eb820 ATT 53 Rcvd Handle Value Notification, Handle: 0x0019 (Unknown)
28
+
29
+ ```
30
+
31
+
4
32
 
5
33
  気がかりな点として、1回の更新通知でonCharacteristicChanged()が複数回呼び出されています。
6
34
 
@@ -10,7 +38,433 @@
10
38
 
11
39
 
12
40
 
41
+ 事象が発生した際のソースコードは以下の通りです。
42
+
43
+ ※SERVICE_UUID、CHARACTERISTIC_UUIDは伏せ字にしてあります。
44
+
45
+ ```Java
46
+
47
+ // import
48
+
49
+ import android.bluetooth.BluetoothAdapter;
50
+
51
+ import android.bluetooth.BluetoothDevice;
52
+
53
+ import android.bluetooth.BluetoothGatt;
54
+
55
+ import android.bluetooth.BluetoothGattCallback;
56
+
57
+ import android.bluetooth.BluetoothGattCharacteristic;
58
+
59
+ import android.bluetooth.BluetoothGattDescriptor;
60
+
61
+ import android.bluetooth.BluetoothGattService;
62
+
63
+ import android.bluetooth.BluetoothManager;
64
+
65
+ import android.bluetooth.BluetoothProfile;
66
+
67
+ import android.bluetooth.le.BluetoothLeScanner;
68
+
69
+ import android.bluetooth.le.ScanCallback;
70
+
71
+ import android.bluetooth.le.ScanResult;
72
+
73
+ import android.bluetooth.le.ScanFilter;
74
+
75
+ import android.bluetooth.le.ScanSettings;
76
+
77
+
78
+
79
+ import android.content.Context;
80
+
81
+ import android.os.ParcelUuid;
82
+
83
+ import android.support.v7.app.AppCompatActivity;
84
+
85
+ import android.os.Bundle;
86
+
87
+ import android.util.Log;
88
+
89
+
90
+
91
+ import java.util.ArrayList;
92
+
93
+ import java.util.Arrays;
94
+
95
+ import java.util.Locale;
96
+
97
+ import java.util.UUID;
98
+
99
+
100
+
101
+ import static android.bluetooth.BluetoothGatt.CONNECTION_PRIORITY_HIGH;
102
+
103
+
104
+
105
+
106
+
107
+ public class MainActivity extends AppCompatActivity {
108
+
109
+ // グローバル変数定義
110
+
111
+ private ArrayList mScanFilterList;
112
+
113
+ private ScanSettings mScanSettings;
114
+
115
+ private BluetoothLeScanner mBtLeScanner;
116
+
117
+ private BluetoothDevice mBtDevice;
118
+
119
+ private BluetoothGatt mBtGatt;
120
+
121
+ private BluetoothGattCharacteristic mBtGattCharacteristic;
122
+
123
+ private byte[] m_byReceiveData = new byte[20];
124
+
125
+ String strMsg;
126
+
127
+ long m_CntNG = 0;
128
+
129
+ long m_CntOK = 0;
130
+
131
+
132
+
133
+ // UUID定義
134
+
135
+ // 対象のサービスUUID.
136
+
137
+ private static final String SERVICE_UUID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX1";
138
+
139
+ // キャラクタリスティックUUID.
140
+
141
+ private static final String CHARACTERISTIC_UUID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX2";
142
+
143
+ // キャラクタリスティック設定UUID(固定値).
144
+
145
+ private static final String CHARACTERISTIC_UUID_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";
146
+
147
+
148
+
149
+ // 初期処理
150
+
151
+ @Override
152
+
153
+ protected void onCreate(Bundle savedInstanceState) {
154
+
155
+ super.onCreate(savedInstanceState);
156
+
157
+
158
+
159
+ BluetoothManager btManager;
160
+
161
+ BluetoothAdapter btAdapter;
162
+
163
+
164
+
165
+ setContentView(R.layout.activity_main);
166
+
167
+
168
+
169
+ // 前回受信データの初期化
170
+
171
+ for(int i=0; i<m_byReceiveData.length; i++) {
172
+
173
+ m_byReceiveData[i] = 0x00;
174
+
175
+ }
176
+
177
+
178
+
179
+ btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
180
+
181
+ btAdapter = btManager.getAdapter();
182
+
183
+
184
+
185
+ // フィルタを設定する。
186
+
187
+ ScanFilter scanFilter = new ScanFilter.Builder()
188
+
189
+ .setServiceUuid(ParcelUuid.fromString(SERVICE_UUID)).build();
190
+
191
+ mScanFilterList = new ArrayList();
192
+
193
+ mScanFilterList.add(scanFilter);
194
+
195
+
196
+
197
+ mScanSettings = new ScanSettings.Builder()
198
+
199
+ .setScanMode(ScanSettings.SCAN_MODE_BALANCED).build();
200
+
201
+
202
+
203
+ // スキャンを開始する。
204
+
205
+ mBtLeScanner = btAdapter.getBluetoothLeScanner();
206
+
207
+ mBtLeScanner.startScan(mScanFilterList, mScanSettings, mScanCallback);
208
+
209
+ }
210
+
211
+
212
+
213
+ // スキャン結果コールバック関数
214
+
215
+ private final ScanCallback mScanCallback = new ScanCallback() {
216
+
217
+ // スキャン結果通知処理
218
+
219
+ @Override
220
+
221
+ public void onScanResult(int callbackType, ScanResult result) {
222
+
223
+ super.onScanResult(callbackType, result);
224
+
225
+
226
+
227
+ // デバイスを発見した
228
+
229
+ if((null != result) && (null != result.getDevice())) {
230
+
231
+ mBtDevice = result.getDevice();
232
+
233
+
234
+
235
+ // デバイスに接続する。
236
+
237
+ mBtGatt = mBtDevice.connectGatt(getApplicationContext(), false, mGattCallback);
238
+
239
+ }
240
+
241
+ }
242
+
243
+ };
244
+
245
+
246
+
247
+ // GATTコールバック関数
248
+
249
+ private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback(){
250
+
251
+ // 接続状態変化検出処理
252
+
253
+ @Override
254
+
255
+ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){
256
+
257
+ // 接続状況が変化したら実行.
258
+
259
+ if(newState == BluetoothProfile.STATE_CONNECTED) {
260
+
261
+ // 接続に成功したらサービスを検索する.
262
+
263
+ gatt.discoverServices();
264
+
265
+ } else if(newState == BluetoothProfile.STATE_DISCONNECTED) {
266
+
267
+ // 接続が切れたらGATTを空にする.
268
+
269
+ if(mBtGatt != null){
270
+
271
+ mBtGatt.close();
272
+
273
+ mBtGatt = null;
274
+
275
+ }
276
+
277
+
278
+
279
+ // スキャンを再開する。
280
+
281
+ mBtLeScanner.startScan(mScanFilterList, mScanSettings, mScanCallback);
282
+
283
+ }
284
+
285
+ }
286
+
287
+
288
+
289
+ // サービス検出処理
290
+
291
+ @Override
292
+
293
+ public void onServicesDiscovered(BluetoothGatt gatt, int status){
294
+
295
+ BluetoothGattService bleService;
296
+
297
+ boolean bRet;
298
+
299
+ BluetoothGattDescriptor bleDescriptor;
300
+
301
+
302
+
303
+ // Serviceが見つかったら実行.
304
+
305
+ if(status == BluetoothGatt.GATT_SUCCESS) {
306
+
307
+ // UUIDが同じかどうかを確認する.
308
+
309
+ bleService = gatt.getService(UUID.fromString(SERVICE_UUID));
310
+
311
+ if(bleService != null){
312
+
313
+ // 指定したUUIDを持つCharacteristicを確認する.
314
+
315
+ mBtGattCharacteristic = bleService.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));
316
+
317
+ if(mBtGattCharacteristic != null) {
318
+
319
+ // Service, CharacteristicのUUIDが同じならBluetoothGattを更新する.
320
+
321
+ mBtGatt = gatt;
322
+
323
+
324
+
325
+ // Notifyを高速で受け取るための要求
326
+
327
+ mBtGatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH);
328
+
329
+
330
+
331
+ // キャラクタリスティックが見つかったら、Notificationをリクエスト.
332
+
333
+ bRet = mBtGatt.setCharacteristicNotification(mBtGattCharacteristic, true);
334
+
335
+
336
+
337
+ if(true == bRet) {
338
+
339
+ // Characteristic の Notificationを有効化する.
340
+
341
+ bleDescriptor = mBtGattCharacteristic.getDescriptor(UUID.fromString(CHARACTERISTIC_UUID_CONFIG));
342
+
343
+ if(null != bleDescriptor) {
344
+
345
+ bleDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
346
+
347
+ mBtGatt.writeDescriptor(bleDescriptor);
348
+
349
+ }
350
+
351
+
352
+
353
+ // scan 終了
354
+
355
+ mBtLeScanner.stopScan(mScanCallback);
356
+
357
+ }
358
+
359
+ }
360
+
361
+ }
362
+
363
+ }
364
+
365
+ }
366
+
367
+
368
+
369
+ // 更新通知受信処理
370
+
371
+ @Override
372
+
373
+ public synchronized void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
374
+
375
+ super.onCharacteristicChanged(gatt, characteristic);
376
+
377
+
378
+
379
+ byte byReceiveData[];
380
+
381
+
382
+
383
+ // キャラクタリスティックのUUIDをチェック(getUuidの結果が全て小文字で帰ってくるのでUpperCaseに変換)
384
+
385
+ if(CHARACTERISTIC_UUID.equals(characteristic.getUuid().toString().toUpperCase())){
386
+
387
+ // characteristicを取得する。
388
+
389
+ byReceiveData = characteristic.getValue();
390
+
391
+
392
+
393
+ // データ長が異なる場合は処理をスキップする。
394
+
395
+ if(byReceiveData.length != m_byReceiveData.length) {
396
+
397
+ strMsg = String.format(Locale.US, "***** Length (%d:%d) *****", byReceiveData.length, m_byReceiveData.length);
398
+
399
+ Log.e("TAG", strMsg);
400
+
401
+ return;
402
+
403
+ }
404
+
405
+
406
+
407
+ // 前回と同一データの場合は処理スキップする。
408
+
409
+ if(Arrays.equals(byReceiveData, m_byReceiveData)) {
410
+
411
+ m_CntNG++;
412
+
413
+ strMsg = String.format("xxx Receive NG(%d)", m_CntNG);
414
+
415
+ Log.e("TAG", strMsg);
416
+
417
+ return;
418
+
419
+ }
420
+
421
+ else {
422
+
423
+ m_CntOK++;
424
+
425
+ strMsg = String.format("ooo Receive OK(%d)", m_CntOK);
426
+
427
+ Log.e("TAG", strMsg);
428
+
429
+ System.arraycopy(byReceiveData, 0, m_byReceiveData, 0, m_byReceiveData.length);
430
+
431
+ }
432
+
433
+ }
434
+
435
+ }
436
+
437
+ };
438
+
439
+ }
440
+
441
+ ```
442
+
443
+ 実行結果は以下のように出力されています。
444
+
445
+ 08-10 14:34:38.108 E: ooo Receive OK(1)
446
+
447
+ 08-10 14:34:38.108 E: xxx Receive NG(1)
448
+
449
+ 08-10 14:34:38.109 E: ooo Receive OK(2)
450
+
451
+ 08-10 14:34:38.110 E: xxx Receive NG(2)
452
+
453
+ 08-10 14:34:38.156 E: ooo Receive OK(3)
454
+
455
+ 08-10 14:34:38.157 E: xxx Receive NG(3)
456
+
457
+ 08-10 14:34:38.258 E: ooo Receive OK(4)
458
+
459
+ 08-10 14:34:38.259 E: xxx Receive NG(4)
460
+
461
+ 08-10 14:34:38.316 E: ooo Receive OK(5)
462
+
463
+ 08-10 14:34:38.317 E: xxx Receive NG(5)
464
+
465
+
466
+
13
- 送信側からは64msec周期で更新通知(20bytes)を送信しています。
467
+ 送信する更新通知20byteす。
14
468
 
15
469
  送信側、受信側共にAndroid(7.0)、Bluetooth V4.2です。
16
470
 

1

コメントを追加した。

2018/08/10 06:12

投稿

LS_Takao
LS_Takao

スコア13

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,8 @@
5
5
  気がかりな点として、1回の更新通知でonCharacteristicChanged()が複数回呼び出されています。
6
6
 
7
7
  これは正常な動作(仕様)なのでしょうか?
8
+
9
+ もしくは、このような事象が発生したという方はいらっしゃいますでしょうか?
8
10
 
9
11
 
10
12