btleplug 0.12.0

A Cross-Platform Rust Bluetooth Low Energy (BLE) GATT library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use super::jni_utils::{
    arrays::byte_array_to_vec,
    exceptions::try_block,
    future::{JFuture, JSendFuture},
    stream::JSendStream,
    task::JPollResult,
    uuid::JUuid,
};
use crate::{
    Error, Result,
    api::{
        self, BDAddr, Characteristic, ConnectionParameterPreset, ConnectionParameters, Descriptor,
        PeripheralProperties, Service, ValueNotification, WriteType,
    },
};
use async_trait::async_trait;
use futures::stream::Stream;
use jni::{
    JNIEnv,
    objects::{GlobalRef, JList, JObject},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "serde")]
use serde_cr as serde;
use std::{
    collections::BTreeSet,
    convert::TryFrom,
    fmt::{self, Debug, Display, Formatter},
    pin::Pin,
    sync::atomic::{AtomicU16, Ordering},
    sync::{Arc, Mutex},
};
use uuid::Uuid;

use super::jni::{
    global_jvm,
    objects::{JBluetoothGattCharacteristic, JBluetoothGattService, JPeripheral},
};
use jni::objects::JClass;
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_cr")
)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PeripheralId(pub(super) BDAddr);
impl Display for PeripheralId {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Display::fmt(&self.0, f)
    }
}

fn get_poll_result<'a: 'b, 'b>(
    env: &'b JNIEnv<'a>,
    result: JPollResult<'a, 'b>,
) -> Result<JObject<'a>> {
    try_block(env, || Ok(Ok(result.get()?)))
        .catch(
            JClass::from(
                super::jni_utils::classcache::get_class(
                    "io/github/gedgygedgy/rust/future/FutureException",
                )
                .unwrap()
                .as_obj(),
            ),
            |ex| {
                let cause = env
                    .call_method(ex, "getCause", "()Ljava/lang/Throwable;", &[])?
                    .l()?;
                if env.is_instance_of(
                    cause,
                    JClass::from(
                        super::jni_utils::classcache::get_class(
                            "com/nonpolynomial/btleplug/android/impl/NotConnectedException",
                        )
                        .unwrap()
                        .as_obj(),
                    ),
                )? {
                    Ok(Err(Error::NotConnected))
                } else if env.is_instance_of(
                    cause,
                    JClass::from(
                        super::jni_utils::classcache::get_class(
                            "com/nonpolynomial/btleplug/android/impl/PermissionDeniedException",
                        )
                        .unwrap()
                        .as_obj(),
                    ),
                )? {
                    Ok(Err(Error::PermissionDenied))
                } else if env.is_instance_of(
                    cause,
                    JClass::from(
                        super::jni_utils::classcache::get_class(
                            "com/nonpolynomial/btleplug/android/impl/UnexpectedCallbackException",
                        )
                        .unwrap()
                        .as_obj(),
                    ),
                )? {
                    Ok(Err(Error::UnexpectedCallback))
                } else if env.is_instance_of(
                    cause,
                    JClass::from(
                        super::jni_utils::classcache::get_class(
                            "com/nonpolynomial/btleplug/android/impl/UnexpectedCharacteristicException",
                        )
                        .unwrap()
                        .as_obj(),
                    ),
                )? {
                    Ok(Err(Error::UnexpectedCharacteristic))
                } else if env.is_instance_of(
                    cause,
                    JClass::from(
                        super::jni_utils::classcache::get_class(
                            "com/nonpolynomial/btleplug/android/impl/NoSuchCharacteristicException",
                        )
                        .unwrap()
                        .as_obj(),
                    ),
                )? {
                    Ok(Err(Error::NoSuchCharacteristic))
                } else if env.is_instance_of(
                    cause,
                    JClass::from(
                        super::jni_utils::classcache::get_class(
                            "com/nonpolynomial/btleplug/android/impl/NoBluetoothAdapterException",
                        )
                        .unwrap()
                        .as_obj(),
                    ),
                )? {
                    Ok(Err(Error::NoAdapterAvailable))
                } else if env.is_instance_of(
                    cause,
                    "java/lang/RuntimeException",
                )? {
                    let msg = env
                        .call_method(cause, "getMessage", "()Ljava/lang/String;", &[])
                        .unwrap()
                        .l()
                        .unwrap();
                    let msgstr:String = env.get_string(msg.into()).unwrap().into();
                    Ok(Err(Error::RuntimeError(msgstr)))
                } else {
                    env.throw(ex)?;
                    Err(jni::errors::Error::JavaException)
                }
            },
        )
        .result()?
}

#[derive(Debug)]
struct PeripheralShared {
    services: BTreeSet<Service>,
    characteristics: BTreeSet<Characteristic>,
    properties: Option<PeripheralProperties>,
    mtu: AtomicU16,
}

#[derive(Clone)]
pub struct Peripheral {
    addr: BDAddr,
    internal: GlobalRef,
    shared: Arc<Mutex<PeripheralShared>>,
    mtu: Arc<AtomicU16>,
}

impl Peripheral {
    pub(crate) fn new(env: &JNIEnv, adapter: JObject, addr: BDAddr) -> Result<Self> {
        let obj = JPeripheral::new(env, adapter, addr)?;
        Ok(Self {
            addr,
            internal: env.new_global_ref(obj)?,
            shared: Arc::new(Mutex::new(PeripheralShared {
                services: BTreeSet::new(),
                characteristics: BTreeSet::new(),
                properties: None,
                mtu: AtomicU16::new(crate::api::DEFAULT_MTU_SIZE),
            })),
            mtu: Arc::new(AtomicU16::new(crate::api::DEFAULT_MTU_SIZE)),
        })
    }

    pub(crate) fn report_properties(&self, properties: PeripheralProperties) {
        let mut guard = self.shared.lock().unwrap();

        guard.properties = Some(properties);
    }

    fn with_obj<T, E>(
        &self,
        f: impl FnOnce(&JNIEnv, JPeripheral) -> std::result::Result<T, E>,
    ) -> std::result::Result<T, E>
    where
        E: From<::jni::errors::Error>,
    {
        let env = global_jvm().get_env()?;
        let obj = JPeripheral::from_env(&env, self.internal.as_obj())?;
        f(&env, obj)
    }

    async fn set_characteristic_notification(
        &self,
        characteristic: &Characteristic,
        enable: bool,
    ) -> Result<()> {
        let future = self.with_obj(|env, obj| {
            let uuid_obj = JUuid::new(env, characteristic.uuid)?;
            JSendFuture::try_from(obj.set_characteristic_notification(uuid_obj, enable)?)
        })?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            get_poll_result(env, result).map(|_| {})
        })
    }
}

impl Debug for Peripheral {
    fn fmt(&self, fmt: &mut Formatter) -> std::result::Result<(), std::fmt::Error> {
        write!(fmt, "{:?}", self.internal.as_obj())
    }
}

#[async_trait]
impl api::Peripheral for Peripheral {
    /// Returns the unique identifier of the peripheral.
    fn id(&self) -> PeripheralId {
        PeripheralId(self.addr)
    }

    fn address(&self) -> BDAddr {
        self.addr
    }

    fn mtu(&self) -> u16 {
        self.mtu.load(Ordering::Relaxed)
    }

    async fn properties(&self) -> Result<Option<PeripheralProperties>> {
        let guard = self.shared.lock().map_err(Into::<Error>::into)?;
        Ok((&guard.properties).clone())
    }

    fn characteristics(&self) -> BTreeSet<Characteristic> {
        let guard = self.shared.lock().unwrap();
        (&guard.characteristics).clone()
    }

    async fn is_connected(&self) -> Result<bool> {
        self.with_obj(|_env, obj| Ok(obj.is_connected()?))
    }

    async fn connect(&self) -> Result<()> {
        let future = self.with_obj(|env, obj| JSendFuture::try_from(obj.connect()?))?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            get_poll_result(env, result).map(|_| {})
        })?;
        // Query the system-cached device name and update local_name
        self.with_obj(|_env, obj| -> std::result::Result<(), Error> {
            if let Ok(Some(name)) = obj.get_device_name() {
                let mut guard = self.shared.lock().map_err(Into::<Error>::into)?;
                if let Some(ref mut props) = guard.properties {
                    props.local_name = Some(name);
                }
            }
            Ok(())
        })?;
        // Auto-negotiate maximum MTU (517) after connection
        let mtu_future = self.with_obj(|env, obj| {
            JSendFuture::try_from(JFuture::from_env(env, obj.request_mtu(517)?)?)
        })?;
        let mtu_result_ref = mtu_future.await?;
        self.with_obj(|env, _obj| -> Result<()> {
            let mtu_result = JPollResult::from_env(env, mtu_result_ref.as_obj())?;
            let mtu_obj = get_poll_result(env, mtu_result)?;
            let mtu_val = env.call_method(mtu_obj, "intValue", "()I", &[])?.i()?;
            self.mtu.store(mtu_val as u16, Ordering::Relaxed);
            Ok(())
        })?;
        Ok(())
    }

    async fn disconnect(&self) -> Result<()> {
        let future = self.with_obj(|env, obj| JSendFuture::try_from(obj.disconnect()?))?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            get_poll_result(env, result).map(|_| {})
        })
    }

    /// The set of services we've discovered for this device. This will be empty until
    /// `discover_services` is called.
    fn services(&self) -> BTreeSet<Service> {
        let guard = self.shared.lock().unwrap();
        (&guard.services).clone()
    }

    async fn discover_services(&self) -> Result<()> {
        let future = self.with_obj(|env, obj| JSendFuture::try_from(obj.discover_services()?))?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            use std::iter::FromIterator;

            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            let obj = get_poll_result(env, result)?;
            let list = JList::from_env(env, obj)?;
            let mut peripheral_services = Vec::new();
            let mut peripheral_characteristics = Vec::new();

            for service in list.iter()? {
                let service = JBluetoothGattService::from_env(env, service)?;
                let mut characteristics = BTreeSet::<Characteristic>::new();
                for characteristic in service.get_characteristics()? {
                    let mut descriptors = BTreeSet::new();
                    for descriptor in characteristic.get_descriptors()? {
                        descriptors.insert(Descriptor {
                            uuid: descriptor.get_uuid()?,
                            service_uuid: service.get_uuid()?,
                            characteristic_uuid: characteristic.get_uuid()?,
                        });
                    }
                    let char = Characteristic {
                        service_uuid: service.get_uuid()?,
                        uuid: characteristic.get_uuid()?,
                        properties: characteristic.get_properties()?,
                        descriptors: descriptors.clone(),
                    };
                    // Only consider the first characteristic of each UUID
                    // This "should" be unique, but of course it's not enforced
                    if characteristics
                        .iter()
                        .filter(|c| c.service_uuid == char.service_uuid && c.uuid == char.uuid)
                        .count()
                        == 0
                    {
                        characteristics.insert(char.clone());
                        peripheral_characteristics.push(char.clone());
                    }
                }
                peripheral_services.push(Service {
                    uuid: service.get_uuid()?,
                    primary: service.is_primary()?,
                    characteristics,
                })
            }
            let mut guard = self.shared.lock().map_err(Into::<Error>::into)?;
            guard.services = BTreeSet::from_iter(peripheral_services.clone());
            guard.characteristics = BTreeSet::from_iter(peripheral_characteristics.clone());
            Ok(())
        })
    }

    async fn write(
        &self,
        characteristic: &Characteristic,
        data: &[u8],
        write_type: WriteType,
    ) -> Result<()> {
        let future = self.with_obj(|env, obj| {
            let uuid = JUuid::new(env, characteristic.uuid)?;
            let data_obj = super::jni_utils::arrays::slice_to_byte_array(env, data)?;
            let write_type = match write_type {
                WriteType::WithResponse => 2,
                WriteType::WithoutResponse => 1,
            };
            JSendFuture::try_from(obj.write(uuid, data_obj.into(), write_type)?)
        })?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            get_poll_result(env, result).map(|_| {})
        })
    }

    async fn read(&self, characteristic: &Characteristic) -> Result<Vec<u8>> {
        let future = self.with_obj(|env, obj| {
            let uuid = JUuid::new(env, characteristic.uuid)?;
            JSendFuture::try_from(obj.read(uuid)?)
        })?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            let bytes = get_poll_result(env, result)?;
            Ok(byte_array_to_vec(env, bytes.into_inner())?)
        })
    }

    async fn subscribe(&self, characteristic: &Characteristic) -> Result<()> {
        self.set_characteristic_notification(characteristic, true)
            .await
    }

    async fn unsubscribe(&self, characteristic: &Characteristic) -> Result<()> {
        self.set_characteristic_notification(characteristic, false)
            .await
    }

    async fn notifications(&self) -> Result<Pin<Box<dyn Stream<Item = ValueNotification> + Send>>> {
        use futures::stream::StreamExt;
        let shared = self.shared.clone();
        let stream = self.with_obj(|_env, obj| JSendStream::try_from(obj.get_notifications()?))?;
        let stream = stream
            .map(move |item| match item {
                Ok(item) => {
                    let env = global_jvm().get_env()?;
                    let item = item.as_obj();
                    let characteristic = JBluetoothGattCharacteristic::from_env(&env, item)?;
                    let uuid = characteristic.get_uuid()?;
                    let value = characteristic.get_value()?;
                    let service_uuid = shared
                        .lock()
                        .ok()
                        .and_then(|guard| {
                            guard
                                .services
                                .iter()
                                .find(|s| s.characteristics.iter().any(|c| c.uuid == uuid))
                                .map(|s| s.uuid)
                        })
                        .unwrap_or_default();
                    Ok(ValueNotification {
                        uuid,
                        service_uuid,
                        value,
                    })
                }
                Err(err) => Err(err),
            })
            .filter_map(|item| async { item.ok() });
        Ok(Box::pin(stream))
    }

    async fn read_rssi(&self) -> Result<i16> {
        let future = self.with_obj(|env, obj| {
            JSendFuture::try_from(JFuture::from_env(env, obj.read_remote_rssi()?)?)
        })?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            let rssi_obj = get_poll_result(env, result)?;
            let rssi_val = env.call_method(rssi_obj, "intValue", "()I", &[])?.i()?;
            Ok(rssi_val as i16)
        })
    }

    async fn write_descriptor(&self, descriptor: &Descriptor, data: &[u8]) -> Result<()> {
        let future = self.with_obj(|env, obj| {
            let characteristic = JUuid::new(env, descriptor.characteristic_uuid)?;
            let uuid = JUuid::new(env, descriptor.uuid)?;
            let data_obj = super::jni_utils::arrays::slice_to_byte_array(env, data)?;
            JSendFuture::try_from(obj.write_descriptor(characteristic, uuid, data_obj.into())?)
        })?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            get_poll_result(env, result).map(|_| {})
        })
    }

    async fn read_descriptor(&self, descriptor: &Descriptor) -> Result<Vec<u8>> {
        let future = self.with_obj(|env, obj| {
            let characteristic = JUuid::new(env, descriptor.characteristic_uuid)?;
            let uuid = JUuid::new(env, descriptor.uuid)?;
            JSendFuture::try_from(obj.read_descriptor(characteristic, uuid)?)
        })?;
        let result_ref = future.await?;
        self.with_obj(|env, _obj| {
            let result = JPollResult::from_env(env, result_ref.as_obj())?;
            let bytes = get_poll_result(env, result)?;
            Ok(byte_array_to_vec(env, bytes.into_inner())?)
        })
    }

    async fn connection_parameters(&self) -> Result<Option<ConnectionParameters>> {
        self.with_obj(|_env, obj| {
            Ok(obj
                .get_connection_parameters()
                .map_err(|e| Error::Other(format!("{:?}", e).into()))?)
        })
    }

    async fn request_connection_parameters(&self, preset: ConnectionParameterPreset) -> Result<()> {
        let priority = match preset {
            ConnectionParameterPreset::Balanced => 0, // CONNECTION_PRIORITY_BALANCED
            ConnectionParameterPreset::ThroughputOptimized => 1, // CONNECTION_PRIORITY_HIGH
            ConnectionParameterPreset::PowerOptimized => 2, // CONNECTION_PRIORITY_LOW_POWER
        };
        self.with_obj(|_env, obj| {
            let success = obj
                .request_connection_priority(priority)
                .map_err(|e| Error::Other(format!("{:?}", e).into()))?;
            if success {
                Ok(())
            } else {
                Err(Error::RuntimeError(
                    "requestConnectionPriority returned false".to_string(),
                ))
            }
        })
    }
}