Merge pull request #536 from bubbledevteam/dev

Add search device before Bluetooth connection
This commit is contained in:
Milos Kozak 2021-10-13 18:45:51 +02:00 committed by GitHub
commit b1dfc8da04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 201 additions and 13 deletions

View file

@ -98,7 +98,11 @@
android:title="RileyLink Configuration">
<intent android:action="info.nightscout.androidaps.plugins.PumpCommon.dialog.RileyLinkBLEConfigActivity" />
</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
android:defaultValue="false"
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" />
</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
android:defaultValue="false"
android:key="@string/key_riley_link_show_battery_level"

View file

@ -32,6 +32,7 @@ public class RileyLinkConst {
//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 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 LastGoodDeviceFrequency = Prefix + "LastGoodDeviceFrequency";
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.BluetoothGattService;
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.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
@ -84,6 +92,17 @@ public class RileyLinkBLE {
if (radioResponseCountNotified != null) {
radioResponseCountNotified.run();
}
if (characteristic.getUuid().toString().equals(GattAttributes.UUID_NOTIF_CHARACTER.toString())) {
final byte[] data = characteristic.getValue();
int first = 0xff & data[0];
aapsLogger.info(LTag.PUMPBTCOMM,
"onCharacteristicChanged " + ByteUtil.shortHexString(characteristic.getValue()) + "=====" + first);
String fv = data[3] + "." + data[4];
String hv = data[5] + "." + data[6];
rileyLinkServiceData.versionOrangeFV = fv;
rileyLinkServiceData.versionOrangeHV = hv;
}
}
@ -234,6 +253,9 @@ public class RileyLinkBLE {
if (gattDebugEnabled) {
debugService(service, 0);
}
if (GattAttributes.isOrange(uuidService)) {
rileyLinkServiceData.isOrange = true;
}
}
if (gattDebugEnabled) {
@ -361,21 +383,47 @@ public class RileyLinkBLE {
aapsLogger.error(LTag.PUMPBTCOMM, "Error setting response count notification");
return false;
}
if(rileyLinkServiceData.isOrange){
enableNotificationsOrange();
}
return true;
}
public boolean enableNotificationsOrange() {
aapsLogger.error(LTag.PUMPBTCOMM, "enableNotificationsORG");
BLECommOperationResult result = setNotification_blocking(GattAttributes.UUID_NOTIF_SERVICE, //
GattAttributes.UUID_NOTIF_CHARACTER);
if (result.resultCode != BLECommOperationResult.RESULT_SUCCESS) {
aapsLogger.error(LTag.PUMPBTCOMM, "Error setting response count notification");
return false;
}
return true;
}
String macAddress;
public void findRileyLink(String RileyLinkAddress) {
aapsLogger.debug(LTag.PUMPBTCOMM, "RileyLink address: " + RileyLinkAddress);
// Must verify that this is a valid MAC, or crash.
rileyLinkDevice = bluetoothAdapter.getRemoteDevice(RileyLinkAddress);
// if this succeeds, we get a connection state change callback?
if (rileyLinkDevice != null) {
connectGatt();
macAddress = RileyLinkAddress;
boolean useScanning = sp.getBoolean(RileyLinkConst.Prefs.OrangeUseScanning, false);
if (useScanning) {
startScan();
} else {
aapsLogger.error(LTag.PUMPBTCOMM, "RileyLink device not found with address: " + RileyLinkAddress);
rileyLinkDevice = bluetoothAdapter.getRemoteDevice(RileyLinkAddress);
// if this succeeds, we get a connection state change callback?
if (rileyLinkDevice != null) {
connectGatt();
} else {
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();
}
}
@ -594,4 +642,117 @@ public class RileyLinkBLE {
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

@ -29,6 +29,9 @@ public class GattAttributes {
public static String CHARA_RADIO_CUSTOM_NAME = "d93b2af0-1e28-11e4-8c21-0800200c9a66";
public static String CHARA_RADIO_VERSION = "30d99dc9-7c91-4295-a051-0a104d238cf2";
public static String CHARA_RADIO_LED_MODE = "c6d84241-f1a7-4f9c-a25f-fce16732f14e";
//Orange Radio Service
public static UUID UUID_NOTIF_SERVICE = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
public static UUID UUID_NOTIF_CHARACTER = UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
private static final Map<String, String> attributes;
private static final Map<String, String> attributesRileyLinkSpecific;
@ -83,5 +86,8 @@ public class GattAttributes {
public static boolean isRileyLink(UUID uuid) {
return attributesRileyLinkSpecific.containsKey(uuid.toString());
}
public static boolean isOrange(UUID uuid) {
return UUID_NOTIF_SERVICE.equals(uuid.toString());
}
}

View file

@ -107,8 +107,14 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re
RileyLinkError rileyLinkError = rileyLinkServiceData.rileyLinkError;
this.connectionError.setText(rileyLinkError == null ? PLACEHOLDER : resourceHelper.gs(rileyLinkError.getResourceId(targetDevice)));
this.firmwareVersion.setText(resourceHelper.gs(R.string.rileylink_firmware_version_value,
Optional.ofNullable(rileyLinkServiceData.versionBLE113).orElse(PLACEHOLDER), Optional.ofNullable(rileyLinkServiceData.versionCC110).orElse(PLACEHOLDER)));
if(rileyLinkServiceData.isOrange){
this.firmwareVersion.setText("FV:"+Optional.ofNullable(rileyLinkServiceData.versionOrangeFV).orElse(PLACEHOLDER)+"\nHV:"+Optional.ofNullable(rileyLinkServiceData.versionOrangeHV).orElse(PLACEHOLDER));
}else{
this.firmwareVersion.setText(resourceHelper.gs(R.string.rileylink_firmware_version_value,
Optional.ofNullable(rileyLinkServiceData.versionBLE113).orElse(PLACEHOLDER), Optional.ofNullable(rileyLinkServiceData.versionCC110).orElse(PLACEHOLDER)));
}
}
RileyLinkPumpDevice rileyLinkPumpDevice = (RileyLinkPumpDevice) activePlugin.getActivePump();

View file

@ -48,6 +48,9 @@ public class RileyLinkServiceData {
public String versionBLE113;
// radio version
public String versionCC110;
public boolean isOrange;
public String versionOrangeFV;
public String versionOrangeHV;
public RileyLinkTargetDevice targetDevice;

View file

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

View file

@ -88,5 +88,7 @@
<item quantity="one">%1$d hour</item>
<item quantity="other">%1$d hours</item>
</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>