Skip to content

Commit a4b07c6

Browse files
committed
chore: Update android for local_name/advertising_name split
1 parent 2069f90 commit a4b07c6

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Peripheral.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public boolean isConnected() {
125125
return this.connected;
126126
}
127127

128+
@SuppressLint("MissingPermission")
129+
public String getDeviceName() {
130+
return this.device.getName();
131+
}
132+
128133
@SuppressLint("MissingPermission")
129134
public Future<byte[]> read(UUID uuid) {
130135
SimpleFuture<byte[]> future = new SimpleFuture<>();

src/droidplug/jni/objects.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct JPeripheral<'a: 'b, 'b> {
2424
get_notifications: JMethodID<'a>,
2525
read_descriptor: JMethodID<'a>,
2626
write_descriptor: JMethodID<'a>,
27+
get_device_name: JMethodID<'a>,
2728
env: &'b JNIEnv<'a>,
2829
}
2930

@@ -102,6 +103,8 @@ impl<'a: 'b, 'b> JPeripheral<'a, 'b> {
102103
"writeDescriptor",
103104
"(Ljava/util/UUID;Ljava/util/UUID;[BI)Lio/github/gedgygedgy/rust/future/Future;",
104105
)?;
106+
let get_device_name =
107+
env.get_method_id(class, "getDeviceName", "()Ljava/lang/String;")?;
105108
Ok(Self {
106109
internal: obj,
107110
connect,
@@ -114,6 +117,7 @@ impl<'a: 'b, 'b> JPeripheral<'a, 'b> {
114117
get_notifications,
115118
read_descriptor,
116119
write_descriptor,
120+
get_device_name,
117121
env,
118122
})
119123
}
@@ -265,6 +269,24 @@ impl<'a: 'b, 'b> JPeripheral<'a, 'b> {
265269
JFuture::from_env(self.env, future_obj)
266270
}
267271

272+
pub fn get_device_name(&self) -> Result<Option<String>> {
273+
let obj = self
274+
.env
275+
.call_method_unchecked(
276+
self.internal,
277+
self.get_device_name,
278+
JavaType::Object("Ljava/lang/String;".to_string()),
279+
&[],
280+
)?
281+
.l()?;
282+
if obj.is_null() {
283+
Ok(None)
284+
} else {
285+
let name_str = self.env.get_string(obj.into())?;
286+
Ok(Some(name_str.into()))
287+
}
288+
}
289+
268290
pub fn write_descriptor(
269291
&self,
270292
characteristic: JUuid<'a, 'b>,
@@ -732,8 +754,8 @@ impl<'a: 'b, 'b> TryFrom<JScanResult<'a, 'b>> for (BDAddr, Option<PeripheralProp
732754
Some(PeripheralProperties {
733755
address: addr,
734756
address_type: None,
735-
local_name: device_name,
736-
advertisement_name: None,
757+
local_name: device_name.clone(),
758+
advertisement_name: device_name,
737759
tx_power_level,
738760
manufacturer_data,
739761
service_data,

src/droidplug/peripheral.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,18 @@ impl api::Peripheral for Peripheral {
249249
self.with_obj(|env, _obj| {
250250
let result = JPollResult::from_env(env, result_ref.as_obj())?;
251251
get_poll_result(env, result).map(|_| {})
252-
})
252+
})?;
253+
// Query the system-cached device name and update local_name
254+
self.with_obj(|_env, obj| -> std::result::Result<(), Error> {
255+
if let Ok(Some(name)) = obj.get_device_name() {
256+
let mut guard = self.shared.lock().map_err(Into::<Error>::into)?;
257+
if let Some(ref mut props) = guard.properties {
258+
props.local_name = Some(name);
259+
}
260+
}
261+
Ok(())
262+
})?;
263+
Ok(())
253264
}
254265

255266
async fn disconnect(&self) -> Result<()> {

0 commit comments

Comments
 (0)