Skip to content

Commit

Permalink
BluetoothItem for bluetooth list
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Jul 15, 2023
1 parent 4793250 commit 1a08116
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 54 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/com/platypii/baseline/Intents.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ public class Intents {
private static final String TAG = "Intents";

/**
* Open track activity
* Open track activity for local track file
*/
public static void openTrackLocal(@NonNull Context context, @NonNull TrackFile trackFile) {
final Intent intent = new Intent(context, TrackLocalActivity.class);
intent.putExtra(TrackLoader.EXTRA_TRACK_FILE, trackFile.file.getAbsolutePath());
context.startActivity(intent);
}

/**
* Open track activity for cloud track
*/
public static void openTrackRemote(@NonNull Context context, @NonNull TrackMetadata track) {
final Intent intent = new Intent(context, TrackRemoteActivity.class);
intent.putExtra(TrackLoader.EXTRA_TRACK_ID, track.track_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ public void stop() {
}

/**
* Add a new listener to be notified of location updates
* Add a new listener to be notified of sensor updates
*/
public void addListener(MySensorListener listener) {
listeners.add(listener);
}

/**
* Remove a listener from location updates
* Remove a listener from sensor updates
*/
public void removeListener(MySensorListener listener) {
listeners.remove(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import android.os.Build;
import com.platypii.baseline.R;
import com.platypii.baseline.Services;
import com.platypii.baseline.bluetooth.BluetoothItem;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -16,18 +16,16 @@
import androidx.annotation.Nullable;
import java.util.List;

import static com.platypii.baseline.bluetooth.BluetoothUtil.getDeviceName;

class BluetoothAdapter extends BaseAdapter {

@NonNull
private final List<BluetoothDevice> devices;
private final List<BluetoothItem> devices;

private final LayoutInflater inflater;

private final String internalGps;

BluetoothAdapter(@NonNull Context context, @NonNull List<BluetoothDevice> devices) {
BluetoothAdapter(@NonNull Context context, @NonNull List<BluetoothItem> devices) {
this.devices = devices;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
internalGps = context.getString(R.string.internal_gps);
Expand Down Expand Up @@ -55,16 +53,15 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
}
} else {
// Bluetooth GPS device
final BluetoothDevice device = devices.get(position - 1);
final String deviceName = getDeviceName(device);
nameView.setText(deviceName);
addressView.setText(device.getAddress());
if (deviceName.contains("GPS") || deviceName.startsWith("Mohawk")) {
final BluetoothItem device = devices.get(position - 1);
nameView.setText(device.name);
addressView.setText(device.address);
if (device.name.contains("GPS") || device.name.startsWith("Mohawk")) {
nameView.setTextColor(0xffeeeeee);
} else {
nameView.setTextColor(0xffbbbbbb);
}
if (device.getAddress().equals(Services.bluetooth.preferences.preferenceDeviceId)) {
if (device.address.equals(Services.bluetooth.preferences.preferenceDeviceId)) {
checkedView.setVisibility(View.VISIBLE);
} else {
checkedView.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.platypii.baseline.views.bluetooth;

import com.platypii.baseline.Services;
import com.platypii.baseline.bluetooth.BluetoothDeviceComparator;
import com.platypii.baseline.bluetooth.BluetoothItem;
import com.platypii.baseline.events.BluetoothEvent;
import com.platypii.baseline.util.Analytics;
import com.platypii.baseline.util.Exceptions;
Expand All @@ -17,17 +19,18 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.ListFragment;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import static com.platypii.baseline.bluetooth.BluetoothUtil.getDeviceName;

public class BluetoothDeviceListFragment extends ListFragment {
private static final String TAG = "BluetoothDeviceList";

private final List<BluetoothDevice> devices = new ArrayList<>();
private final List<BluetoothItem> devices = new ArrayList<>();
private final Set<BluetoothItem> deviceSet = new HashSet<>();
@Nullable
private BluetoothAdapter bluetoothAdapter;

Expand All @@ -45,49 +48,45 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

private void updateDeviceList() {
devices.clear();
deviceSet.clear();
try {
final List<BluetoothDevice> updatedDevices = Services.bluetooth.getDevices();
devices.addAll(updatedDevices);
for (BluetoothDevice device : Services.bluetooth.getBondedDevices()) {
deviceSet.add(new BluetoothItem(device));
}
} catch (SecurityException e) {
Log.e(TAG, "Error getting device list", e);
}
devices.addAll(deviceSet);
devices.sort(new BluetoothDeviceComparator());
if (bluetoothAdapter != null) {
bluetoothAdapter.notifyDataSetChanged();
}
}

@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
final BluetoothDevice device = (BluetoothDevice) l.getItemAtPosition(position);
final BluetoothItem device = (BluetoothItem) l.getItemAtPosition(position);
final Activity activity = getActivity();
if (activity != null) {
if (device != null) {
final String deviceId = device.getAddress();
final String deviceName = getDeviceName(device);
Log.i(TAG, "Bluetooth device selected " + deviceName);
Log.i(TAG, "Bluetooth device selected " + device.name);

if (deviceId != null && !deviceId.equals(Services.bluetooth.preferences.preferenceDeviceId)) {
if (!device.address.equals(Services.bluetooth.preferences.preferenceDeviceId)) {
// Changed bluetooth device, reset state
Services.location.locationProviderBluetooth.reset();
}

// Save device preference
boolean ble = false;
try {
ble = device.getType() != BluetoothDevice.DEVICE_TYPE_CLASSIC;
} catch (SecurityException e) {
Exceptions.report(e);
}
Services.bluetooth.preferences.save(activity, true, device.getAddress(), deviceName, ble);
Services.bluetooth.preferences.save(activity, true, device.address, device.name, device.ble);
// Update ui
EventBus.getDefault().post(new BluetoothEvent());
// Start / restart bluetooth service
Services.bluetooth.restart(activity);

// Log event
final Bundle bundle = new Bundle();
bundle.putString("device_id", device.getAddress());
bundle.putString("device_name", deviceName);
bundle.putString("device_id", device.address);
bundle.putString("device_name", device.name);
Analytics.logEvent(activity, "bluetooth_selected", bundle);
} else {
Log.i(TAG, "Internal GPS selected");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void onConnectionFailed(@NonNull BluetoothPeripheral peripheral, @NonNull

@Override
public void onDisconnectedPeripheral(@NonNull final BluetoothPeripheral peripheral, @NonNull final HciStatus status) {
Log.i(TAG, "Rangefinder disconnected " + peripheral.getName() + " with status " + status);
Log.i(TAG, "BLE disconnected " + peripheral.getName() + " with status " + status);
currentPeripheral = null;
// Go back to searching
if (BluetoothState.started(bluetoothState)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package com.platypii.baseline.bluetooth;

import android.bluetooth.BluetoothDevice;
import androidx.annotation.NonNull;
import java.util.Comparator;

import static com.platypii.baseline.bluetooth.BluetoothUtil.getDeviceName;

/**
* Used to put GPS at top of device list
*/
public class BluetoothDeviceComparator implements Comparator<BluetoothDevice> {
public class BluetoothDeviceComparator implements Comparator<BluetoothItem> {
@Override
public int compare(@NonNull BluetoothDevice device1, @NonNull BluetoothDevice device2) {
public int compare(@NonNull BluetoothItem device1, @NonNull BluetoothItem device2) {
return score(device2) - score(device1);
}

private int score(@NonNull BluetoothDevice device) {
final String name = getDeviceName(device);
if (name.startsWith("Mohawk")) return 2;
else if (name.contains("GPS")) return 1;
private int score(@NonNull BluetoothItem device) {
if (device.name.startsWith("Mohawk")) return 2;
else if (device.name.startsWith("RaceBox")) return 1;
else if (device.name.contains("GPS")) return 1;
else return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.platypii.baseline.bluetooth;

import com.platypii.baseline.util.Exceptions;

import android.bluetooth.BluetoothDevice;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static com.platypii.baseline.bluetooth.BluetoothUtil.getDeviceName;

public class BluetoothItem {
public final String name;
public final String address;
public boolean ble;

public BluetoothItem(@NonNull BluetoothDevice device) {
name = getDeviceName(device);
address = device.getAddress();
ble = false;
try {
ble = device.getType() != BluetoothDevice.DEVICE_TYPE_CLASSIC;
} catch (SecurityException e) {
Exceptions.report(e);
}
}

@Override
public boolean equals(@Nullable Object obj) {
return (obj instanceof BluetoothItem) && ((BluetoothItem) obj).address.equals(address);
}

@Override
public int hashCode() {
return address.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import static com.platypii.baseline.RequestCodes.RC_BLUE_ENABLE;
Expand Down Expand Up @@ -104,24 +102,21 @@ private BluetoothAdapter getAdapter(@NonNull Activity activity) {
}

/**
* Return list of bonded devices, with GPS devices first
* Return list of bonded devices
*/
@NonNull
public List<BluetoothDevice> getDevices() {
public Set<BluetoothDevice> getBondedDevices() {
final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
try {
final Set<BluetoothDevice> deviceSet = bluetoothAdapter.getBondedDevices();
final List<BluetoothDevice> devices = new ArrayList<>(deviceSet);
Collections.sort(devices, new BluetoothDeviceComparator());
return devices;
return bluetoothAdapter.getBondedDevices();
} catch (SecurityException e) {
Log.w(TAG, "Tried to get devices, but bluetooth permission denied", e);
return new ArrayList<>();
return new HashSet<>();
}
} else {
Log.w(TAG, "Tried to get devices, but bluetooth is not enabled");
return new ArrayList<>();
return new HashSet<>();
}
}

Expand Down

0 comments on commit 1a08116

Please sign in to comment.