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

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

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

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

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

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

Q&A

0回答

2586閲覧

AndroidのBLE通信について

vike

総合スコア17

Bluetooth

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

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

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

0グッド

0クリップ

投稿2017/04/25 09:02

AndroidのBLE通信について質問です。
現在AndroidのアプリとSONYのMESHを接続しようと考えています。
しかしBLEについて全く知識がないので四苦八苦しています。

この下のコードを用いてLEDタグに信号を送り光らせたり、ボタンタグから信号を受け取り表示させるにはどのようにすれば良いでしょうか?

java

1 private final static int MESSAGE_NEW_RECEIVEDNUM = 0; 2 private final static int MESSAGE_NEW_SENDNUM = 1; 3 private final static int REQUEST_ENABLE_BT = 123456; 4 private BluetoothManager mBleManager; 5 private BluetoothAdapter mBleAdapter; 6 private boolean mIsBluetoothEnable = false; 7 private BluetoothLeScanner mBleScanner; 8 private BluetoothGatt mBleGatt; 9 private BluetoothGattCharacteristic mBleCharacteristic; 10 private TextView mTxtReceivedNum; 11 private TextView mTxtSendNum; 12 private String mStrReceivedNum = ""; 13 private String mStrSendNum = ""; 14 15 // 対象のサービスUUID. 16 private static final String SERVICE_UUID = "72C90001-57A9-4D40-B746-534E22EC9F9E"; 17 // キャラクタリスティックUUID. 18 private static final String CHARACTERISTIC_UUID = "72C90003-57A9-4D40-B746-534E22EC9F9E"; 19 // キャラクタリスティック設定UUID(固定値). 20 private static final String CHARACTERISTIC_CONFIG_UUID = "00002902-0000-1000-8000-00805f9b34fb"; 21 22 // 乱数送信用. 23 private Random mRandom = new Random(); 24 private Timer mTimer; 25 private SendDataTimer mSendDataTimer; 26 27 private final LeScanCallback mScanCallback = new BluetoothAdapter.LeScanCallback() { 28 @Override 29 public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { 30 runOnUiThread(new Runnable() { 31 @Override 32 public void run() { 33 // スキャン中に見つかったデバイスに接続を試みる.第三引数には接続後に呼ばれるBluetoothGattCallbackを指定する. 34 mBleGatt = device.connectGatt(getApplicationContext(), false, mGattCallback); 35 } 36 }); 37 } 38 }; 39 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 40 @Override 41 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) 42 { 43 // 接続状況が変化したら実行. 44 if (newState == BluetoothProfile.STATE_CONNECTED) { 45 // 接続に成功したらサービスを検索する. 46 gatt.discoverServices(); 47 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 48 // 接続が切れたらGATTを空にする. 49 if (mBleGatt != null) 50 { 51 mBleGatt.close(); 52 mBleGatt = null; 53 } 54 mIsBluetoothEnable = false; 55 } 56 } 57 @Override 58 public void onServicesDiscovered(BluetoothGatt gatt, int status) 59 { 60 // Serviceが見つかったら実行. 61 if (status == BluetoothGatt.GATT_SUCCESS) { 62 // UUIDが同じかどうかを確認する. 63 BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_UUID)); 64 if (service != null) 65 { 66 // 指定したUUIDを持つCharacteristicを確認する. 67 mBleCharacteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID)); 68 69 if (mBleCharacteristic != null) { 70 // Service, CharacteristicのUUIDが同じならBluetoothGattを更新する. 71 mBleGatt = gatt; 72 73 // キャラクタリスティックが見つかったら、Notificationをリクエスト. 74 boolean registered = mBleGatt.setCharacteristicNotification(mBleCharacteristic, true); 75 76 // Characteristic の Notificationを有効化する. 77 BluetoothGattDescriptor descriptor = mBleCharacteristic.getDescriptor( 78 UUID.fromString(CHARACTERISTIC_CONFIG_UUID)); 79 80 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 81 mBleGatt.writeDescriptor(descriptor); 82 // 接続が完了したらデータ送信を開始する. 83 mIsBluetoothEnable = true; 84 } 85 } 86 } 87 } 88 @Override 89 public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 90 { 91 // キャラクタリスティックのUUIDをチェック(getUuidの結果が全て小文字で帰ってくるのでUpperCaseに変換) 92 if (CHARACTERISTIC_UUID.equals(characteristic.getUuid().toString().toUpperCase())) 93 { 94 // Peripheralで値が更新されたらNotificationを受ける. 95 mStrReceivedNum = characteristic.getStringValue(0); 96 // メインスレッドでTextViewに値をセットする. 97 mBleHandler.sendEmptyMessage(MESSAGE_NEW_RECEIVEDNUM); 98 } 99 } 100 }; 101 private Handler mBleHandler = new Handler() 102 { 103 public void handleMessage(Message msg) 104 { 105 // UIスレッドで実行する処理. 106 switch (msg.what) 107 { 108 case MESSAGE_NEW_RECEIVEDNUM: 109 mTxtReceivedNum.setText(mStrReceivedNum); 110 break; 111 case MESSAGE_NEW_SENDNUM: 112 mTxtSendNum.setText(mStrSendNum); 113 break; 114 } 115 } 116 }; 117 118 @Override 119 protected void onCreate(Bundle savedInstanceState) { 120 super.onCreate(savedInstanceState); 121 setContentView(R.layout.activity_main); 122 123 mIsBluetoothEnable = false; 124 125 // Writeリクエストで送信する値、Notificationで受け取った値をセットするTextView. 126 mTxtReceivedNum = (TextView) findViewById(R.id.received_num); 127 mTxtSendNum = (TextView) findViewById(R.id.send_num); 128 129 // Bluetoothの使用準備. 130 mBleManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 131 mBleAdapter = mBleManager.getAdapter(); 132 133 // Writeリクエスト用のタイマーをセット. 134 mTimer = new Timer(); 135 mSendDataTimer = new SendDataTimer(); 136 // 第二引数:最初の処理までのミリ秒 第三引数:以降の処理実行の間隔(ミリ秒). 137 mTimer.schedule(mSendDataTimer, 500, 1000); 138 139 // BluetoothがOffならインテントを表示する. 140 if ((mBleAdapter == null) 141 || (! mBleAdapter.isEnabled())) { 142 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 143 // Intentでボタンを押すとonActivityResultが実行されるので、第二引数の番号を元に処理を行う. 144 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 145 } 146 else 147 { 148 // BLEが使用可能ならスキャン開始. 149 this.scanNewDevice(); 150 } 151 } 152 @Override 153 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 154 // Intentでユーザーがボタンを押したら実行. 155 super.onActivityResult(requestCode, resultCode, data); 156 switch (requestCode) { 157 case REQUEST_ENABLE_BT: 158 if ((mBleAdapter != null) 159 || (mBleAdapter.isEnabled())) { 160 // BLEが使用可能ならスキャン開始. 161 this.scanNewDevice(); 162 } 163 break; 164 } 165 } 166 private void scanNewDevice() 167 { 168 // OS ver.5.0以上ならBluetoothLeScannerを使用する. 169 if (Build.VERSION.SDK_INT >= SDKVER_LOLLIPOP) 170 { 171 this.startScanByBleScanner(); 172 } 173 else 174 { 175 // デバイスの検出. 176 mBleAdapter.startLeScan(mScanCallback); 177 } 178 } 179 @TargetApi(SDKVER_LOLLIPOP) 180 private void startScanByBleScanner() 181 { 182 mBleScanner = mBleAdapter.getBluetoothLeScanner(); 183 // デバイスの検出. 184 mBleScanner.startScan(new ScanCallback() { 185 @Override 186 public void onScanResult(int callbackType, ScanResult result) { 187 super.onScanResult(callbackType, result); 188 // スキャン中に見つかったデバイスに接続を試みる.第三引数には接続後に呼ばれるBluetoothGattCallbackを指定する. 189 result.getDevice().connectGatt(getApplicationContext(), false, mGattCallback); 190 } 191 @Override 192 public void onScanFailed(int intErrorCode) 193 { 194 super.onScanFailed(intErrorCode); 195 } 196 }); 197 } 198 public class SendDataTimer extends TimerTask{ 199 @Override 200 public void run() { 201 if(mIsBluetoothEnable) 202 { 203 // 設定時間ごとに0~999までの乱数を作成. 204 mStrSendNum = String.valueOf(mRandom.nextInt(100)); 205 // UIスレッドで生成した数をTextViewにセット. 206 mBleHandler.sendEmptyMessage(MESSAGE_NEW_SENDNUM); 207 // キャラクタリスティックに値をセットして、Writeリクエストを送信. 208 mBleCharacteristic.setValue(mStrSendNum); 209 mBleGatt.writeCharacteristic(mBleCharacteristic); 210 } 211 } 212 } 213 @Override 214 protected void onDestroy() 215 { 216 // 画面遷移時は通信を切断する. 217 mIsBluetoothEnable = false; 218 if(mBleGatt != null) { 219 mBleGatt.close(); 220 mBleGatt = null; 221 } 222 super.onDestroy(); 223 } 224} 225

回答よろしくお願いします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問