-
Notifications
You must be signed in to change notification settings - Fork 0
/
DevicesFragment.java
211 lines (175 loc) · 7.85 KB
/
DevicesFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package simple.bluetooth.terminal;
import static li.garteroboter.pren.Constants.ACCEPTED_ESP32_DEVICE_NAMES;
import static li.garteroboter.pren.Constants.ESP_MAC_ADRESES;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.ListFragment;
import java.util.ArrayList;
import li.garteroboter.pren.R;
public class DevicesFragment extends ListFragment {
private static final String TAG = "DevicesFragment";
private BluetoothAdapter bluetoothAdapter;
private final ArrayList<BluetoothDevice> listItems = new ArrayList<>();
private ArrayAdapter<BluetoothDevice> listAdapter;
private boolean autoConnectToESP32;
public static DevicesFragment newInstance() {
DevicesFragment fragment = new DevicesFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// In some situations, we might not want to automatically connect to ESP. For example,
// if there is no device at hand and we just want to test.
if (getArguments() != null) {
try {
String autoConnect = getArguments().getString("autoConnect");
if (autoConnect != null && autoConnect.equals("true")) {
Log.d(TAG, "autoConnectToESP32 == true");
autoConnectToESP32 = true;
} else {
Log.e(TAG, "autoConnectToESP32 == false");
autoConnectToESP32 = false;
}
} catch (Exception e) {
Log.e(TAG, "Attempted to get Arguments in Fragment startup. " +
"The autoConnectToESP32 Property was not found.");
e.printStackTrace();
}
}
if (getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
listAdapter = new ArrayAdapter<BluetoothDevice>(getActivity(), 0, listItems) {
@SuppressLint("MissingPermission")
@NonNull
@Override
public View getView(int position, View view, @NonNull ViewGroup parent) {
BluetoothDevice device = listItems.get(position);
if (view == null)
view = getActivity().getLayoutInflater().inflate(R.layout.device_list_item,
parent, false);
TextView text1 = view.findViewById(R.id.text1);
TextView text2 = view.findViewById(R.id.text2);
text1.setText(device.getName());
text2.setText(device.getAddress());
return view;
}
};
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(null);
View header = getActivity().getLayoutInflater().inflate(R.layout.device_list_header, null
, false);
getListView().addHeaderView(header, null, false);
setEmptyText("initializing...");
((TextView) getListView().getEmptyView()).setTextSize(18);
setListAdapter(listAdapter);
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume()");
if (bluetoothAdapter == null) {
setEmptyText("<bluetooth not supported>");
} else if (!bluetoothAdapter.isEnabled()) {
setEmptyText("<bluetoothAdapter is disabled>");
}else if (!autoConnectToESP32) {
setEmptyText("Toggle Bluetooth in App-Settings. ");
return; // don't scan if we don't use bluetooth
} else {
setEmptyText("<no bluetooth devices matching the MAC-Addresses>");
}
refresh();
}
@SuppressLint("MissingPermission")
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
void refresh() {
listItems.clear();
if (bluetoothAdapter != null) {
for (BluetoothDevice device : bluetoothAdapter.getBondedDevices())
if (device.getType() != BluetoothDevice.DEVICE_TYPE_LE)
if (isRelevantBluetoothDevice(device))
listItems.add(device);
}
listItems.sort(DevicesFragment::compareTo);
listAdapter.notifyDataSetChanged();
if (autoConnectToESP32) {
Log.d(TAG, "autoConnectToESP ");
if (listItems.size() == 1) {
BluetoothDevice ESP32_Device = listItems.get(0);
initializeTerminalFragment(ESP32_Device);
} else {
// there may be multiple devices with the name "ESP32"
// here I can check for the MAC Address.
// This can be useful when there are multiple ESP32 devices in the area.
}
}
}
private boolean isRelevantBluetoothDevice(BluetoothDevice device) {
final String bluetoothDeviceAddress = device.getAddress().toUpperCase();
Log.e(TAG, bluetoothDeviceAddress);
return ESP_MAC_ADRESES.contains(bluetoothDeviceAddress);
}
private boolean isRelevantBluetoothDeviceByName(BluetoothDevice device) {
@SuppressLint("MissingPermission")
final String bluetoothDeviceName = device.getName().toLowerCase();
return ACCEPTED_ESP32_DEVICE_NAMES.contains(bluetoothDeviceName);
}
@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
BluetoothDevice device = listItems.get(position - 1);
initializeTerminalFragment(device);
}
public void initializeTerminalFragment(BluetoothDevice device) {
Bundle args = new Bundle();
args.putString("device", device.getAddress());
Fragment terminalFragment = new TerminalFragment();
terminalFragment.setArguments(args);
if (getActivity() == null) {
Log.e(TAG, "FATAL: getActivity() == null");
return;
}
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragmentBluetoothChain, terminalFragment,
"terminal").addToBackStack("terminal").commit();
}
/**
* sort by name, then address. sort named devices first
*/
static int compareTo(BluetoothDevice a, BluetoothDevice b) {
@SuppressLint("MissingPermission") boolean aValid =
a.getName() != null && !a.getName().isEmpty();
@SuppressLint("MissingPermission") boolean bValid =
b.getName() != null && !b.getName().isEmpty();
if (aValid && bValid) {
@SuppressLint("MissingPermission") int ret = a.getName().compareTo(b.getName());
if (ret != 0) return ret;
return a.getAddress().compareTo(b.getAddress());
}
if (aValid) return -1;
if (bValid) return +1;
return a.getAddress().compareTo(b.getAddress());
}
public DevicesFragment() {
// required empty constructor
}
}