BLE 経由で Android デバイスを介して µ-controller と通信しようとしています。カスタム特性 (Dev-Kit の LED を切り替える) を書き込むことはできますが、LED ステータスの値 (オフの場合は「0x00」、オンの場合は「0x01」) を読み取ることができません。
アイテムをクリックしたときに ExpandableListView をクリックしたときに読みたいです。今のところ、onChildClickListener 内に実装しました。特性許可の場合 "PROPERTY_WRITE" > 0
、値を書き込む必要があります。
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
mBluetoothLeService.readCustomCharacteristic();
mBluetoothLeService.writeCustomCharacteristic(0x01);
}
return true;
}
return false;
}
しかし、常に特性の読み取りに失敗します。LED 値が書き込まれ、LED は「ON」に切り替わりますが、LED の値は読み取られません。リスト内の特性をクリックして、LED のオン/オフを切り替えるための値を読み取りたいです。
何が間違っているのかわかりません。特性を読み取り、テキストフィールドに書き込む必要があります。私のBLEデバイスでは、読み取りを有効にしました。を使用して、ubuntu-terminal で値を読み取ることができますgatttool -> char-read-hnd [hnd] [val]
。
これはreadCustomCharacteristic()
mBluetoothLeServiceの私の実装です
public void readCustomCharacteristic() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("edfec62e-9910-0bac-5241-d8bda6932a2f"));
if(mCustomService == null){
Log.w(TAG, "Custom BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("18192021-2223-2425-2627-282930313233"));
mBluetoothGatt.readCharacteristic(mReadCharacteristic);
if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){
Log.w(TAG, "Failed to read characteristic");
}
return;
}