Skip to content

Commit

Permalink
refactor BT services, allow matching device by advertised service
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1asl committed Sep 18, 2023
1 parent cf20315 commit 6000064
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 86 deletions.
40 changes: 13 additions & 27 deletions src/main/java/com/astoev/cave/survey/activity/main/BTActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
Expand All @@ -23,13 +22,11 @@
import com.astoev.cave.survey.activity.UIUtilities;
import com.astoev.cave.survey.service.bluetooth.BluetoothService;
import com.astoev.cave.survey.service.bluetooth.device.AbstractBluetoothDevice;
import com.astoev.cave.survey.service.bluetooth.device.DiscoveredBluetoothDevice;
import com.astoev.cave.survey.util.ConfigUtil;
import com.astoev.cave.survey.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Created with IntelliJ IDEA.
Expand All @@ -40,7 +37,7 @@
*/
public class BTActivity extends MainMenuActivity implements Refresheable {

Set<Pair<String, String>> devices = new HashSet<>();
List<DiscoveredBluetoothDevice> devices = new ArrayList<>();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -107,7 +104,7 @@ private void displaySupportedDevices() {

for (AbstractBluetoothDevice device : BluetoothService.getSupportedDevices()) {
TextView deviceLabel = new TextView(getApplicationContext());
deviceLabel.setText("\t\u2022 " + device.getDescription());
deviceLabel.setText(String.format("\t\u2022 %s", device.getDescription()));
deviceLabel.setTextColor(Color.WHITE);
devicesList.addView(deviceLabel);
}
Expand Down Expand Up @@ -146,22 +143,14 @@ protected String getScreenTitle() {
private void refreshDevicesList() {

devices = BluetoothService.getPairedCompatibleDevices();

String selectedDeviceName = null;
String selectedBtDeviceAddress = ConfigUtil.getStringProperty(ConfigUtil.PROP_CURR_BT_DEVICE_ADDRESS);
if (StringUtils.isNotEmpty(selectedBtDeviceAddress)) {
String selectedBtDeviceName = ConfigUtil.getStringProperty(ConfigUtil.PROP_CURR_BT_DEVICE_NAME);
selectedDeviceName = buildDeviceName(new Pair<>(selectedBtDeviceName, selectedBtDeviceAddress));
}

List<String> devicesList = new ArrayList<>();
int index = 0;
int selectedDeviceIndex = -1;
String tempName;
for (final Pair<String, String> device : devices) {
tempName = buildDeviceName(device);
devicesList.add(tempName);
if (tempName.equals(selectedDeviceName)) {
for (final DiscoveredBluetoothDevice device : devices) {
devicesList.add(device.getDisplayName());
if (device.address.equals(selectedBtDeviceAddress)) {
selectedDeviceIndex = index;
}
index++;
Expand Down Expand Up @@ -190,22 +179,19 @@ public void pairNewDevice() {
startActivity(intentOpenBluetoothSettings);
}

private String buildDeviceName(Pair<String, String> aDevice) {
return aDevice.first + " : " + aDevice.second;
}

public void selectDevice() {
// get selected
Spinner devicesChooser = findViewById(R.id.bt_devices);
Pair<String, String> device = new ArrayList<>(devices).get(devicesChooser.getSelectedItemPosition());
Log.i(LOG_TAG_UI, "Try to use " + device.first + ":" + device.second);
DiscoveredBluetoothDevice device = devices.get(devicesChooser.getSelectedItemPosition());
Log.i(LOG_TAG_UI, "Try to use " + device.getDisplayName());

UIUtilities.showNotification(R.string.bt_device_connecting, device.first);
UIUtilities.showNotification(R.string.bt_device_connecting, device.getDisplayName());

// store & propagate
ConfigUtil.setStringProperty(ConfigUtil.PROP_CURR_BT_DEVICE_NAME, device.first);
ConfigUtil.setStringProperty(ConfigUtil.PROP_CURR_BT_DEVICE_ADDRESS, device.second);
BluetoothService.selectDevice(device.second);
ConfigUtil.setStringProperty(ConfigUtil.PROP_CURR_BT_DEVICE_NAME, device.name);
ConfigUtil.setStringProperty(ConfigUtil.PROP_CURR_BT_DEVICE_ADDRESS, device.address);
ConfigUtil.setStringProperty(ConfigUtil.PROP_CURR_BT_DEVICE_DEFINITION, device.definition.getClass().getName());
BluetoothService.selectDevice(device);
}

/**
Expand Down
Loading

0 comments on commit 6000064

Please sign in to comment.