Moved the list adapter creation out of DeviceControlActivity
This commit is contained in:
+22
-69
@@ -36,10 +36,7 @@ import android.widget.ExpandableListView;
|
|||||||
import android.widget.SimpleExpandableListAdapter;
|
import android.widget.SimpleExpandableListAdapter;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
@@ -58,8 +55,6 @@ import uk.co.alt236.btlescan.services.BluetoothLeService;
|
|||||||
public class DeviceControlActivity extends AppCompatActivity {
|
public class DeviceControlActivity extends AppCompatActivity {
|
||||||
private static final String EXTRA_DEVICE = DeviceControlActivity.class.getName() + ".EXTRA_DEVICE";
|
private static final String EXTRA_DEVICE = DeviceControlActivity.class.getName() + ".EXTRA_DEVICE";
|
||||||
private final static String TAG = DeviceControlActivity.class.getSimpleName();
|
private final static String TAG = DeviceControlActivity.class.getSimpleName();
|
||||||
private static final String LIST_NAME = "NAME";
|
|
||||||
private static final String LIST_UUID = "UUID";
|
|
||||||
@Bind(R.id.gatt_services_list)
|
@Bind(R.id.gatt_services_list)
|
||||||
protected ExpandableListView mGattServicesList;
|
protected ExpandableListView mGattServicesList;
|
||||||
@Bind(R.id.connection_state)
|
@Bind(R.id.connection_state)
|
||||||
@@ -75,7 +70,7 @@ public class DeviceControlActivity extends AppCompatActivity {
|
|||||||
private Exporter mExporter;
|
private Exporter mExporter;
|
||||||
private BluetoothGattCharacteristic mNotifyCharacteristic;
|
private BluetoothGattCharacteristic mNotifyCharacteristic;
|
||||||
private BluetoothLeService mBluetoothLeService;
|
private BluetoothLeService mBluetoothLeService;
|
||||||
private List<List<BluetoothGattCharacteristic>> mGattCharacteristics = new ArrayList<>();
|
|
||||||
// If a given GATT characteristic is selected, check for supported features. This sample
|
// If a given GATT characteristic is selected, check for supported features. This sample
|
||||||
// demonstrates 'Read' and 'Notify' features. See
|
// demonstrates 'Read' and 'Notify' features. See
|
||||||
// http://d.android.com/reference/android/bluetooth/BluetoothGatt.html for the complete
|
// http://d.android.com/reference/android/bluetooth/BluetoothGatt.html for the complete
|
||||||
@@ -83,25 +78,27 @@ public class DeviceControlActivity extends AppCompatActivity {
|
|||||||
private final ExpandableListView.OnChildClickListener servicesListClickListner = new ExpandableListView.OnChildClickListener() {
|
private final ExpandableListView.OnChildClickListener servicesListClickListner = new ExpandableListView.OnChildClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onChildClick(final ExpandableListView parent, final View v, final int groupPosition, final int childPosition, final long id) {
|
public boolean onChildClick(final ExpandableListView parent, final View v, final int groupPosition, final int childPosition, final long id) {
|
||||||
if (mGattCharacteristics != null) {
|
final GattDataAdapterFactory.GattDataAdapter adapter =
|
||||||
final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(groupPosition).get(childPosition);
|
(GattDataAdapterFactory.GattDataAdapter) parent.getAdapter();
|
||||||
final int charaProp = characteristic.getProperties();
|
|
||||||
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
|
final BluetoothGattCharacteristic characteristic =
|
||||||
// If there is an active notification on a characteristic, clear
|
adapter.getBluetoothGattCharacteristic(groupPosition, childPosition);
|
||||||
// it first so it doesn't update the data field on the user interface.
|
|
||||||
if (mNotifyCharacteristic != null) {
|
final int charaProp = characteristic.getProperties();
|
||||||
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
|
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
|
||||||
mNotifyCharacteristic = null;
|
// If there is an active notification on a characteristic, clear
|
||||||
}
|
// it first so it doesn't update the data field on the user interface.
|
||||||
mBluetoothLeService.readCharacteristic(characteristic);
|
if (mNotifyCharacteristic != null) {
|
||||||
|
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
|
||||||
|
mNotifyCharacteristic = null;
|
||||||
}
|
}
|
||||||
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
|
mBluetoothLeService.readCharacteristic(characteristic);
|
||||||
mNotifyCharacteristic = characteristic;
|
|
||||||
mBluetoothLeService.setCharacteristicNotification(characteristic, true);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
|
||||||
|
mNotifyCharacteristic = characteristic;
|
||||||
|
mBluetoothLeService.setCharacteristicNotification(characteristic, true);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -179,52 +176,8 @@ public class DeviceControlActivity extends AppCompatActivity {
|
|||||||
mDevice.getAddress(),
|
mDevice.getAddress(),
|
||||||
gattServices);
|
gattServices);
|
||||||
|
|
||||||
String uuid = null;
|
final GattDataAdapterFactory.GattDataAdapter adapter = GattDataAdapterFactory.createAdapter(this, gattServices);
|
||||||
final String unknownServiceString = getResources().getString(R.string.unknown_service);
|
mGattServicesList.setAdapter(adapter);
|
||||||
final String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
|
|
||||||
final List<Map<String, String>> gattServiceData = new ArrayList<>();
|
|
||||||
final List<List<Map<String, String>>> gattCharacteristicData = new ArrayList<>();
|
|
||||||
mGattCharacteristics = new ArrayList<>();
|
|
||||||
|
|
||||||
// Loops through available GATT Services.
|
|
||||||
for (final BluetoothGattService gattService : gattServices) {
|
|
||||||
final Map<String, String> currentServiceData = new HashMap<>();
|
|
||||||
uuid = gattService.getUuid().toString();
|
|
||||||
currentServiceData.put(LIST_NAME, GattAttributeResolver.getAttributeName(uuid, unknownServiceString));
|
|
||||||
currentServiceData.put(LIST_UUID, uuid);
|
|
||||||
gattServiceData.add(currentServiceData);
|
|
||||||
|
|
||||||
final List<Map<String, String>> gattCharacteristicGroupData = new ArrayList<>();
|
|
||||||
final List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
|
|
||||||
final List<BluetoothGattCharacteristic> charas = new ArrayList<>();
|
|
||||||
|
|
||||||
// Loops through available Characteristics.
|
|
||||||
for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
|
|
||||||
charas.add(gattCharacteristic);
|
|
||||||
final Map<String, String> currentCharaData = new HashMap<>();
|
|
||||||
uuid = gattCharacteristic.getUuid().toString();
|
|
||||||
currentCharaData.put(LIST_NAME, GattAttributeResolver.getAttributeName(uuid, unknownCharaString));
|
|
||||||
currentCharaData.put(LIST_UUID, uuid);
|
|
||||||
gattCharacteristicGroupData.add(currentCharaData);
|
|
||||||
}
|
|
||||||
|
|
||||||
mGattCharacteristics.add(charas);
|
|
||||||
gattCharacteristicData.add(gattCharacteristicGroupData);
|
|
||||||
}
|
|
||||||
|
|
||||||
final 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);
|
|
||||||
invalidateOptionsMenu();
|
invalidateOptionsMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package uk.co.alt236.btlescan.ui.control;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||||||
|
import android.bluetooth.BluetoothGattService;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.widget.SimpleExpandableListAdapter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import uk.co.alt236.bluetoothlelib.resolvers.GattAttributeResolver;
|
||||||
|
import uk.co.alt236.btlescan.R;
|
||||||
|
|
||||||
|
/*package*/ class GattDataAdapterFactory {
|
||||||
|
private static final String LIST_NAME = "NAME";
|
||||||
|
private static final String LIST_UUID = "UUID";
|
||||||
|
|
||||||
|
public static GattDataAdapter createAdapter(final Context context,
|
||||||
|
final List<BluetoothGattService> gattServices) {
|
||||||
|
|
||||||
|
|
||||||
|
final String unknownServiceString = context.getString(R.string.unknown_service);
|
||||||
|
final String unknownCharaString = context.getString(R.string.unknown_characteristic);
|
||||||
|
final List<Map<String, String>> gattServiceData = new ArrayList<>();
|
||||||
|
final List<List<Map<String, String>>> gattCharacteristicData = new ArrayList<>();
|
||||||
|
final List<List<BluetoothGattCharacteristic>> fullGattCharacteristics = new ArrayList<>();
|
||||||
|
|
||||||
|
// Loops through available GATT Services.
|
||||||
|
String uuid;
|
||||||
|
for (final BluetoothGattService gattService : gattServices) {
|
||||||
|
final Map<String, String> currentServiceData = new HashMap<>();
|
||||||
|
uuid = gattService.getUuid().toString();
|
||||||
|
currentServiceData.put(LIST_NAME, GattAttributeResolver.getAttributeName(uuid, unknownServiceString));
|
||||||
|
currentServiceData.put(LIST_UUID, uuid);
|
||||||
|
gattServiceData.add(currentServiceData);
|
||||||
|
|
||||||
|
final List<Map<String, String>> gattCharacteristicGroupData = new ArrayList<>();
|
||||||
|
final List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
|
||||||
|
final List<BluetoothGattCharacteristic> charas = new ArrayList<>();
|
||||||
|
|
||||||
|
// Loops through available Characteristics.
|
||||||
|
for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
|
||||||
|
charas.add(gattCharacteristic);
|
||||||
|
final Map<String, String> currentCharaData = new HashMap<>();
|
||||||
|
uuid = gattCharacteristic.getUuid().toString();
|
||||||
|
currentCharaData.put(LIST_NAME, GattAttributeResolver.getAttributeName(uuid, unknownCharaString));
|
||||||
|
currentCharaData.put(LIST_UUID, uuid);
|
||||||
|
gattCharacteristicGroupData.add(currentCharaData);
|
||||||
|
}
|
||||||
|
|
||||||
|
fullGattCharacteristics.add(charas);
|
||||||
|
gattCharacteristicData.add(gattCharacteristicGroupData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new GattDataAdapter(
|
||||||
|
context,
|
||||||
|
fullGattCharacteristics,
|
||||||
|
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}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class GattDataAdapter extends SimpleExpandableListAdapter {
|
||||||
|
|
||||||
|
private final List<List<BluetoothGattCharacteristic>> mGattCharacteristics;
|
||||||
|
|
||||||
|
public GattDataAdapter(Context context,
|
||||||
|
List<List<BluetoothGattCharacteristic>> gattCharacteristics,
|
||||||
|
List<Map<String, String>> groupData,
|
||||||
|
int groupLayout, String[] groupFrom,
|
||||||
|
int[] groupTo,
|
||||||
|
List<List<Map<String, String>>> childData,
|
||||||
|
int childLayout,
|
||||||
|
String[] childFrom,
|
||||||
|
int[] childTo) {
|
||||||
|
|
||||||
|
super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
|
||||||
|
mGattCharacteristics = gattCharacteristics;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BluetoothGattCharacteristic getBluetoothGattCharacteristic(final int groupPosition, final int childPosition) {
|
||||||
|
return mGattCharacteristics.get(groupPosition).get(childPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user