###前提・実現したいこと
Android StuioのサンプルにあるBluetoothLeGattを改変し、Genuino101とBLE通信を行うアプリを作成しています。
GenuinoのCharacteristicの値を適当な時間ごとに変化させるように設定し、Android側でNotificationをセットしています。
現在はAndroid側からGenuino側に送信はできていますが、受信した値を表示することができていません。その値をボタンを押すことなく表示したいと考えています。
Notificationの設定方法等アドバイスお願いします。
###LeGattとの主な変更点
BluetoothLeService部のonServicesDiscoverd実行時にNotificationを設定。
ControlActivity部のdisplayGattServices、servicesListClickListner等の消去。
標準で用意されている心拍数の測定・表示等に関する部分の消去。
###BluetoothLeService.javaを改変したもの(抜粋)
java
1/* resDataTextにCharacteristicの値を入れたい 2 Genuino_UUIDはサービスUUID Genuino_Readは受信UUID Genuino_Writeは送信UUIDとしています */ 3 4 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 5 @Override 6 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 7 String intentAction; 8 if (newState == BluetoothProfile.STATE_CONNECTED) { 9 intentAction = ACTION_GATT_CONNECTED; 10 broadcastUpdate(intentAction); 11 Log.e(TAG, "Connected to GATT server."); 12 Log.e(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); 13 14 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 15 intentAction = ACTION_GATT_DISCONNECTED; 16 Log.e(TAG, "Disconnected from GATT server."); 17 broadcastUpdate(intentAction); 18 } 19 } 20 21 @Override 22 public void onServicesDiscovered(BluetoothGatt gatt, int status) { 23 if (status == BluetoothGatt.GATT_SUCCESS) { 24 broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); 25 BluetoothGattService myService = mBluetoothGatt.getService(UUID.fromString(Genuino_UUID)); 26 BluetoothGattCharacteristic characteristic = myService.getCharacteristic(UUID.fromString(Genuino_Read)); 27 mBluetoothGatt.setCharacteristicNotification(characteristic, true); 28 29 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); 30 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 31 mBluetoothGatt.writeDescriptor(descriptor); 32 } else { 33 Log.e(TAG, "onServicesDiscovered received: " + status); 34 } 35 } 36 37 @Override 38 public void onCharacteristicRead(BluetoothGatt gatt, 39 BluetoothGattCharacteristic characteristic, 40 int status) { 41 if (status == BluetoothGatt.GATT_SUCCESS) { 42 broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 43 } 44 } 45 46 @Override 47 public void onCharacteristicChanged(BluetoothGatt gatt, 48 BluetoothGattCharacteristic characteristic) { 49 broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 50 } 51 }; 52 53 private void broadcastUpdate(final String action) { 54 final Intent intent = new Intent(action); 55 sendBroadcast(intent); 56 } 57 58 private void broadcastUpdate(final String action, 59 final BluetoothGattCharacteristic characteristic) { 60 final Intent intent = new Intent(action); 61 if (UUID.fromString(Genuino_Read).equals(characteristic.getUuid())) { 62 final byte[] data = characteristic.getValue(); 63 if (data != null && data.length > 0) { 64 intent.putExtra(EXTRA_DATA, data); 65 } 66 } 67 sendBroadcast(intent); 68 } 69 70 71 public void readCharacteristic(BluetoothGattCharacteristic characteristic) { 72 if (mBluetoothAdapter == null || mBluetoothGatt == null) { 73 Log.w(TAG, "BluetoothAdapter not initialized"); 74 return; 75 } 76 mBluetoothGatt.readCharacteristic(characteristic); 77 } 78 79 public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, 80 boolean enabled) { 81 if (mBluetoothAdapter == null || mBluetoothGatt == null) { 82 Log.w(TAG, "BluetoothAdapter not initialized"); 83 return; 84 } 85 mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); 86 87 if (Genuino_UUID.equals(characteristic.getUuid())) { 88 BluetoothGattDescriptor descriptor = characteristic.getDescriptor( 89 UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); 90 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 91 mBluetoothGatt.writeDescriptor(descriptor); 92 } 93 } 94 95 public void sendData(byte[] bytes){ 96 BluetoothGattService myService = mBluetoothGatt.getService(UUID.fromString(Genuino_UUID)); 97 BluetoothGattCharacteristic characteristic = myService.getCharacteristic(UUID.fromString(Genuino_Write)); 98 characteristic.setValue(bytes); 99 mBluetoothGatt.writeCharacteristic(characteristic); 100 } 101} 102
###DeviceControlActivity.javaを改変したもの(抜粋)
java
1 private final ServiceConnection mServiceConnection = new ServiceConnection() { 2 @Override 3 public void onServiceConnected(ComponentName componentName, IBinder service) { 4 mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); 5 if (!mBluetoothLeService.initialize()) { 6 Log.e(TAG, "Unable to initialize Bluetooth"); 7 finish(); 8 } 9 mBluetoothLeService.connect(mDeviceAddress); 10 } 11 @Override 12 public void onServiceDisconnected(ComponentName componentName) { 13 mBluetoothLeService = null; 14 } 15 }; 16 17 private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 18 @Override 19 public void onReceive(Context context, Intent intent) { 20 final String action = intent.getAction(); 21 if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { 22 invalidateOptionsMenu(); 23 } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { 24 invalidateOptionsMenu(); 25 } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { 26 27 } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { 28 byte[] rsvData = intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA); 29 if (rsvData != null) { 30 int sensorValue = convByteToInt(rsvData[0]); 31 resDataText.setText("[" + String.valueOf(sensorValue) + "]"); 32 } 33 } 34 } 35 }; 36 37 @Override 38 protected void onResume() { 39 super.onResume(); 40 registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); 41 if (mBluetoothLeService != null) { 42 final boolean result = mBluetoothLeService.connect(mDeviceAddress); 43 Log.d(TAG, "Connect request result=" + result); 44 } 45 } 46 47 48 private static IntentFilter makeGattUpdateIntentFilter() { 49 final IntentFilter intentFilter = new IntentFilter(); 50 intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED); 51 intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED); 52 intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED); 53 intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE); 54 return intentFilter; 55 } 56 57 class ButtonClickListener implements OnClickListener { 58 @Override 59 public void onClick(View v) { 60 switch (v.getId()) { 61 case R.id.genuino_send0: 62 byte[] off = new byte[8]; 63 off[0] = 0; 64 mBluetoothLeService.sendData(off); //送信 65 break; 66 67 case R.id.genuino_send1: 68 byte[] on = new byte[8]; 69 on[0] = 1; 70 mBluetoothLeService.sendData(on); //送信 71 break; 72 73 default: 74 break; 75 } 76 } 77 } 78} 79
###試したこと
いくつかLogで確認したところ、BluetoothLeServiceの中のbroadcastUpdate(ACTION_DATA_AVAILABLE, characteristic)がうまく機能していないことは確認ができました。
が、どういじればいいか分からない状況です。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/01 13:21 編集
2017/12/04 02:44
2017/12/04 10:11
2017/12/04 13:10