Skip to content

Commit 7784681

Browse files
authored
Add btleplug peripheral caching to get_platform() (fixes linux jvm) (#1079)
1 parent 05a9c38 commit 7784681

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
runner:
18-
- macos-13
18+
- macos-15-intel
1919
- ubuntu-24.04-arm
2020
- ubuntu-latest
2121
- windows-latest

kable-btleplug-ffi/src/peripheral.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::uuid::Uuid;
99
use crate::write_type::WriteType;
1010
use crate::{Error, get_adapter};
1111
use btleplug::api::{Central, CentralEvent, Peripheral as _, ScanFilter};
12-
use std::sync::Arc;
12+
use std::sync::{Arc, Mutex};
1313
use std::time::Duration;
1414
use tokio::time::timeout;
1515
use tokio_stream::StreamExt;
@@ -28,15 +28,26 @@ pub struct Peripheral {
2828
id: Arc<PeripheralId>,
2929
callbacks: Arc<Box<dyn PeripheralCallbacks>>,
3030
cancellation: CancellationHandle,
31+
cached_peripheral: Arc<Mutex<Option<btleplug::platform::Peripheral>>>,
3132
}
3233

3334
impl Peripheral {
3435
async fn get_platform(&self) -> Result<btleplug::platform::Peripheral> {
35-
get_adapter()
36-
.await
37-
.peripheral(&self.id.platform)
38-
.await
39-
.map_err(Into::into)
36+
let platform = self.cached_peripheral.lock().unwrap().clone();
37+
if platform.is_none() {
38+
match get_adapter()
39+
.await
40+
.peripheral(&self.id.platform)
41+
.await
42+
.map_err(Into::into)
43+
{
44+
Ok(peripheral) => *self.cached_peripheral.lock().unwrap() = Some(peripheral),
45+
Err(err) => return Err(err),
46+
}
47+
return Ok(self.cached_peripheral.lock().unwrap().clone().unwrap());
48+
}
49+
50+
Ok(platform.unwrap())
4051
}
4152

4253
async fn platform_connect(
@@ -95,7 +106,9 @@ impl Peripheral {
95106
id: id.clone(),
96107
callbacks: callbacks.clone(),
97108
cancellation: CancellationHandle::from_token(token.clone()),
109+
cached_peripheral: Arc::new(Mutex::new(None)),
98110
};
111+
let cached_peripheral = peripheral.cached_peripheral.clone();
99112

100113
std::thread::spawn(|| {
101114
let rt = tokio::runtime::Builder::new_current_thread()
@@ -115,7 +128,10 @@ impl Peripheral {
115128
CentralEvent::DeviceConnected(connected) =>
116129
if connected == id.platform { callbacks.connected() }
117130
CentralEvent::DeviceDisconnected(disconnected) =>
118-
if disconnected == id.platform { callbacks.disconnected() }
131+
if disconnected == id.platform {
132+
*cached_peripheral.lock().unwrap() = None;
133+
callbacks.disconnected()
134+
}
119135
_ => {}
120136
},
121137
}

kable-btleplug-ffi/src/peripheral_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Display for PeripheralId {
3535
impl PeripheralId {
3636
#[uniffi::constructor]
3737
fn new(value: String) -> Self {
38-
PeripheralId {
38+
Self {
3939
platform: serde_json::from_str(&value).unwrap(),
4040
}
4141
}

0 commit comments

Comments
 (0)