質問編集履歴

4

初心者アイコン

2017/11/20 16:22

投稿

tmn
tmn

スコア13

test CHANGED
File without changes
test CHANGED
File without changes

3

改善

2017/11/20 16:22

投稿

tmn
tmn

スコア13

test CHANGED
@@ -1 +1 @@
1
- ボタンを押すことなくCharacteristicの値を表示したい
1
+ ボタンを押すことなくCharacteristicの値を更新・表示したい
test CHANGED
@@ -18,7 +18,9 @@
18
18
 
19
19
 
20
20
 
21
- 実現したいことは、BLE接続後Genuinoから送られてきたCharacteristicの値を操作することなく画面上に常時表示することです。
21
+ 実現したいことは、BLE接続後Genuinoから送られてきたCharacteristicの値を操作することなく画面上に常時更新・表示することです。
22
+
23
+ 常時表示でなくても何秒かごとに更新される、といった動きでも構いません。
22
24
 
23
25
  どの部分にどのような変更が必要なのかご教授願います。
24
26
 
@@ -440,4 +442,6 @@
440
442
 
441
443
  参考にさせていただいたサイトのプログラムのうちDviceActivityに対して変更を加えています。それ以外の部分はUUIDの設定以外は触っていません。
442
444
 
445
+ 常に実行されているスレッドがあればそこに追加すればいいと思い、Logを出力して常に実行されている処理があるかを確認しようとしましたが、どの部分が常に実行されているか分かっていません。
446
+
443
447
  Genuinoに関してはArduino IDEのスケッチ例にあるCurieBLE⇒Peripheral⇒LEDに少し変更を加えたものを利用しています。

2

コードの追加

2017/11/16 11:36

投稿

tmn
tmn

スコア13

test CHANGED
@@ -1 +1 @@
1
- Characteristicの値を常時表示したい
1
+ ボタンを押すことなくCharacteristicの値を表示したい
test CHANGED
@@ -4,13 +4,21 @@
4
4
 
5
5
  http://greety.sakura.ne.jp/redo/2013/11/androidblecharacteristicsreadwrite.html
6
6
 
7
- 上記のサイトを参考にし、Android端末とGenuino 101をBluetoothで接続するアプリを作成しています。
7
+ 上記のサイトを参考にし、Android端末とGenuino 101をBLEで接続するアプリを作成しています。
8
+
8
-
9
+ 上記サイトのソースコードとの変更点は
10
+
11
+ ・onServicesDiscoveredの部分をGenuinoで設定したカスタムUUIDに対応付けさせた
12
+
13
+ ・CharacteristicReadの部分をCharacteristicの値を表示できるようにした
14
+
15
+ という2点になります。
16
+
9
- 現在はアプリ上のボタンを押すことでCharacteristicの値を変更しデータの送受信を行うことができています。
17
+ これにより、アプリ上のボタンを押すことでCharacteristicの値を読み取ることができています。
10
-
11
-
12
-
18
+
19
+
20
+
13
- Genuinoから送られてきたCharacteristicの値を操作することなく画面上に表示したい考えています。
21
+ 実現したいことは、BLE接続後Genuinoから送られてきたCharacteristicの値を操作することなく画面上に常時表示するこす。
14
22
 
15
23
  どの部分にどのような変更が必要なのかご教授願います。
16
24
 
@@ -26,8 +34,6 @@
26
34
 
27
35
  private static final String TAG = "BLEDevice";
28
36
 
29
-
30
-
31
37
  public static final String EXTRA_BLUETOOTH_DEVICE = "BT_DEVICE";
32
38
 
33
39
  private BluetoothAdapter mBTAdapter;
@@ -42,8 +48,6 @@
42
48
 
43
49
  private Button mReadGenuinoButton;
44
50
 
45
-
46
-
47
51
  private final BluetoothGattCallback mGattcallback = new BluetoothGattCallback() {
48
52
 
49
53
  @Override
@@ -52,8 +56,6 @@
52
56
 
53
57
  int newState) {
54
58
 
55
- Log.d("callback" , "チェック");
56
-
57
59
  if (newState == BluetoothProfile.STATE_CONNECTED) {
58
60
 
59
61
  mStatus = newState;
@@ -86,8 +88,6 @@
86
88
 
87
89
  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
88
90
 
89
- Log.d("ServicesDiscovered" , "チェック");
90
-
91
91
  for (BluetoothGattService service : gatt.getServices()) {
92
92
 
93
93
  if ((service == null) || (service.getUuid() == null)) {
@@ -216,7 +216,221 @@
216
216
 
217
217
 
218
218
 
219
-
219
+ @Override
220
+
221
+ protected void onResume() {
222
+
223
+ super.onResume();
224
+
225
+ init();
226
+
227
+ }
228
+
229
+
230
+
231
+ @Override
232
+
233
+ protected void onDestroy() {
234
+
235
+ super.onDestroy();
236
+
237
+ if (mConnGatt != null) {
238
+
239
+ if ((mStatus != BluetoothProfile.STATE_DISCONNECTING)
240
+
241
+ && (mStatus != BluetoothProfile.STATE_DISCONNECTED)) {
242
+
243
+ mConnGatt.disconnect();
244
+
245
+ }
246
+
247
+ mConnGatt.close();
248
+
249
+ mConnGatt = null;
250
+
251
+ }
252
+
253
+ }
254
+
255
+
256
+
257
+ @Override
258
+
259
+ public void onClick(View v) {
260
+
261
+ if (v.getId() == R.id.write_genuino_button) {
262
+
263
+ byte[] data = (mWriteGenuinoButton.isChecked())? new byte[] { (byte) 0x00} : new byte[] {(byte) 0x01};
264
+
265
+ if ((v.getTag() != null) && (v.getTag() instanceof BluetoothGattCharacteristic)) {
266
+
267
+ BluetoothGattCharacteristic ch = (BluetoothGattCharacteristic) v.getTag();
268
+
269
+ ch.setValue(data);
270
+
271
+ if (mConnGatt.writeCharacteristic(ch)) {
272
+
273
+ setProgressBarIndeterminateVisibility(true);
274
+
275
+ }
276
+
277
+ }
278
+
279
+ } else if (v.getId() == R.id.read_genuino_button) {
280
+
281
+ if ((v.getTag() != null) && (v.getTag() instanceof BluetoothGattCharacteristic)) {
282
+
283
+ BluetoothGattCharacteristic ch = (BluetoothGattCharacteristic) v.getTag();
284
+
285
+ if (mConnGatt.readCharacteristic(ch)) {
286
+
287
+ setProgressBarIndeterminateVisibility(true);
288
+
289
+ }
290
+
291
+ }
292
+
293
+ }
294
+
295
+ }
296
+
297
+
298
+
299
+ private void init() {
300
+
301
+ // BLE check
302
+
303
+ if (!BleUtil.isBLESupported(this)) {
304
+
305
+ Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT)
306
+
307
+ .show();
308
+
309
+ finish();
310
+
311
+ return;
312
+
313
+ }
314
+
315
+
316
+
317
+ // BT check
318
+
319
+ BluetoothManager manager = BleUtil.getManager(this);
320
+
321
+ if (manager != null) {
322
+
323
+ mBTAdapter = manager.getAdapter();
324
+
325
+ }
326
+
327
+ if (mBTAdapter == null) {
328
+
329
+ Toast.makeText(this, R.string.bt_unavailable, Toast.LENGTH_SHORT)
330
+
331
+ .show();
332
+
333
+ finish();
334
+
335
+ return;
336
+
337
+ }
338
+
339
+
340
+
341
+ // check BluetoothDevice
342
+
343
+ if (mDevice == null) {
344
+
345
+ mDevice = getBTDeviceExtra();
346
+
347
+ if (mDevice == null) {
348
+
349
+ finish();
350
+
351
+ return;
352
+
353
+ }
354
+
355
+ }
356
+
357
+
358
+
359
+ mWriteGenuinoButton.setEnabled(false);
360
+
361
+ mReadGenuinoButton.setEnabled(false);
362
+
363
+
364
+
365
+ // connect to Gatt
366
+
367
+ if ((mConnGatt == null)
368
+
369
+ && (mStatus == BluetoothProfile.STATE_DISCONNECTED)) {
370
+
371
+ // try to connect
372
+
373
+ mConnGatt = mDevice.connectGatt(this, false, mGattcallback);
374
+
375
+ mStatus = BluetoothProfile.STATE_CONNECTING;
376
+
377
+ } else {
378
+
379
+ if (mConnGatt != null) {
380
+
381
+ // re-connect and re-discover Services
382
+
383
+ mConnGatt.connect();
384
+
385
+ mConnGatt.discoverServices();
386
+
387
+ } else {
388
+
389
+ Log.e(TAG, "state error");
390
+
391
+ finish();
392
+
393
+ return;
394
+
395
+ }
396
+
397
+ }
398
+
399
+ setProgressBarIndeterminateVisibility(true);
400
+
401
+ }
402
+
403
+
404
+
405
+ private BluetoothDevice getBTDeviceExtra() {
406
+
407
+ Intent intent = getIntent();
408
+
409
+ if (intent == null) {
410
+
411
+ return null;
412
+
413
+ }
414
+
415
+
416
+
417
+ Bundle extras = intent.getExtras();
418
+
419
+ if (extras == null) {
420
+
421
+ return null;
422
+
423
+ }
424
+
425
+
426
+
427
+ return extras.getParcelable(EXTRA_BLUETOOTH_DEVICE);
428
+
429
+ }
430
+
431
+ }
432
+
433
+
220
434
 
221
435
  ```
222
436
 
@@ -224,6 +438,6 @@
224
438
 
225
439
  ###試したこと
226
440
 
227
- 参考にさせていただいたサイトのプログラムのうちDviceActivityに対して変更を加えています。
441
+ 参考にさせていただいたサイトのプログラムのうちDviceActivityに対して変更を加えています。それ以外の部分はUUIDの設定以外は触っていません。
228
442
 
229
443
  Genuinoに関してはArduino IDEのスケッチ例にあるCurieBLE⇒Peripheral⇒LEDに少し変更を加えたものを利用しています。

1

文法の修正

2017/11/15 14:45

投稿

tmn
tmn

スコア13

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,9 @@
10
10
 
11
11
 
12
12
 
13
- Genuinoから送られてきたCharacteristicの値を、操作することなく画面上に表示したいと考えています。どの部分にどのような変更が必要なのかご教授願います。
13
+ Genuinoから送られてきたCharacteristicの値を、操作することなく画面上に表示したいと考えています。
14
+
15
+ どの部分にどのような変更が必要なのかご教授願います。
14
16
 
15
17
 
16
18
 
@@ -20,60 +22,6 @@
20
22
 
21
23
 
22
24
 
23
- package youten.redo.ble.readwrite;
24
-
25
- import java.util.UUID;
26
-
27
- import youten.redo.ble.util.BleUtil;
28
-
29
- import youten.redo.ble.util.BleUuid;
30
-
31
- import android.app.Activity;
32
-
33
- import android.bluetooth.BluetoothAdapter;
34
-
35
- import android.bluetooth.BluetoothDevice;
36
-
37
- import android.bluetooth.BluetoothGatt;
38
-
39
- import android.bluetooth.BluetoothGattCallback;
40
-
41
- import android.bluetooth.BluetoothGattCharacteristic;
42
-
43
- import android.bluetooth.BluetoothGattService;
44
-
45
- import android.bluetooth.BluetoothManager;
46
-
47
- import android.bluetooth.BluetoothProfile;
48
-
49
- import android.content.Intent;
50
-
51
- import android.os.Bundle;
52
-
53
- import android.util.Log;
54
-
55
- import android.view.View;
56
-
57
- import android.view.Window;
58
-
59
- import android.widget.Button;
60
-
61
- import android.widget.TextView;
62
-
63
- import android.widget.Toast;
64
-
65
- import android.widget.ToggleButton;
66
-
67
-
68
-
69
- /**
70
-
71
- * BLEデバイスへのconnect・Service
72
-
73
- * Discoveryを実施し、Characteristicsのread/writeをハンドリングするActivity
74
-
75
- */
76
-
77
25
  public class DeviceActivity extends Activity implements View.OnClickListener {
78
26
 
79
27
  private static final String TAG = "BLEDevice";
@@ -194,8 +142,6 @@
194
142
 
195
143
  BluetoothGattCharacteristic characteristic, int status) {
196
144
 
197
- Log.d("CharacteristicRead" , "チェック");
198
-
199
145
  if (status == BluetoothGatt.GATT_SUCCESS) {
200
146
 
201
147
  if (BleUuid.READ_VALUE.equalsIgnoreCase(characteristic.getUuid().toString())) {
@@ -270,219 +216,7 @@
270
216
 
271
217
 
272
218
 
273
- @Override
219
+
274
-
275
- protected void onResume() {
276
-
277
- super.onResume();
278
-
279
- init();
280
-
281
- }
282
-
283
-
284
-
285
- @Override
286
-
287
- protected void onDestroy() {
288
-
289
- super.onDestroy();
290
-
291
- if (mConnGatt != null) {
292
-
293
- if ((mStatus != BluetoothProfile.STATE_DISCONNECTING)
294
-
295
- && (mStatus != BluetoothProfile.STATE_DISCONNECTED)) {
296
-
297
- mConnGatt.disconnect();
298
-
299
- }
300
-
301
- mConnGatt.close();
302
-
303
- mConnGatt = null;
304
-
305
- }
306
-
307
- }
308
-
309
-
310
-
311
- @Override
312
-
313
- public void onClick(View v) {
314
-
315
- if (v.getId() == R.id.write_genuino_button) {
316
-
317
- byte[] data = (mWriteGenuinoButton.isChecked())? new byte[] { (byte) 0x00} : new byte[] {(byte) 0x01};
318
-
319
- if ((v.getTag() != null) && (v.getTag() instanceof BluetoothGattCharacteristic)) {
320
-
321
- BluetoothGattCharacteristic ch = (BluetoothGattCharacteristic) v.getTag();
322
-
323
- ch.setValue(data);
324
-
325
- if (mConnGatt.writeCharacteristic(ch)) {
326
-
327
- setProgressBarIndeterminateVisibility(true);
328
-
329
- }
330
-
331
- }
332
-
333
- } else if (v.getId() == R.id.read_genuino_button) {
334
-
335
- if ((v.getTag() != null) && (v.getTag() instanceof BluetoothGattCharacteristic)) {
336
-
337
- BluetoothGattCharacteristic ch = (BluetoothGattCharacteristic) v.getTag();
338
-
339
- if (mConnGatt.readCharacteristic(ch)) {
340
-
341
- setProgressBarIndeterminateVisibility(true);
342
-
343
- }
344
-
345
- }
346
-
347
- }
348
-
349
- }
350
-
351
-
352
-
353
- private void init() {
354
-
355
- // BLE check
356
-
357
- if (!BleUtil.isBLESupported(this)) {
358
-
359
- Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT)
360
-
361
- .show();
362
-
363
- finish();
364
-
365
- return;
366
-
367
- }
368
-
369
-
370
-
371
- // BT check
372
-
373
- BluetoothManager manager = BleUtil.getManager(this);
374
-
375
- if (manager != null) {
376
-
377
- mBTAdapter = manager.getAdapter();
378
-
379
- }
380
-
381
- if (mBTAdapter == null) {
382
-
383
- Toast.makeText(this, R.string.bt_unavailable, Toast.LENGTH_SHORT)
384
-
385
- .show();
386
-
387
- finish();
388
-
389
- return;
390
-
391
- }
392
-
393
-
394
-
395
- // check BluetoothDevice
396
-
397
- if (mDevice == null) {
398
-
399
- mDevice = getBTDeviceExtra();
400
-
401
- if (mDevice == null) {
402
-
403
- finish();
404
-
405
- return;
406
-
407
- }
408
-
409
- }
410
-
411
-
412
-
413
- mWriteGenuinoButton.setEnabled(false);
414
-
415
- mReadGenuinoButton.setEnabled(false);
416
-
417
-
418
-
419
- // connect to Gatt
420
-
421
- if ((mConnGatt == null)
422
-
423
- && (mStatus == BluetoothProfile.STATE_DISCONNECTED)) {
424
-
425
- // try to connect
426
-
427
- mConnGatt = mDevice.connectGatt(this, false, mGattcallback);
428
-
429
- mStatus = BluetoothProfile.STATE_CONNECTING;
430
-
431
- } else {
432
-
433
- if (mConnGatt != null) {
434
-
435
- // re-connect and re-discover Services
436
-
437
- mConnGatt.connect();
438
-
439
- mConnGatt.discoverServices();
440
-
441
- } else {
442
-
443
- Log.e(TAG, "state error");
444
-
445
- finish();
446
-
447
- return;
448
-
449
- }
450
-
451
- }
452
-
453
- setProgressBarIndeterminateVisibility(true);
454
-
455
- }
456
-
457
-
458
-
459
- private BluetoothDevice getBTDeviceExtra() {
460
-
461
- Intent intent = getIntent();
462
-
463
- if (intent == null) {
464
-
465
- return null;
466
-
467
- }
468
-
469
-
470
-
471
- Bundle extras = intent.getExtras();
472
-
473
- if (extras == null) {
474
-
475
- return null;
476
-
477
- }
478
-
479
-
480
-
481
- return extras.getParcelable(EXTRA_BLUETOOTH_DEVICE);
482
-
483
- }
484
-
485
- }
486
220
 
487
221
  ```
488
222