// Demonstrates how to iterate through the supported GATT // Services/Characteristics. // In this sample, we populate the data structure that is bound to the // ExpandableListView // on the UI. private void displayGattServices(List<BleGattService> gattServices) { if (gattServices == null) return; String uuid = null; String unknownServiceString = getResources().getString(R.string.unknown_service); String unknownCharaString = getResources().getString(R.string.unknown_characteristic); ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>(); ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData = new ArrayList<ArrayList<HashMap<String, String>>>(); mGattCharacteristics = new ArrayList<ArrayList<BleGattCharacteristic>>(); // Loops through available GATT Services. for (BleGattService gattService : gattServices) { HashMap<String, String> currentServiceData = new HashMap<String, String>(); uuid = gattService.getUuid().toString().toUpperCase(); currentServiceData.put( LIST_NAME, Utils.BLE_SERVICES.containsKey(uuid) ? Utils.BLE_SERVICES.get(uuid) : unknownServiceString); currentServiceData.put(LIST_UUID, uuid); gattServiceData.add(currentServiceData); ArrayList<HashMap<String, String>> gattCharacteristicGroupData = new ArrayList<HashMap<String, String>>(); List<BleGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); ArrayList<BleGattCharacteristic> charas = new ArrayList<BleGattCharacteristic>(); // Loops through available Characteristics. for (BleGattCharacteristic gattCharacteristic : gattCharacteristics) { charas.add(gattCharacteristic); HashMap<String, String> currentCharaData = new HashMap<String, String>(); uuid = gattCharacteristic.getUuid().toString().toUpperCase(); currentCharaData.put( LIST_NAME, Utils.BLE_CHARACTERISTICS.containsKey(uuid) ? Utils.BLE_CHARACTERISTICS.get(uuid) : unknownCharaString); currentCharaData.put(LIST_UUID, uuid); gattCharacteristicGroupData.add(currentCharaData); } mGattCharacteristics.add(charas); gattCharacteristicData.add(gattCharacteristicGroupData); } SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter( this, gattServiceData, android.R.layout.simple_expandable_list_item_2, new String[] {LIST_NAME, LIST_UUID}, new int[] {android.R.id.text1, android.R.id.text2}, gattCharacteristicData, android.R.layout.simple_expandable_list_item_2, new String[] {LIST_NAME, LIST_UUID}, new int[] {android.R.id.text1, android.R.id.text2}); mGattServicesList.setAdapter(gattServiceAdapter); }
@Override public boolean onChildClick( ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Log.d(TAG, "onChildClick " + groupPosition + " " + childPosition); if (mGattCharacteristics != null) { final BleGattCharacteristic characteristic = mGattCharacteristics.get(groupPosition).get(childPosition); Intent intent = new Intent(DeviceControlActivity.this, CharacteristicActivity.class); intent.putExtra("address", mDeviceAddress); Log.d(TAG, "service size " + mBle.getServices(mDeviceAddress).size()); intent.putExtra( "service", mBle.getServices(mDeviceAddress).get(groupPosition).getUuid().toString()); intent.putExtra("characteristic", characteristic.getUuid().toString().toUpperCase()); startActivity(intent); return true; } return false; }