Skip to content

Commit

Permalink
Refactored test app to select multiple ble addresses to write to simu…
Browse files Browse the repository at this point in the history
…ltenously
  • Loading branch information
bayosip committed Aug 6, 2021
1 parent a038fe6 commit 0e71c88
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import android.app.Activity;

import java.util.List;

public interface ControlFragmentListener extends AppActivity {

void sendInstructions (String instruct);//function takes string command and passes
//it to the gat service
void speechInputCall();
void stopListening();
void startListening();
void setSelectedServiceUUID ( String uuid, int flag);
void setSelectedServiceUUID ( String uuid);
void setSelectedAddresses(List<String> addresses);
void getDeviceAddressFromPopUp();
Activity getFragmentactivity();
}
10 changes: 10 additions & 0 deletions app/src/main/java/inc/osips/bleproject/utilities/ServiceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ private static Boolean isServiceBLEAlreadyRunningAPI16(Context activity) {
return false;
}

private boolean isMyServiceRunning(Context context, Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

public static Boolean isAnyRemoteConnectionServiceRunningAPI16(Context context){
if (isServiceWiFiAlreadyRunningAPI16(context) || isServiceBLEAlreadyRunningAPI16(context))
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public class ControllerActivity extends AppCompatActivity implements ControlFrag
public static final String UP = "up";
public static final String DOWN = "down";

private static final int UUID = 1;
private static final int ADDR = 0;
public static final int UUID = 1;
public static final int ADDR = 0;

private static final String TAG = ControllerActivity.class.getSimpleName();

Expand Down Expand Up @@ -143,15 +143,24 @@ public void getDeviceAddressFromPopUp() {
}

@Override
public void setSelectedServiceUUID(String uuidAddr, int flag) {
if (flag==UUID)
presenter.setBaseUuidOfBLEDeviceAndConnect(uuidAddr);
else if(flag == ADDR){
presenter.setDeviceAddressAndSendInstructions(uuidAddr, instructions);
}
public void setSelectedServiceUUID(String uuidAddr) {
presenter.setBaseUuidOfBLEDeviceAndConnect(uuidAddr);
dialog.dismiss();
}

@Override
public void setSelectedAddresses(List<String> addresses) {
dialog.dismiss();
for (String addr: addresses){
presenter.setDeviceAddressAndSendInstructions(addr, instructions);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void getButtonConfigPopUp(){
final Dialog buttonConfig = new Dialog(this);
buttonConfig.requestWindowFeature(Window.FEATURE_NO_TITLE);
Expand All @@ -176,21 +185,18 @@ public void getButtonConfigPopUp(){
final EditText txtDown = buttonConfig.findViewById(R.id.editArrowDown);
presenter.setEditTextIfStringAvailable(txtDown, GeneralUtil.getAppPrefStoredStringWithName(DOWN));

saveConfig.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
presenter.setASharedPrefFromButtonConfig(ON_OFF, txtOnOff);
presenter.setASharedPrefFromButtonConfig(LEFT, txtLeft);
presenter.setASharedPrefFromButtonConfig(RIGHT, txtRight);
presenter.setASharedPrefFromButtonConfig(UP, txtUp);
presenter.setASharedPrefFromButtonConfig(DOWN, txtDown);

if(!GeneralUtil.getAppPref().contains(BUTTON_CONFIG))
GeneralUtil.getEditor().putBoolean(BUTTON_CONFIG, true).commit();

manualFrag.shouldEnableButtons();
buttonConfig.dismiss();
}
saveConfig.setOnClickListener(view -> {
presenter.setASharedPrefFromButtonConfig(ON_OFF, txtOnOff);
presenter.setASharedPrefFromButtonConfig(LEFT, txtLeft);
presenter.setASharedPrefFromButtonConfig(RIGHT, txtRight);
presenter.setASharedPrefFromButtonConfig(UP, txtUp);
presenter.setASharedPrefFromButtonConfig(DOWN, txtDown);

if(!GeneralUtil.getAppPref().contains(BUTTON_CONFIG))
GeneralUtil.getEditor().putBoolean(BUTTON_CONFIG, true).commit();

manualFrag.shouldEnableButtons();
buttonConfig.dismiss();
});

runOnUiThread(buttonConfig::show);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import inc.osips.bleproject.R;
import inc.osips.bleproject.interfaces.ControlFragmentListener;
import inc.osips.bleproject.interfaces.ServiceSelectorListener;
import inc.osips.bleproject.view.activities.ControllerActivity;
import inc.osips.bleproject.view.listviews.DevicesViewHolderAdapter;


Expand All @@ -38,6 +40,7 @@ public class ServiceSelectorDialog extends DialogFragment implements ServiceSele
private RecyclerView listServices;
private DevicesViewHolderAdapter adapter;
private String selectedUUID;
private List<String> selected = new ArrayList<>();

public static void setListUUID(List<String> listUUID, int flag) {
ServiceSelectorDialog.listUUID = listUUID;
Expand Down Expand Up @@ -73,6 +76,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
super.onViewCreated(view, savedInstanceState);
//deviceAddr = requireArguments().getString(ADDRESS);
initialiseWidgets(view);
selected.clear();
}

private void initialiseWidgets(View view){
Expand All @@ -84,8 +88,14 @@ private void initialiseWidgets(View view){

listServices.setLayoutManager(layoutManager);
listServices.setAdapter(adapter);
enterUUID.setOnClickListener(view1 ->
listener.setSelectedServiceUUID(selectedUUID, FLAG));
enterUUID.setOnClickListener(view1 ->{
if(FLAG == ControllerActivity.UUID){
listener.setSelectedServiceUUID(selectedUUID);
}else {
listener.setSelectedAddresses(selected);
}
}
);
}

@Override
Expand All @@ -100,9 +110,13 @@ public void onResume() {

@Override
public void selectAServiceWith(int pos) {
selectedUUID = listUUID.get(pos);
Log.d(TAG, "selectAServiceWith: " + selectedUUID);
adapter.notifyDataSetChanged();
if(FLAG == ControllerActivity.UUID) {
selectedUUID = listUUID.get(pos);
Log.d(TAG, "selectAServiceWith: " + selectedUUID);
adapter.notifyDataSetChanged();
}else {
selected.add(listUUID.get(pos));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class DevicesViewHolder extends RecyclerView.ViewHolder implements View.O
private boolean isRemoteServices = false;
private RefreshItem refresh;
private Context context;
private boolean change = false;

interface RefreshItem {
void setSelectedPosition(int position);
Expand Down Expand Up @@ -86,6 +87,7 @@ public void changeItemBackground(boolean shouldChange) {
@Override
public void onClick(View view) {
if(isRemoteServices){
changeItemBackground(!change);
refresh.setSelectedPosition(getAbsoluteAdapterPosition());
listener1.selectAServiceWith(getAbsoluteAdapterPosition());
}else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ public void sendInstructionsToConnectedDevice(String deviceAddr, @Nullable UUID
BluetoothGattCharacteristic gattCharacteristic =null;
BleWriteService wService = gattServicesMap.get(bleGatt);
assert wService != null;
//Checks for characteristic with particular descriptor
if(charxDescriptor!=null){
for (BluetoothGattCharacteristic charx: wService.getCharacteristics()){
if(charx.getDescriptor(charxDescriptor)!=null){
Expand Down

0 comments on commit 0e71c88

Please sign in to comment.