Add search device before Bluetooth connection
This commit is contained in:
parent
966f7b35f2
commit
5f5df3424e
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -34,6 +34,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;
|
||||
|
|
|
@ -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;
|
||||
|
@ -364,18 +372,32 @@ public class RileyLinkBLE {
|
|||
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 +616,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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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>
|
Loading…
Reference in a new issue