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

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

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

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

Q&A

解決済

2回答

982閲覧

GenuinoとAndroidのBLE通信時の、Android側での複数Characteristicデータの常時表示

tmn

総合スコア13

Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

0グッド

0クリップ

投稿2018/01/19 15:46

編集2018/01/23 08:11

###前提・実現したいこと
現在、AndroidアプリとGenuino101をBLEで繋ぎ、Characteristicの値を変更することで、データの送受信を行っています。
Androidアプリ側で、Genuinoから送信された複数のCharacteristicの値を同時に常時表示したいと考えています。
複数が難しければ2つの値を常時表示でも構いませんので、回答よろしくお願いいたします。

###問題点
1つの値を常時表示することには成功しています。
配布されているアプリ(nRFConnect)でGenuinoの情報を見るとGenuino側の設定は間違っていないようです。

Genuino側から複数のデータを送信しても、1つのCharacteristicの値しか表示されません。

常時表示したいCharacteristicの情報をmBluetoothGattに結び付ける必要があると思い、descriptorを複数セットしたものの、最初に宣言したもの(今回の場合はDetectionData)のみ有効になり、1つの値しか表示されません。

###該当のソースコード

java

1public class BluetoothLeService extends Service { 2/* 該当しそうな部分の抜粋です */ 3 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 4 @Override 5 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 6 String intentAction; 7 if (newState == BluetoothProfile.STATE_CONNECTED) { 8 intentAction = ACTION_GATT_CONNECTED; 9 broadcastUpdate(intentAction); 10 Log.e(TAG, "Connected to GATT server."); 11 Log.e(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); 12 13 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 14 intentAction = ACTION_GATT_DISCONNECTED; 15 Log.e(TAG, "Disconnected from GATT server."); 16 broadcastUpdate(intentAction); 17 } 18 } 19 20 @Override 21 public void onServicesDiscovered(BluetoothGatt gatt, int status) { 22 if (status == BluetoothGatt.GATT_SUCCESS) { 23 broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); 24 BluetoothGattService myService = mBluetoothGatt.getService(UUID.fromString(Genuino_UUID)); 25 26 BluetoothGattCharacteristic characteristic = myService.getCharacteristic(UUID.fromString(Reception_DetectionData)); 27 mBluetoothGatt.setCharacteristicNotification(characteristic, true); 28 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); 29 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 30 /* 追加部分 */ 31 BluetoothGattCharacteristic characteristic2 = myService.getCharacteristic(UUID.fromString(Reception_UsonicData)); 32 mBluetoothGatt.setCharacteristicNotification(characteristic2, true); 33 BluetoothGattDescriptor descriptor2 = characteristic2.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); 34 descriptor2.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 35 36 mBluetoothGatt.writeDescriptor(descriptor); 37 mBluetoothGatt.writeDescriptor(descriptor2); 38 39 } else { 40 Log.e(TAG, "onServicesDiscovered received: " + status); 41 } 42 } 43 44 @Override 45 public void onCharacteristicRead(BluetoothGatt gatt, 46 BluetoothGattCharacteristic characteristic, 47 int status) { 48 if (status == BluetoothGatt.GATT_SUCCESS) { 49 broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 50 } 51 } 52 53 @Override 54 public void onCharacteristicChanged(BluetoothGatt gatt, 55 BluetoothGattCharacteristic characteristic) { 56 broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 57 } 58 }; 59 60 private void broadcastUpdate(final String action) { 61 final Intent intent = new Intent(action); 62 sendBroadcast(intent); 63 } 64 65 private void broadcastUpdate(final String action, 66 final BluetoothGattCharacteristic characteristic) { 67 final Intent intent = new Intent(action); 68 final byte[] data = characteristic.getValue(); 69 if (data != null && data.length > 0) { 70 intent.putExtra(EXTRA_DATA, data); 71 } 72 sendBroadcast(intent); 73 }

java

1public class DeviceControlActivity extends Activity { 2 public final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 3 @Override 4 public void onReceive(Context context, Intent intent) { 5 final String action = intent.getAction(); 6 if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { 7 ConnectionStateText.setText("Connected"); 8 mConnected = true; 9 invalidateOptionsMenu(); 10 } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { 11 ConnectionStateText.setText("Disconnected"); 12 mConnected = false; 13 invalidateOptionsMenu(); 14 } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { 15 16 } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { 17 /* 受信データ表示 */ 18 byte[] rsvData = intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA); 19 if (rsvData != null) { 20 int sensorValue = convByteToInt(rsvData[0]); 21 CheckState.setText("[" + String.valueOf(sensorValue) + "]"); 22 } 23 } 24 } 25 };

Genuino側では下記のようにCharacteristicを設定し、detectionData、UsonicDataのCharacteristiの値をAndroid側に送信しています。

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service BLEUnsignedCharCharacteristic send_detectionData("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead|BLENotify); BLEUnsignedLongCharacteristic send_UsonicData("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead|BLENotify); BLEUnsignedLongCharacteristic read_selectFunction("19B10003-E8F2-537E-4F6C-D104768A1214", BLEWrite); BLEUnsignedLongCharacteristic read_threshold("19B10004-E8F2-537E-4F6C-D104768A1214", BLEWrite); BLEUnsignedLongCharacteristic read_instruction("19B10005-E8F2-537E-4F6C-D104768A1214", BLEWrite);
#include <CurieBLE.h> BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service BLEUnsignedLongCharacteristic send_detectionData("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead|BLENotify); BLEUnsignedLongCharacteristic send_UsonicData("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead|BLENotify); BLEUnsignedLongCharacteristic read_selectFunction("19B10002-E8F2-537E-4F6C-D104768A1214", BLEWrite); BLEUnsignedLongCharacteristic read_threshold("19B10003-E8F2-537E-4F6C-D104768A1214", BLEWrite); BLEUnsignedLongCharacteristic read_instruction("19B10004-E8F2-537E-4F6C-D104768A1214", BLEWrite); void setup() { Serial.begin(9600); BLE.begin(); BLE.setLocalName("BLETest"); BLE.setAdvertisedService(ledService); ledService.addCharacteristic(send_detectionData);  ledService.addCharacteristic(send_UsonicData); ledService.addCharacteristic(read_selectFunction); ledService.addCharacteristic(read_threshold); ledService.addCharacteristic(read_instruction); BLE.addService(ledService); BLE.advertise(); Serial.println("BLE LED Peripheral"); } void loop() { BLEDevice central = BLE.central(); if (central) { Serial.print("Connected to central: "); Serial.println(central.address()); while (central.connected()) { send_detectionData.setValue(1); delay(100); send_UsonicData.setValue(2); delay(100); } Serial.print(F("Disconnected from central: ")); Serial.println(central.address()); } }

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

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

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

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

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

guest

回答2

0

自己解決

Genuino側からのCharacteristicの値を、Android側で複数表示する必要がなくなったため、今まで通りGenuinoからの送信は一つのみで実行したいと思います

投稿2018/01/23 08:11

tmn

総合スコア13

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

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

0

Genuinoは純粋なarduinoとは違うものとの認識を当方ではしています。
絶対にGenuinoを使用になるのであれば、それは両者の違いを熟知する必要があると考えます。
貴殿の対象となる処理では通信(シリアル)を伴っていると予測します。

多くに方がシリアル通信(USB含む)で表記法がArduinoと異なると記載されているようです。

まず確実に目標を達成するには、単体とPCでのシリアル通信からかと考えます。
また質問においてPC(Java)側の記載はまあまあですが、Genuino側についてはトピックに見えます。

まずは主処理の動きをまとめられる事を希望します。

投稿2018/01/22 16:27

MasahikoHirata

総合スコア3747

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

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

tmn

2018/01/23 08:05

回答ありがとうございます。 Genuino側では、ArduinoIDEのサンプルCurieBLE⇒Peripheral⇒LEDに変更を加えたものを利用しています。主な変更点はUUID、Characteristic等、LED点灯部分を送信用Characteristicの書き換えに変えている点です。何も難しい動作をしていないので記載が不要かと思い、省略してしまいました。 Android側では、AndroidStudioのサンプルBluetoothLeGattを改変したものを利用しており、主処理の大きな変更はなく、Characteristicの値を分かりやすく表示するようにしているだけです。 念のためGenuino側のコードも上げて置きます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問