Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
de2ae2a
Add Dexcom G7 Direct BG source
t1dude Sep 26, 2026
81c6cbe
Add Dexcom G7 alarms to the G7 Direct source
t1dude Sep 27, 2026
f2db9f9
G7: sensor end milestones, 24 h log, reconnect fix, G7 icon
t1dude Sep 27, 2026
ff1e6f7
Record the Dexcom G7 state keys in the preference snapshot
t1dude Sep 28, 2026
4a15469
G7: background reconnect after pairing, reminder waits for the readin…
t1dude Oct 3, 2026
487384d
G7: log the short links after a reading as one line
t1dude Oct 3, 2026
6fb10cd
G7: no 15-minute pause when the sensor accepts the key but not the bond
t1dude Oct 5, 2026
e524572
G7: test switch to pair into the sensor's watch slot
t1dude Oct 16, 2026
56be492
G7: move the sensor connection into :cgm:dexcomg7:core
t1dude Oct 16, 2026
6517222
G7: direct to watch
t1dude Oct 16, 2026
67517aa
Merge remote-tracking branch 'origin/dev' into g7-log-fixes
t1dude Oct 16, 2026
624383b
G7: fix the build after merging dev
t1dude Oct 16, 2026
0e7c5dc
G7: record the direct-to-watch key in the preference snapshot
t1dude Oct 16, 2026
e83e6d8
G7: direct to watch keeps working with the screen off
t1dude Oct 16, 2026
d681d93
Merge remote-tracking branch 'origin/dev' into g7-log-fixes
t1dude Oct 25, 2026
62cdb69
G7: tidy comments
t1dude Oct 25, 2026
edbf8f4
G7: open source alarm sounds from Trio, and a sound picker with an ur…
t1dude Oct 25, 2026
f42d041
Merge remote-tracking branch 'origin/dev' into g7-log-fixes
t1dude Oct 25, 2026
8650b9f
Merge remote-tracking branch 'origin/dev' into g7-log-fixes
t1dude Oct 25, 2026
87f8355
Improved pairing and logging.
t1dude Oct 26, 2026
30ffd80
G7: notifications on the certificate stream, like xDrip, and log the …
t1dude Oct 26, 2026
f614918
G7: say why the certificate stream uses notifications
t1dude Oct 26, 2026
de143f3
G7: open the link again after one missed reading, and when Bluetooth …
t1dude Oct 26, 2026
3cf95d0
G7: one handshake per link, a new connection request after a failed c…
t1dude Oct 26, 2026
8a23ef5
G7: no missed reading check before the sensor has sent anything
t1dude Oct 26, 2026
9afc6a8
G7: always a new connection request after a disconnect, never a re-ar…
t1dude Oct 27, 2026
2a0c734
G7: wait 5 s after a failed connect also right after a reading, and l…
t1dude Oct 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ dependencies {
implementation(project(":database:persistence"))
implementation(project(":pump:virtual"))
implementation(project(":workflow"))
// Dexcom G7 Direct BG source. Android only, so it is not under :plugins: (which :appshell pulls
// into its multiplatform commonMain).
implementation(project(":cgm:dexcomg7"))

// Pump drivers — only for full + pumpcontrol flavors. Derived from the :pump:* modules included
// in settings.gradle (single source of truth) minus one exception:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ContributedPluginsTest {
// sensitivity
100, 110, 120,
200, 210, 220, 230, 240,
400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550,
// BG sources. 445 is Dexcom G7 Direct, from :cgm:dexcomg7.
400, 410, 420, 430, 440, 445, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550,
// sync - the every-build part of the 300 block. SmsCommunicator 300 and Tidepool 320 are
// @NotNSClient; 340 (OpenHumans) comes from its own graph extension.
310, 330, 350, 360, 370,
Expand Down
3 changes: 3 additions & 0 deletions app/src/testFull/resources/prefs-schema.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ danars_v3_pairing_key_%s|DanaStringComposedKey|true
danars_v3_randompairing_key_%s|DanaStringComposedKey|true
danars_v3_randomsync_key_%s|DanaStringComposedKey|true
device_type|MedtrumIntNonKey|true
dexcom_g7_direct_alarm_state|G7StringNonKey|false
dexcom_g7_direct_state|G7StringNonKey|false
dexcom_g7_watch_direct|G7BooleanNonKey|true
dexcom_lognssensorchange|BooleanKey|true
dexcomg5_nsupload|BooleanKey|true
diaconn_bt_selector|DiaconnIntentKey|false
Expand Down
66 changes: 66 additions & 0 deletions cgm/dexcomg7/SOUNDS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Alarm sounds

The alarm sounds in `src/main/res/raw/g7_*.m4a` come from Trio, the open source app for people with
diabetes on iOS.

- Source: [nightscout/Trio pull request 1645](https://github.com/nightscout/Trio/pull/1645),
"Rework Trio Alarm Sounds: Add Urgency Levels and Voice Alarms", merged on 2026-10-19
(commit `cd6ad0706db1e7a546360d4631f75b24a402ba58`).
- Brought into Trio by Deniz Cengiz ([dnzxy](https://github.com/dnzxy)).
- The sounds were designed for Trio by a sound designer who is a diabetes dad and Trio user, and
who wants to stay anonymous.
- The picker, with one row per style and an urgency slider, follows the one in the same pull request.

Thank you to both of them.

## What we took

- The Acoustic and Electronic sets, each at three urgency levels (mild, medium, harsh), for these
Trio alarms: urgent low (`ulg`), low (`lg`), low soon (`lgs`), high (`hg`), device critical (`dcr`)
and device time-sensitive (`dts`).
- Eight extra tones from the same pull request: glass bell, harp, meow, saw wave, hard saw wave,
sine wave, open can and open wrapper.

Not taken: the carbs required set (we have no such alarm), the spoken alarms, and the older Trio
tones that were already there before that pull request.

Our alarms that have no set of their own share one, as Trio does for its normal device alerts: fall
rate uses low soon, rise rate uses high, and signal loss, sensor issue and sensor end use device
time-sensitive.

## What we changed

- Converted from CAF (16-bit PCM) to AAC at 96 kbit/s in an `.m4a` file, because Android does not
play CAF. Made with `afconvert -f m4af -d aac -b 96000` on macOS.
- Renamed to `g7_<style>_<alarm>_<level>.m4a` and `g7_tone_<name>.m4a`.

Nothing else was changed.

## Licence

Trio is under the MIT licence, and the sounds are part of Trio
([ACKNOWLEDGEMENTS.md](https://github.com/nightscout/Trio/blob/dev/ACKNOWLEDGEMENTS.md)):

```
MIT License

Copyright (c) 2021 Ivan Valkou

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
39 changes: 39 additions & 0 deletions cgm/dexcomg7/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.metro)
id("kotlinx-serialization")
id("android-module-dependencies")
id("test-module-dependencies")
id("jacoco-module-dependencies")
}

android {
namespace = "app.aaps.cgm.dexcomg7"
}

// Dexcom G7 / ONE+ read straight over Bluetooth, as a BG source. Android only: the Bluetooth link, the
// camera for the applicator barcode, and the screens. The wire protocol is in :cgm:dexcomg7:protocol.
dependencies {
api(project(":cgm:dexcomg7:core"))
implementation(project(":core:data"))
implementation(project(":core:interfaces"))
implementation(project(":core:keys"))
implementation(project(":core:objects"))
implementation(project(":core:utils"))
implementation(project(":core:ui"))
// The readings list every BG source shows (view, remove, duplicates marked).
implementation(project(":plugins:source"))

implementation(libs.androidx.camera.camera2)
implementation(libs.androidx.camera.lifecycle)
implementation(libs.androidx.camera.view)
implementation(libs.com.google.zxing.core)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.lifecycle.runtime.compose)

implementation(libs.androidx.ui.tooling.preview)
debugImplementation(libs.androidx.ui.tooling)

testImplementation(project(":shared:tests"))
}
24 changes: 24 additions & 0 deletions cgm/dexcomg7/core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.metro)
id("kotlinx-serialization")
id("android-module-dependencies")
id("test-module-dependencies")
id("jacoco-module-dependencies")
}

android {
namespace = "app.aaps.cgm.dexcomg7.core"
}

// The Dexcom G7 / ONE+ connection without any screens: the Bluetooth link, pairing, the session that
// collects readings, and the saved sensor state. Kept apart from :cgm:dexcomg7 so the watch app can
// run the same connection in the sensor's watch slot. Needs nothing the watch app does not have.
dependencies {
api(project(":cgm:dexcomg7:protocol"))
implementation(project(":core:data"))
implementation(project(":core:interfaces"))
implementation(project(":core:keys"))

testImplementation(project(":shared:tests"))
}
8 changes: 8 additions & 0 deletions cgm/dexcomg7/core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package app.aaps.cgm.dexcomg7.ble

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
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.pm.PackageManager
import android.os.ParcelUuid
import androidx.core.content.ContextCompat
import app.aaps.cgm.dexcomg7.protocol.G7Advertisement
import app.aaps.cgm.dexcomg7.protocol.G7Gatt
import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.Inject
import dev.zacsweers.metro.SingleIn
import java.util.UUID

/** One advertisement seen during a scan. */
class G7ScanResult(val address: String, val advertisement: G7Advertisement, val rssi: Int)

/**
* The Android Bluetooth adapter, as far as this driver needs it: permission checks, a filtered scan,
* and looking up a device by address.
*/
@SuppressLint("MissingPermission")
@SingleIn(AppScope::class)
@Inject
class G7Bluetooth(private val context: Context) {

private val adapter: BluetoothAdapter?
get() = (context.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager)?.adapter

val isEnabled: Boolean get() = runCatching { adapter?.isEnabled == true }.getOrDefault(false)

/**
* True when this app may scan and connect. False is normal on a fresh install: plugins start before
* the permission has been asked for. Checked before every scan, because a scan without the
* permission throws on a Bluetooth thread where nothing catches it, and the app is killed.
*/
val hasPermissions: Boolean
get() = ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED

/** Why nothing could work right now, or null when Bluetooth is usable. */
fun blockedReason(): String? = when {
!hasPermissions -> "Bluetooth permission not granted"
adapter == null -> "No Bluetooth adapter"
!isEnabled -> "Bluetooth is off"
else -> null
}

fun device(address: String): BluetoothDevice? = runCatching { adapter?.getRemoteDevice(address) }.getOrNull()

private var scanCallback: ScanCallback? = null

/**
* Starts a scan filtered on the service UUID a sensor advertises (FEBC). An unfiltered scan with the
* screen off returns nothing on current Android. Returns false when the scan could not start.
*/
fun startScan(onResult: (G7ScanResult) -> Unit, onFailed: (String) -> Unit): Boolean {
stopScan()
blockedReason()?.let {
onFailed(it)
return false
}
val scanner = adapter?.bluetoothLeScanner ?: run {
onFailed("No Bluetooth scanner")
return false
}
val filter = ScanFilter.Builder()
.setServiceUuid(ParcelUuid(UUID.fromString(G7Gatt.ADVERTISEMENT_SERVICE)), ParcelUuid(UUID.fromString(G7Gatt.ADVERTISEMENT_SERVICE_MASK)))
.build()
val settings = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()
val callback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
val name = result.scanRecord?.deviceName ?: runCatching { result.device.name }.getOrNull() ?: return
val manufacturerData = result.scanRecord?.getManufacturerSpecificData(G7Gatt.MANUFACTURER_ID)
onResult(G7ScanResult(result.device.address, G7Advertisement(name, manufacturerData), result.rssi))
}

override fun onBatchScanResults(results: MutableList<ScanResult>) {
results.forEach { onScanResult(ScanSettings.CALLBACK_TYPE_ALL_MATCHES, it) }
}

override fun onScanFailed(errorCode: Int) {
onFailed("Scan failed: $errorCode")
}
}
scanCallback = callback
return runCatching { scanner.startScan(listOf(filter), settings, callback) }
.onFailure { onFailed("Bluetooth refused the scan: ${it.message}") }
.isSuccess
}

fun stopScan() {
val callback = scanCallback ?: return
scanCallback = null
runCatching { adapter?.bluetoothLeScanner?.stopScan(callback) }
}
}
Loading
Loading