Add search device before Bluetooth connection

This commit is contained in:
bubble_devteam 2021-05-25 18:42:03 +08:00
parent 966f7b35f2
commit 5f5df3424e
6 changed files with 157 additions and 10 deletions

View file

@ -98,7 +98,11 @@
android:title="RileyLink Configuration"> android:title="RileyLink Configuration">
<intent android:action="info.nightscout.androidaps.plugins.PumpCommon.dialog.RileyLinkBLEConfigActivity" /> <intent android:action="info.nightscout.androidaps.plugins.PumpCommon.dialog.RileyLinkBLEConfigActivity" />
</Preference> </Preference>
<SwitchPreference
android:defaultValue="false"
android:key="@string/key_orange_use_scanning"
android:summary="@string/orange_use_scanning_level_summary"
android:title="@string/orange_use_scanning_level" />
<SwitchPreference <SwitchPreference
android:defaultValue="false" android:defaultValue="false"
android:key="@string/key_riley_link_show_battery_level" android:key="@string/key_riley_link_show_battery_level"

View file

@ -15,6 +15,11 @@
<intent android:action="info.nightscout.androidaps.plugins.PumpCommon.dialog.RileyLinkBLEConfigActivity" /> <intent android:action="info.nightscout.androidaps.plugins.PumpCommon.dialog.RileyLinkBLEConfigActivity" />
</Preference> </Preference>
<SwitchPreference
android:defaultValue="false"
android:key="@string/key_orange_use_scanning"
android:summary="@string/orange_use_scanning_level_summary"
android:title="@string/orange_use_scanning_level" />
<SwitchPreference <SwitchPreference
android:defaultValue="false" android:defaultValue="false"
android:key="@string/key_riley_link_show_battery_level" android:key="@string/key_riley_link_show_battery_level"

View file

@ -34,6 +34,7 @@ public class RileyLinkConst {
//public static final String RileyLinkAddress = PrefPrefix + "mac_address"; // pref_rileylink_mac_address //public static final String RileyLinkAddress = PrefPrefix + "mac_address"; // pref_rileylink_mac_address
public static final int RileyLinkAddress = R.string.key_rileylink_mac_address; public static final int RileyLinkAddress = R.string.key_rileylink_mac_address;
public static final int RileyLinkName = R.string.key_rileylink_name; public static final int RileyLinkName = R.string.key_rileylink_name;
public static final int OrangeUseScanning = R.string.key_orange_use_scanning;
public static final String LastGoodDeviceCommunicationTime = Prefix + "lastGoodDeviceCommunicationTime"; public static final String LastGoodDeviceCommunicationTime = Prefix + "lastGoodDeviceCommunicationTime";
public static final String LastGoodDeviceFrequency = Prefix + "LastGoodDeviceFrequency"; public static final String LastGoodDeviceFrequency = Prefix + "LastGoodDeviceFrequency";
public static final int Encoding = R.string.key_medtronic_encoding; public static final int Encoding = R.string.key_medtronic_encoding;

View file

@ -8,11 +8,19 @@ import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock; import android.os.SystemClock;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.UUID; import java.util.UUID;
@ -364,20 +372,34 @@ public class RileyLinkBLE {
return true; return true;
} }
String macAddress;
public void findRileyLink(String RileyLinkAddress) { public void findRileyLink(String RileyLinkAddress) {
aapsLogger.debug(LTag.PUMPBTCOMM, "RileyLink address: " + RileyLinkAddress); aapsLogger.debug(LTag.PUMPBTCOMM, "RileyLink address: " + RileyLinkAddress);
// Must verify that this is a valid MAC, or crash. // Must verify that this is a valid MAC, or crash.
macAddress = RileyLinkAddress;
boolean useScanning = sp.getBoolean(RileyLinkConst.Prefs.OrangeUseScanning, false);
if (useScanning) {
startScan();
} else {
rileyLinkDevice = bluetoothAdapter.getRemoteDevice(RileyLinkAddress); rileyLinkDevice = bluetoothAdapter.getRemoteDevice(RileyLinkAddress);
// if this succeeds, we get a connection state change callback? // if this succeeds, we get a connection state change callback?
if (rileyLinkDevice != null) { if (rileyLinkDevice != null) {
connectGatt(); connectGatt();
} else { } else {
aapsLogger.error(LTag.PUMPBTCOMM, "RileyLink device not found with address: " + RileyLinkAddress); aapsLogger.error(LTag.PUMPBTCOMM, "RileyLink device not found with address: " + RileyLinkAddress);
} }
} }
}
public void connectGattCheckOrange() {
boolean useScanning = sp.getBoolean(RileyLinkConst.Prefs.OrangeUseScanning, false);
if (useScanning) {
startScan();
} else {
connectGatt();
}
}
// This function must be run on UI thread. // This function must be run on UI thread.
@ -594,4 +616,117 @@ public class RileyLinkBLE {
return statusMessage; return statusMessage;
} }
private List<ScanFilter> buildScanFilters() {
ArrayList scanFilterList = new ArrayList<>();
ScanFilter.Builder scanFilterBuilder = new ScanFilter.Builder();
scanFilterBuilder.setDeviceAddress(macAddress);
scanFilterList.add(scanFilterBuilder.build());
return scanFilterList;
}
private ScanSettings buildScanSettings() {
ScanSettings.Builder scanSettingBuilder = new ScanSettings.Builder();
scanSettingBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
scanSettingBuilder.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE);
scanSettingBuilder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
return scanSettingBuilder.build();
}
public void startScan() {
try {
stopScan();
aapsLogger.debug(LTag.PUMPBTCOMM, "startScan");
handler.sendEmptyMessageDelayed(TIME_OUT_WHAT, TIME_OUT);
BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
if (bluetoothLeScanner == null) {
bluetoothAdapter.startLeScan(mLeScanCallback);
return;
}
bluetoothLeScanner.startScan(buildScanFilters(), buildScanSettings(), scanCallback);
} catch (Exception e) {
e.printStackTrace();
aapsLogger.error(LTag.PUMPBTCOMM, e.getMessage());
}
}
ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
String name = result.getDevice().getName();
String address = result.getDevice().getAddress();
if (macAddress.equals(address)) {
stopScan();
rileyLinkDevice = result.getDevice();
connectGatt();
}
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
stopScan();
}
};
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, final int rssi,
final byte[] scanRecord) {
if (macAddress.equals(device.getAddress())) {
stopScan();
rileyLinkDevice = device;
connectGatt();
}
}
};
public static final int TIME_OUT = 90 * 1000;
public static final int TIME_OUT_WHAT = 0x12;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case TIME_OUT_WHAT:
stopScan();
break;
}
}
};
public void stopScan() {
handler.removeMessages(TIME_OUT_WHAT);
if (bluetoothAdapter == null) {
return;
}
try {
BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
if (bluetoothLeScanner == null) {
if (isBluetoothAvailable()) {
bluetoothAdapter.stopLeScan(mLeScanCallback);
}
return;
}
if (isBluetoothAvailable()) {
bluetoothLeScanner.stopScan(scanCallback);
}
} catch (Exception e) {
e.printStackTrace();
aapsLogger.error(LTag.PUMPBTCOMM, e.getMessage());
}
}
public boolean isBluetoothAvailable() {
return (bluetoothAdapter != null &&
bluetoothAdapter.isEnabled() &&
bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
}
} }

View file

@ -38,7 +38,7 @@ public class DiscoverGattServicesTask extends ServiceTask {
RileyLinkPumpDevice pumpDevice = (RileyLinkPumpDevice) activePlugin.getActivePump(); RileyLinkPumpDevice pumpDevice = (RileyLinkPumpDevice) activePlugin.getActivePump();
if (needToConnect) { if (needToConnect) {
pumpDevice.getRileyLinkService().getRileyLinkBLE().connectGatt(); pumpDevice.getRileyLinkService().getRileyLinkBLE().connectGattCheckOrange();
} }
pumpDevice.getRileyLinkService().getRileyLinkBLE().discoverServices(); pumpDevice.getRileyLinkService().getRileyLinkBLE().discoverServices();

View file

@ -88,5 +88,7 @@
<item quantity="one">%1$d hour</item> <item quantity="one">%1$d hour</item>
<item quantity="other">%1$d hours</item> <item quantity="other">%1$d hours</item>
</plurals> </plurals>
<string name="orange_use_scanning_level">Use Scanning</string>
<string name="orange_use_scanning_level_summary">Scan before connecting to OrangeLink, it should improve connections (can also be used with other RileyLink clones, if needed)</string>
<string name="key_orange_use_scanning" translatable="false">pref_orange_use_scanning</string>
</resources> </resources>