Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,22 @@
*/
public class ConnectedShimmersListFragment extends ListFragment {

OnShimmerDeviceSelectedListener mCallBack;
String selectedDeviceAddress, selectedDeviceName;
final static String LOG_TAG = "SHIMMER";
OnShimmerDeviceSelectedListener mCallBack;
String selectedDeviceAddress;
ListView savedListView = null;
ArrayAdapter<String> savedListAdapter = null;
int selectedItemPos = -1;
List<ShimmerDevice> shimmerDeviceList;
Context context;
int selectedDevicePos = -1;


public ConnectedShimmersListFragment() {
// Required empty public constructor
}

//Container Activity must implement this interface
public interface OnShimmerDeviceSelectedListener {
public void onShimmerDeviceSelected(String macAddress, String deviceName);
public void onShimmerDeviceSelected(String macAddress, String deviceName, Boolean selected);
}

@Override
Expand All @@ -66,72 +64,71 @@ public static ConnectedShimmersListFragment newInstance() {

public void buildShimmersConnectedListView(final List<ShimmerDevice> deviceList, final Context context) {
if(isVisible()){
shimmerDeviceList = deviceList;
this.context = context;
if(deviceList == null) {
//String[] displayList = {"Service not yet initialised"};
String[] displayList = {"No devices connected"};
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, displayList);
setListAdapter(listAdapter);
}
else {
final String[] nameList = new String[deviceList.size()];
final String[] macList = new String[deviceList.size()];
final String[] displayList = new String[deviceList.size()];

for (int i = 0; i < nameList.length; i++) {
nameList[i] = deviceList.get(i).getShimmerUserAssignedName();
macList[i] = deviceList.get(i).getMacId();
displayList[i] = nameList[i] + "\n" + macList[i];
shimmerDeviceList = deviceList;
this.context = context;
if(deviceList == null) {
//String[] displayList = {"Service not yet initialised"};
String[] displayList = {"No devices connected"};
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, displayList);
setListAdapter(listAdapter);
}
else {
final String[] nameList = new String[deviceList.size()];
final String[] macList = new String[deviceList.size()];
final String[] displayList = new String[deviceList.size()];

for (int i = 0; i < nameList.length; i++) {
nameList[i] = deviceList.get(i).getShimmerUserAssignedName();
macList[i] = deviceList.get(i).getMacId();
displayList[i] = nameList[i] + "\n" + macList[i];
}

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(context, R.layout.simple_list_item_multiple_choice_force_black_text, displayList);

//Set the list of devices to be displayed in the Fragment
setListAdapter(listAdapter);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(context, R.layout.simple_list_item_multiple_choice_force_black_text, displayList);

final ListView listView = getListView();
listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedItemPos = position;
//Set the list of devices to be displayed in the Fragment
setListAdapter(listAdapter);

selectedDeviceAddress = macList[position];
selectedDeviceName = nameList[position];
selectedDevicePos = position;
final ListView listView = getListView();
listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(selectedDeviceAddress != null && selectedDeviceAddress == macList[position]){
selectedDeviceAddress = null;
selectedDevicePos = -1;
}
else{
selectedDeviceAddress = macList[position];
selectedDevicePos = position;
}

try {
mCallBack.onShimmerDeviceSelected(macList[position], nameList[position]);
} catch (ClassCastException cce) {
try {
mCallBack.onShimmerDeviceSelected(macList[position], nameList[position], selectedDevicePos == -1 ? false : true);
} catch (ClassCastException cce) {

}
}
}
});
});

//Save the listView so that it can be restored in onCreateView when returning to the Fragment.
savedListView = listView;
savedListAdapter = listAdapter;
//Save the listView so that it can be restored in onCreateView when returning to the Fragment.
savedListView = listView;
savedListAdapter = listAdapter;

//Ensure that the selected item's checkbox is checked
if (selectedDeviceAddress != null) {
//Ensure that the selected item's checkbox is checked
for (int i = 0; i < listView.getAdapter().getCount(); i++) {

View view = getViewByPosition(i, listView);
CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(android.R.id.text1);
if (checkedTextView != null) {
String text = checkedTextView.getText().toString();
if (text.contains(selectedDeviceAddress)) {
listView.setItemChecked(i, true);
} else {
if (selectedDeviceAddress == null || !text.contains(selectedDeviceAddress)) {
listView.setItemChecked(i, false);
}
else {
listView.setItemChecked(i, true);
}
}

}
}

}
}
}

Expand All @@ -157,6 +154,11 @@ public View getViewByPosition(int pos, ListView listView) {
}
}

public void removeSelectedDevice(){
selectedDeviceAddress = null;
selectedDevicePos = -1;
}

public int getNumShimmersConnected() {
if(shimmerDeviceList != null) {
return shimmerDeviceList.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import com.shimmerresearch.androidinstrumentdriver.R;
Expand All @@ -20,6 +21,7 @@ public class DataSyncFragment extends Fragment {
public static TextView TextViewPayloadIndex;
public static TextView TextViewSpeed;
public static TextView TextViewDirectory;
public static Button ButtonDataSync;

public DataSyncFragment() {
// Required empty public constructor
Expand All @@ -42,13 +44,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {

ButtonDataSync = (Button) getView().findViewById(R.id.dataSyncButton);
editTextParticipantName = (EditText) getView().findViewById(R.id.participantName);
editTextTrialName = (EditText) getView().findViewById(R.id.trialName);
TextViewPayloadIndex = (TextView) getView().findViewById(R.id.payloadIndex);
TextViewSpeed = (TextView) getView().findViewById(R.id.speed);
TextViewDirectory = (TextView) getView().findViewById(R.id.directory);

ButtonDataSync.setVisibility(View.GONE);
super.onActivityCreated(savedInstanceState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.shimmerresearch.driverUtilities.SensorDetails;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -48,10 +49,14 @@ public void buildDeviceConfigList(final ShimmerDevice shimmerDevice, final Conte
final Map<String, ConfigOptionDetailsSensor> configOptionsMap = shimmerDevice.getConfigOptionsMap();
shimmerDeviceClone = shimmerDevice.deepClone();
Map<Integer, SensorDetails> sensorMap = shimmerDevice.getSensorMap();
List<String> listOfKeys = new ArrayList<String>();
final List<String> listOfKeys = new ArrayList<String>();
for (SensorDetails sd:sensorMap.values()) {
if (sd.mSensorDetailsRef.mListOfConfigOptionKeysAssociated!=null && sd.isEnabled()) {
listOfKeys.addAll(sd.mSensorDetailsRef.mListOfConfigOptionKeysAssociated);
for(String configOptionKey:sd.mSensorDetailsRef.mListOfConfigOptionKeysAssociated){
if(!listOfKeys.contains(configOptionKey)){
listOfKeys.add(configOptionKey);
}
}
}
}

Expand Down Expand Up @@ -135,9 +140,10 @@ public boolean onChildClick(ExpandableListView parent, View v, int groupPosition
if(expandListView.getFooterViewsCount() == 0) {
LinearLayout buttonLayout = new LinearLayout(context);
buttonLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
buttonLayout.setOrientation(LinearLayout.VERTICAL);
Button writeConfigButton = new Button(context);
Button resetListButton = new Button(context);
Button setDefaultConfigButton = new Button(context);
writeConfigButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -161,10 +167,39 @@ public void onClick(View v) {
Toast.makeText(context, "Settings have been reset", Toast.LENGTH_SHORT).show();
}
});
setDefaultConfigButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Map<String, Integer> defaultConfigMap = new HashMap<String, Integer>();
defaultConfigMap.put("Accel_Range", 3);
defaultConfigMap.put("Gyro_Range", 3);
defaultConfigMap.put("Accel_Rate", 2);
defaultConfigMap.put("Mode", 1);
defaultConfigMap.put("Accel_Gyro_Rate", 6);
defaultConfigMap.put("LP Mode", 1);
defaultConfigMap.put("Range", 3);
defaultConfigMap.put("PPG Rate", 3);

shimmerDeviceClone = shimmerDevice.deepClone();
for(String key : listOfKeys) {
if(defaultConfigMap.containsKey(key)){
final ConfigOptionDetailsSensor cods = configOptionsMap.get(key);
if(cods != null){
shimmerDeviceClone.setConfigValueUsingConfigLabel(key, cods.mConfigValues[defaultConfigMap.get(key)]);
}
}
}
expandListAdapter.updateCloneDevice(shimmerDeviceClone);
expandListAdapter.notifyDataSetChanged();
Toast.makeText(context, "Settings have been set to default", Toast.LENGTH_SHORT).show();
}
});
writeConfigButton.setText("Write config to Shimmer");
resetListButton.setText("Reset settings");
setDefaultConfigButton.setText("Set default config");
buttonLayout.addView(resetListButton);
buttonLayout.addView(writeConfigButton);
buttonLayout.addView(setDefaultConfigButton);
expandListView.addFooterView(buttonLayout);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

import bolts.TaskCompletionSource;

/**
* Each instance of this class represents a ble radio that is used to communicate with a verisense device
*/
public class AndroidBleRadioByteCommunication extends AbstractByteCommunication {
BleDevice mBleDevice;
String TxID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E";
Expand All @@ -40,11 +43,19 @@ public class AndroidBleRadioByteCommunication extends AbstractByteCommunication

//"DA:A6:19:F0:4A:D7"
//"E7:45:2C:6D:6F:14"
/**
* Initialize a ble radio
* @param mac mac address of the verisense device e.g. d0:2b:46:3d:a2:bb
*/
public AndroidBleRadioByteCommunication(String mac) {
mMac = mac;
}
TaskCompletionSource<String> mTaskConnect = new TaskCompletionSource<>();
TaskCompletionSource<String> mTaskMTU = new TaskCompletionSource<>();

/**
* Connect to the verisense device
*/
@Override
public void connect() {
mTaskConnect = new TaskCompletionSource<>();
Expand Down Expand Up @@ -111,6 +122,10 @@ public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, Bl
}
}

/**
* Get services and start notify for characteristics changed
* @param bleDevice BLE device
*/
public void startServiceS(BleDevice bleDevice){
List<BluetoothGattService> services = BleManager.getInstance().getBluetoothGattServices(bleDevice);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Expand All @@ -136,6 +151,11 @@ else if (characteristic.getUuid().compareTo(rxid)==0){
}
}

/**
* Start notify for characteristics changed
* @param bleDevice BLE device
* @param characteristic
*/
public void newConnectedBLEDevice(final BleDevice bleDevice, final BluetoothGattCharacteristic characteristic) {


Expand Down Expand Up @@ -168,11 +188,18 @@ public void onCharacteristicChanged(byte[] data) {
}
}

/**
* Disconnect from the verisense device
*/
@Override
public void disconnect() {
BleManager.getInstance().disconnect(mBleDevice);
}

/**
* Write bytes to the verisense device
* @param bytes byte array to be written
*/
@Override
public void writeBytes(byte[] bytes) {
BleManager.getInstance().write(mBleDevice, sid.toString(), txid.toString(), bytes, false, new BleWriteCallback() {
Expand All @@ -192,11 +219,20 @@ public void onWriteFailure(BleException exception) {
public void stop() {

}

/**
* Convert mac address to uuid
* @param MacID e.g. d0:2b:46:3d:a2:bb
* @return uuid e.g. 00000000-0000-0000-0000-d02b463da2bb
*/
public String convertMacIDtoUUID(String MacID) {
String uuid = "00000000-0000-0000-0000-";
return uuid + MacID.replace(":", "");
}

/**
* @return uuid e.g. 00000000-0000-0000-0000-d02b463da2bb
*/
public String getUuid() {
return convertMacIDtoUUID(this.mMac);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/dataSyncButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Data Sync" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package shimmerresearch.com.shimmerbasicexample;
package shimmerresearch.com.shimmerblebasicexample;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
Expand Down
Loading