RileyLinkBLEConfigActivity -> kt
This commit is contained in:
parent
bc66ca312e
commit
4fbef2188f
1 changed files with 214 additions and 285 deletions
|
@ -1,359 +1,288 @@
|
||||||
package info.nightscout.androidaps.plugins.pump.common.dialog;
|
package info.nightscout.androidaps.plugins.pump.common.dialog
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.annotation.SuppressLint
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.app.AlertDialog
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothAdapter
|
||||||
import android.bluetooth.le.BluetoothLeScanner;
|
import android.bluetooth.BluetoothDevice
|
||||||
import android.bluetooth.le.ScanCallback;
|
import android.bluetooth.BluetoothManager
|
||||||
import android.bluetooth.le.ScanFilter;
|
import android.bluetooth.le.BluetoothLeScanner
|
||||||
import android.bluetooth.le.ScanResult;
|
import android.bluetooth.le.ScanCallback
|
||||||
import android.bluetooth.le.ScanSettings;
|
import android.bluetooth.le.ScanFilter
|
||||||
import android.os.Bundle;
|
import android.bluetooth.le.ScanResult
|
||||||
import android.os.Handler;
|
import android.bluetooth.le.ScanSettings
|
||||||
import android.os.ParcelUuid;
|
import android.content.Context
|
||||||
import android.util.Log;
|
import android.content.DialogInterface
|
||||||
import android.view.LayoutInflater;
|
import android.os.Bundle
|
||||||
import android.view.View;
|
import android.os.Handler
|
||||||
import android.view.ViewGroup;
|
import android.os.HandlerThread
|
||||||
import android.widget.BaseAdapter;
|
import android.os.ParcelUuid
|
||||||
import android.widget.Button;
|
import android.view.View
|
||||||
import android.widget.ListView;
|
import android.view.ViewGroup
|
||||||
import android.widget.TextView;
|
import android.widget.*
|
||||||
import android.widget.Toast;
|
import android.widget.AdapterView.OnItemClickListener
|
||||||
|
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import info.nightscout.androidaps.interfaces.ActivePlugin
|
||||||
|
import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck
|
||||||
import java.util.ArrayList;
|
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.R
|
||||||
import java.util.Collections;
|
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst
|
||||||
import java.util.HashMap;
|
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil
|
||||||
import java.util.List;
|
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.GattAttributes
|
||||||
import java.util.Map;
|
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.databinding.RileyLinkBleConfigActivityBinding
|
||||||
|
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice
|
||||||
import javax.inject.Inject;
|
import info.nightscout.shared.logging.AAPSLogger
|
||||||
|
import info.nightscout.shared.logging.LTag
|
||||||
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
|
import info.nightscout.shared.sharedPreferences.SP
|
||||||
import info.nightscout.androidaps.interfaces.ActivePlugin;
|
import org.apache.commons.lang3.StringUtils
|
||||||
import info.nightscout.shared.logging.AAPSLogger;
|
import java.util.*
|
||||||
import info.nightscout.shared.logging.LTag;
|
import javax.inject.Inject
|
||||||
import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck;
|
|
||||||
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.R;
|
|
||||||
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst;
|
|
||||||
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil;
|
|
||||||
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.GattAttributes;
|
|
||||||
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice;
|
|
||||||
import info.nightscout.androidaps.utils.resources.ResourceHelper;
|
|
||||||
import info.nightscout.shared.sharedPreferences.SP;
|
|
||||||
|
|
||||||
// IMPORTANT: This activity needs to be called from RileyLinkSelectPreference (see pref_medtronic.xml as example)
|
// IMPORTANT: This activity needs to be called from RileyLinkSelectPreference (see pref_medtronic.xml as example)
|
||||||
public class RileyLinkBLEConfigActivity extends NoSplashAppCompatActivity {
|
class RileyLinkBLEConfigActivity : NoSplashAppCompatActivity() {
|
||||||
|
|
||||||
@Inject AAPSLogger aapsLogger;
|
@Inject lateinit var aapsLogger: AAPSLogger
|
||||||
@Inject SP sp;
|
@Inject lateinit var sp: SP
|
||||||
@Inject ResourceHelper rh;
|
@Inject lateinit var blePreCheck: BlePreCheck
|
||||||
@Inject BlePreCheck blePrecheck;
|
@Inject lateinit var rileyLinkUtil: RileyLinkUtil
|
||||||
@Inject RileyLinkUtil rileyLinkUtil;
|
@Inject lateinit var activePlugin: ActivePlugin
|
||||||
@Inject ActivePlugin activePlugin;
|
@Inject lateinit var context: Context
|
||||||
|
|
||||||
private static final String TAG = "RileyLinkBLEConfigActivity";
|
private val handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
|
||||||
private static final long SCAN_PERIOD_MILLIS = 15_000;
|
private val bluetoothAdapter: BluetoothAdapter? get() = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter
|
||||||
|
private var deviceListAdapter = LeDeviceListAdapter()
|
||||||
|
private var settings: ScanSettings? = null
|
||||||
|
private var filters: List<ScanFilter>? = null
|
||||||
|
private var bleScanner: BluetoothLeScanner? = null
|
||||||
|
private var scanning = false
|
||||||
|
|
||||||
private ScanSettings settings;
|
private lateinit var binding: RileyLinkBleConfigActivityBinding
|
||||||
private List<ScanFilter> filters;
|
private val stopScanAfterTimeoutRunnable = Runnable {
|
||||||
private TextView currentlySelectedRileyLinkName;
|
|
||||||
private TextView currentlySelectedRileyLinkAddress;
|
|
||||||
private Button buttonRemoveRileyLink;
|
|
||||||
private Button buttonStartScan;
|
|
||||||
private Button buttonStopScan;
|
|
||||||
private BluetoothAdapter bluetoothAdapter;
|
|
||||||
private BluetoothLeScanner bleScanner;
|
|
||||||
private LeDeviceListAdapter deviceListAdapter;
|
|
||||||
private Handler handler;
|
|
||||||
public boolean scanning;
|
|
||||||
|
|
||||||
private final Runnable stopScanAfterTimeoutRunnable = () -> {
|
|
||||||
if (scanning) {
|
if (scanning) {
|
||||||
stopLeDeviceScan();
|
stopLeDeviceScan()
|
||||||
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this); // Reconnect current RL
|
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this) // Reconnect current RL
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
@Override
|
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState)
|
||||||
super.onCreate(savedInstanceState);
|
binding = RileyLinkBleConfigActivityBinding.inflate(layoutInflater)
|
||||||
setContentView(R.layout.riley_link_ble_config_activity);
|
setContentView(binding.root)
|
||||||
|
|
||||||
// Initializes Bluetooth adapter.
|
// Initializes Bluetooth adapter.
|
||||||
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
binding.rileyLinkBleConfigScanDeviceList.adapter = deviceListAdapter
|
||||||
deviceListAdapter = new LeDeviceListAdapter();
|
binding.rileyLinkBleConfigScanDeviceList.onItemClickListener = OnItemClickListener { _: AdapterView<*>?, view: View, _: Int, _: Long ->
|
||||||
handler = new Handler();
|
|
||||||
currentlySelectedRileyLinkName = findViewById(R.id.riley_link_ble_config_currently_selected_riley_link_name);
|
|
||||||
currentlySelectedRileyLinkAddress = findViewById(R.id.riley_link_ble_config_currently_selected_riley_link_address);
|
|
||||||
buttonRemoveRileyLink = findViewById(R.id.riley_link_ble_config_button_remove_riley_link);
|
|
||||||
buttonStartScan = findViewById(R.id.riley_link_ble_config_scan_start);
|
|
||||||
buttonStopScan = findViewById(R.id.riley_link_ble_config_button_scan_stop);
|
|
||||||
ListView deviceList = findViewById(R.id.riley_link_ble_config_scan_device_list);
|
|
||||||
deviceList.setAdapter(deviceListAdapter);
|
|
||||||
deviceList.setOnItemClickListener((parent, view, position, id) -> {
|
|
||||||
// stop scanning if still active
|
// stop scanning if still active
|
||||||
if (scanning) {
|
if (scanning) stopLeDeviceScan()
|
||||||
stopLeDeviceScan();
|
|
||||||
}
|
|
||||||
|
|
||||||
String bleAddress = ((TextView) view.findViewById(R.id.riley_link_ble_config_scan_item_device_address)).getText().toString();
|
val bleAddress = (view.findViewById(R.id.riley_link_ble_config_scan_item_device_address) as TextView).text.toString()
|
||||||
String deviceName = ((TextView) view.findViewById(R.id.riley_link_ble_config_scan_item_device_name)).getText().toString();
|
val deviceName = (view.findViewById(R.id.riley_link_ble_config_scan_item_device_name) as TextView).text.toString()
|
||||||
|
sp.putString(RileyLinkConst.Prefs.RileyLinkAddress, bleAddress)
|
||||||
sp.putString(RileyLinkConst.Prefs.RileyLinkAddress, bleAddress);
|
sp.putString(RileyLinkConst.Prefs.RileyLinkName, deviceName)
|
||||||
sp.putString(RileyLinkConst.Prefs.RileyLinkName, deviceName);
|
val rileyLinkPump = activePlugin.activePump as RileyLinkPumpDevice
|
||||||
|
rileyLinkPump.rileyLinkService.verifyConfiguration(true) // force reloading of address to assure that the RL gets reconnected (even if the address didn't change)
|
||||||
RileyLinkPumpDevice rileyLinkPump = (RileyLinkPumpDevice) activePlugin.getActivePump();
|
rileyLinkPump.triggerPumpConfigurationChangedEvent()
|
||||||
rileyLinkPump.getRileyLinkService().verifyConfiguration(true); // force reloading of address to assure that the RL gets reconnected (even if the address didn't change)
|
finish()
|
||||||
rileyLinkPump.triggerPumpConfigurationChangedEvent();
|
}
|
||||||
|
binding.rileyLinkBleConfigScanStart.setOnClickListener {
|
||||||
finish();
|
|
||||||
});
|
|
||||||
|
|
||||||
buttonStartScan.setOnClickListener(view -> {
|
|
||||||
// disable currently selected RL, so that we can discover it
|
// disable currently selected RL, so that we can discover it
|
||||||
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect, this);
|
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect, this)
|
||||||
startLeDeviceScan();
|
startLeDeviceScan()
|
||||||
});
|
}
|
||||||
|
binding.rileyLinkBleConfigButtonScanStop.setOnClickListener {
|
||||||
buttonStopScan.setOnClickListener(view -> {
|
|
||||||
if (scanning) {
|
if (scanning) {
|
||||||
stopLeDeviceScan();
|
stopLeDeviceScan()
|
||||||
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this); // Reconnect current RL
|
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this) // Reconnect current RL
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
binding.rileyLinkBleConfigButtonRemoveRileyLink.setOnClickListener {
|
||||||
buttonRemoveRileyLink.setOnClickListener(view -> new AlertDialog.Builder(this)
|
AlertDialog.Builder(this)
|
||||||
.setIcon(android.R.drawable.ic_dialog_alert)
|
.setIcon(android.R.drawable.ic_dialog_alert)
|
||||||
.setTitle(getString(R.string.riley_link_ble_config_remove_riley_link_confirmation_title))
|
.setTitle(getString(R.string.riley_link_ble_config_remove_riley_link_confirmation_title))
|
||||||
.setMessage(getString(R.string.riley_link_ble_config_remove_riley_link_confirmation))
|
.setMessage(getString(R.string.riley_link_ble_config_remove_riley_link_confirmation))
|
||||||
.setPositiveButton(getString(R.string.riley_link_common_yes), (dialog, which) -> {
|
.setPositiveButton(getString(R.string.riley_link_common_yes)) { _: DialogInterface?, _: Int ->
|
||||||
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect, RileyLinkBLEConfigActivity.this);
|
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect, this@RileyLinkBLEConfigActivity)
|
||||||
sp.remove(RileyLinkConst.Prefs.RileyLinkAddress);
|
sp.remove(RileyLinkConst.Prefs.RileyLinkAddress)
|
||||||
sp.remove(RileyLinkConst.Prefs.RileyLinkName);
|
sp.remove(RileyLinkConst.Prefs.RileyLinkName)
|
||||||
updateCurrentlySelectedRileyLink();
|
updateCurrentlySelectedRileyLink()
|
||||||
})
|
}
|
||||||
.setNegativeButton(getString(R.string.riley_link_common_no), null)
|
.setNegativeButton(getString(R.string.riley_link_common_no), null)
|
||||||
.show());
|
.show()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateCurrentlySelectedRileyLink() {
|
private fun updateCurrentlySelectedRileyLink() {
|
||||||
String address = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, "");
|
val address = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, "")
|
||||||
if (StringUtils.isEmpty(address)) {
|
if (StringUtils.isEmpty(address)) {
|
||||||
currentlySelectedRileyLinkName.setText(R.string.riley_link_ble_config_no_riley_link_selected);
|
binding.rileyLinkBleConfigCurrentlySelectedRileyLinkName.setText(R.string.riley_link_ble_config_no_riley_link_selected)
|
||||||
currentlySelectedRileyLinkAddress.setVisibility(View.GONE);
|
binding.rileyLinkBleConfigCurrentlySelectedRileyLinkAddress.visibility = View.GONE
|
||||||
buttonRemoveRileyLink.setVisibility(View.GONE);
|
binding.rileyLinkBleConfigButtonRemoveRileyLink.visibility = View.GONE
|
||||||
} else {
|
} else {
|
||||||
currentlySelectedRileyLinkAddress.setVisibility(View.VISIBLE);
|
binding.rileyLinkBleConfigCurrentlySelectedRileyLinkAddress.visibility = View.VISIBLE
|
||||||
buttonRemoveRileyLink.setVisibility(View.VISIBLE);
|
binding.rileyLinkBleConfigButtonRemoveRileyLink.visibility = View.VISIBLE
|
||||||
|
binding.rileyLinkBleConfigCurrentlySelectedRileyLinkName.text = sp.getString(RileyLinkConst.Prefs.RileyLinkName, "RileyLink (?)")
|
||||||
currentlySelectedRileyLinkName.setText(sp.getString(RileyLinkConst.Prefs.RileyLinkName, "RileyLink (?)"));
|
binding.rileyLinkBleConfigCurrentlySelectedRileyLinkAddress.text = address
|
||||||
currentlySelectedRileyLinkAddress.setText(address);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override protected void onResume() {
|
override fun onResume() {
|
||||||
super.onResume();
|
super.onResume()
|
||||||
prepareForScanning();
|
prepareForScanning()
|
||||||
|
updateCurrentlySelectedRileyLink()
|
||||||
updateCurrentlySelectedRileyLink();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override protected void onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy()
|
||||||
if (scanning) {
|
if (scanning) {
|
||||||
stopLeDeviceScan();
|
stopLeDeviceScan()
|
||||||
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this); // Reconnect current RL
|
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this) // Reconnect current RL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareForScanning() {
|
private fun prepareForScanning() {
|
||||||
boolean checkOK = blePrecheck.prerequisitesCheck(this);
|
val checkOK = blePreCheck.prerequisitesCheck(this)
|
||||||
|
|
||||||
if (checkOK) {
|
if (checkOK) {
|
||||||
bleScanner = bluetoothAdapter.getBluetoothLeScanner();
|
bleScanner = bluetoothAdapter?.bluetoothLeScanner
|
||||||
settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
|
settings = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()
|
||||||
filters = Collections.singletonList(new ScanFilter.Builder().setServiceUuid(
|
filters = listOf(
|
||||||
ParcelUuid.fromString(GattAttributes.SERVICE_RADIO)).build());
|
ScanFilter.Builder().setServiceUuid(
|
||||||
|
ParcelUuid.fromString(GattAttributes.SERVICE_RADIO)
|
||||||
|
).build()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final ScanCallback bleScanCallback = new ScanCallback() {
|
private val bleScanCallback: ScanCallback = object : ScanCallback() {
|
||||||
@Override
|
override fun onScanResult(callbackType: Int, scanRecord: ScanResult) {
|
||||||
public void onScanResult(int callbackType, final ScanResult scanRecord) {
|
aapsLogger.debug(LTag.PUMPBTCOMM, scanRecord.toString())
|
||||||
Log.d(TAG, scanRecord.toString());
|
runOnUiThread { if (addDevice(scanRecord)) deviceListAdapter.notifyDataSetChanged() }
|
||||||
|
|
||||||
runOnUiThread(() -> {
|
|
||||||
if (addDevice(scanRecord))
|
|
||||||
deviceListAdapter.notifyDataSetChanged();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun onBatchScanResults(results: List<ScanResult>) {
|
||||||
public void onBatchScanResults(final List<ScanResult> results) {
|
runOnUiThread {
|
||||||
runOnUiThread(() -> {
|
var added = false
|
||||||
boolean added = false;
|
for (result in results) {
|
||||||
|
if (addDevice(result)) added = true
|
||||||
for (ScanResult result : results) {
|
|
||||||
if (addDevice(result))
|
|
||||||
added = true;
|
|
||||||
}
|
}
|
||||||
|
if (added) deviceListAdapter.notifyDataSetChanged()
|
||||||
if (added)
|
}
|
||||||
deviceListAdapter.notifyDataSetChanged();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean addDevice(ScanResult result) {
|
private fun addDevice(result: ScanResult): Boolean {
|
||||||
BluetoothDevice device = result.getDevice();
|
val device = result.device
|
||||||
|
val serviceUuids = result.scanRecord?.serviceUuids
|
||||||
List<ParcelUuid> serviceUuids = result.getScanRecord().getServiceUuids();
|
if (serviceUuids == null || serviceUuids.size == 0) {
|
||||||
|
aapsLogger.debug(LTag.PUMPBTCOMM, "Device " + device.address + " has no serviceUuids (Not RileyLink).")
|
||||||
if (serviceUuids == null || serviceUuids.size() == 0) {
|
} else if (serviceUuids.size > 1) {
|
||||||
Log.v(TAG, "Device " + device.getAddress() + " has no serviceUuids (Not RileyLink).");
|
aapsLogger.debug(LTag.PUMPBTCOMM, "Device " + device.address + " has too many serviceUuids (Not RileyLink).")
|
||||||
} else if (serviceUuids.size() > 1) {
|
|
||||||
Log.v(TAG, "Device " + device.getAddress() + " has too many serviceUuids (Not RileyLink).");
|
|
||||||
} else {
|
} else {
|
||||||
String uuid = serviceUuids.get(0).getUuid().toString().toLowerCase();
|
val uuid = serviceUuids[0].uuid.toString().lowercase(Locale.getDefault())
|
||||||
|
if (uuid == GattAttributes.SERVICE_RADIO) {
|
||||||
if (uuid.equals(GattAttributes.SERVICE_RADIO)) {
|
aapsLogger.debug(LTag.PUMPBTCOMM, "Found RileyLink with address: " + device.address)
|
||||||
Log.i(TAG, "Found RileyLink with address: " + device.getAddress());
|
deviceListAdapter.addDevice(result)
|
||||||
deviceListAdapter.addDevice(result);
|
return true
|
||||||
return true;
|
|
||||||
} else {
|
} else {
|
||||||
Log.v(TAG, "Device " + device.getAddress() + " has incorrect uuid (Not RileyLink).");
|
aapsLogger.debug(LTag.PUMPBTCOMM, "Device " + device.address + " has incorrect uuid (Not RileyLink).")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun onScanFailed(errorCode: Int) {
|
||||||
public void onScanFailed(int errorCode) {
|
aapsLogger.error(LTag.PUMPBTCOMM, "Scan Failed", "Error Code: $errorCode")
|
||||||
Log.e("Scan Failed", "Error Code: " + errorCode);
|
Toast.makeText(
|
||||||
Toast.makeText(RileyLinkBLEConfigActivity.this, rh.gs(R.string.riley_link_ble_config_scan_error, errorCode),
|
this@RileyLinkBLEConfigActivity, rh.gs(R.string.riley_link_ble_config_scan_error, errorCode),
|
||||||
Toast.LENGTH_LONG).show();
|
Toast.LENGTH_LONG
|
||||||
|
).show()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
private fun startLeDeviceScan() {
|
||||||
|
|
||||||
private void startLeDeviceScan() {
|
|
||||||
if (bleScanner == null) {
|
if (bleScanner == null) {
|
||||||
aapsLogger.error(LTag.PUMPBTCOMM, "startLeDeviceScan failed: bleScanner is null");
|
aapsLogger.error(LTag.PUMPBTCOMM, "startLeDeviceScan failed: bleScanner is null")
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
deviceListAdapter.clear()
|
||||||
deviceListAdapter.clear();
|
deviceListAdapter.notifyDataSetChanged()
|
||||||
deviceListAdapter.notifyDataSetChanged();
|
handler.postDelayed(stopScanAfterTimeoutRunnable, SCAN_PERIOD_MILLIS)
|
||||||
|
runOnUiThread {
|
||||||
handler.postDelayed(stopScanAfterTimeoutRunnable, SCAN_PERIOD_MILLIS);
|
binding.rileyLinkBleConfigScanStart.isEnabled = false
|
||||||
|
binding.rileyLinkBleConfigButtonScanStop.visibility = View.VISIBLE
|
||||||
buttonStartScan.setEnabled(false);
|
}
|
||||||
buttonStopScan.setVisibility(View.VISIBLE);
|
scanning = true
|
||||||
|
bleScanner?.startScan(filters, settings, bleScanCallback)
|
||||||
scanning = true;
|
aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start")
|
||||||
bleScanner.startScan(filters, settings, bleScanCallback);
|
Toast.makeText(this@RileyLinkBLEConfigActivity, R.string.riley_link_ble_config_scan_scanning, Toast.LENGTH_SHORT).show()
|
||||||
aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start");
|
|
||||||
Toast.makeText(RileyLinkBLEConfigActivity.this, R.string.riley_link_ble_config_scan_scanning, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopLeDeviceScan() {
|
private fun stopLeDeviceScan() {
|
||||||
if (scanning) {
|
if (scanning) {
|
||||||
scanning = false;
|
scanning = false
|
||||||
|
bleScanner?.stopScan(bleScanCallback)
|
||||||
bleScanner.stopScan(bleScanCallback);
|
aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop")
|
||||||
|
Toast.makeText(this, R.string.riley_link_ble_config_scan_finished, Toast.LENGTH_SHORT).show()
|
||||||
aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop");
|
handler.removeCallbacks(stopScanAfterTimeoutRunnable)
|
||||||
Toast.makeText(this, R.string.riley_link_ble_config_scan_finished, Toast.LENGTH_SHORT).show();
|
}
|
||||||
handler.removeCallbacks(stopScanAfterTimeoutRunnable);
|
runOnUiThread {
|
||||||
|
binding.rileyLinkBleConfigScanStart.isEnabled = true
|
||||||
|
binding.rileyLinkBleConfigButtonScanStop.visibility = View.GONE
|
||||||
}
|
}
|
||||||
|
|
||||||
buttonStartScan.setEnabled(true);
|
|
||||||
buttonStopScan.setVisibility(View.GONE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class LeDeviceListAdapter extends BaseAdapter {
|
private inner class LeDeviceListAdapter : BaseAdapter() {
|
||||||
private final ArrayList<BluetoothDevice> mLeDevices;
|
|
||||||
private final Map<BluetoothDevice, Integer> rileyLinkDevices;
|
|
||||||
private final LayoutInflater mInflator;
|
|
||||||
|
|
||||||
public LeDeviceListAdapter() {
|
private val leDevices: ArrayList<BluetoothDevice> = ArrayList()
|
||||||
super();
|
private val rileyLinkDevices: MutableMap<BluetoothDevice, Int> = HashMap()
|
||||||
mLeDevices = new ArrayList<>();
|
|
||||||
rileyLinkDevices = new HashMap<>();
|
|
||||||
mInflator = RileyLinkBLEConfigActivity.this.getLayoutInflater();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addDevice(ScanResult result) {
|
fun addDevice(result: ScanResult) {
|
||||||
if (!mLeDevices.contains(result.getDevice())) {
|
if (!leDevices.contains(result.device)) {
|
||||||
mLeDevices.add(result.getDevice());
|
leDevices.add(result.device)
|
||||||
}
|
}
|
||||||
rileyLinkDevices.put(result.getDevice(), result.getRssi());
|
rileyLinkDevices[result.device] = result.rssi
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear() {
|
fun clear() {
|
||||||
mLeDevices.clear();
|
leDevices.clear()
|
||||||
rileyLinkDevices.clear();
|
rileyLinkDevices.clear()
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun getCount(): Int = leDevices.size
|
||||||
public int getCount() {
|
override fun getItem(i: Int): Any = leDevices[i]
|
||||||
return mLeDevices.size();
|
override fun getItemId(i: Int): Long = i.toLong()
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@SuppressLint("InflateParams")
|
||||||
public Object getItem(int i) {
|
override fun getView(i: Int, v: View?, viewGroup: ViewGroup): View {
|
||||||
return mLeDevices.get(i);
|
var view = v
|
||||||
}
|
val viewHolder: ViewHolder
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getItemId(int i) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View getView(int i, View view, ViewGroup viewGroup) {
|
|
||||||
ViewHolder viewHolder;
|
|
||||||
// General ListView optimization code.
|
// General ListView optimization code.
|
||||||
if (view == null) {
|
if (view == null) {
|
||||||
view = mInflator.inflate(R.layout.riley_link_ble_config_scan_item, null);
|
view = View.inflate(applicationContext, R.layout.riley_link_ble_config_scan_item, null)
|
||||||
viewHolder = new ViewHolder();
|
viewHolder = ViewHolder(view)
|
||||||
viewHolder.deviceAddress = view.findViewById(R.id.riley_link_ble_config_scan_item_device_address);
|
view.tag = viewHolder
|
||||||
viewHolder.deviceName = view.findViewById(R.id.riley_link_ble_config_scan_item_device_name);
|
} else viewHolder = view.tag as ViewHolder
|
||||||
view.setTag(viewHolder);
|
|
||||||
} else {
|
val device = leDevices[i]
|
||||||
viewHolder = (ViewHolder) view.getTag();
|
var deviceName = device.name
|
||||||
|
if (StringUtils.isBlank(deviceName)) deviceName = "RileyLink (?)"
|
||||||
|
deviceName += " [" + rileyLinkDevices[device] + "]"
|
||||||
|
val currentlySelectedAddress = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, "")
|
||||||
|
if (currentlySelectedAddress == device.address) {
|
||||||
|
deviceName += " (" + resources.getString(R.string.riley_link_ble_config_scan_selected) + ")"
|
||||||
}
|
}
|
||||||
|
viewHolder.deviceName.text = deviceName
|
||||||
BluetoothDevice device = mLeDevices.get(i);
|
viewHolder.deviceAddress.text = device.address
|
||||||
String deviceName = device.getName();
|
return view!!
|
||||||
|
|
||||||
if (StringUtils.isBlank(deviceName)) {
|
|
||||||
deviceName = "RileyLink (?)";
|
|
||||||
}
|
|
||||||
|
|
||||||
deviceName += " [" + rileyLinkDevices.get(device) + "]";
|
|
||||||
|
|
||||||
String currentlySelectedAddress = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, "");
|
|
||||||
|
|
||||||
if (currentlySelectedAddress.equals(device.getAddress())) {
|
|
||||||
deviceName += " (" + getResources().getString(R.string.riley_link_ble_config_scan_selected) + ")";
|
|
||||||
}
|
|
||||||
|
|
||||||
viewHolder.deviceName.setText(deviceName);
|
|
||||||
viewHolder.deviceAddress.setText(device.getAddress());
|
|
||||||
|
|
||||||
return view;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ViewHolder {
|
internal class ViewHolder(view: View) {
|
||||||
TextView deviceName;
|
|
||||||
TextView deviceAddress;
|
val deviceName: TextView = view.findViewById(R.id.riley_link_ble_config_scan_item_device_name)
|
||||||
|
val deviceAddress: TextView = view.findViewById(R.id.riley_link_ble_config_scan_item_device_address)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
companion object {
|
||||||
|
|
||||||
|
private const val SCAN_PERIOD_MILLIS: Long = 15000
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue