bluetooth_rust/
bluetooth_uuid.rs

1//! UUID stuff for android bluetooth
2
3#[cfg(target_os = "android")]
4use super::android::Java;
5#[cfg(target_os = "android")]
6use super::android::jerr;
7#[cfg(target_os = "android")]
8use jni_min_helper::*;
9#[cfg(target_os = "android")]
10use std::sync::{Arc, Mutex};
11
12/// Represents the uuid for a bluetooth service
13#[derive(Debug, PartialEq)]
14pub enum BluetoothUuid {
15    /// Android auto
16    AndroidAuto,
17    /// Serial port protocol
18    SPP,
19    /// a2dp source
20    A2dpSource,
21    /// a2dp sink
22    A2dpSink,
23    /// base bluetooth profile
24    Base,
25    /// headset protocol, hs
26    HspHs,
27    /// headset protocol ag
28    HspAg,
29    /// handsfree protocol, ag
30    HfpAg,
31    /// Handsfree protocol, hs
32    HfpHs,
33    /// Obex opp protocol
34    ObexOpp,
35    /// Obex ftp protocol
36    ObexFtp,
37    /// Obex mas protocol
38    ObexMas,
39    /// Obex mns protocol
40    ObexMns,
41    /// Obex pse protocol
42    ObexPse,
43    /// Obex sync protocol
44    ObexSync,
45    /// Avrcp remote protocol
46    AvrcpRemote,
47    /// Network nap protocol for bluetooth networking
48    NetworkingNap,
49    /// An unknown bluetooth uuid
50    Unknown(String),
51}
52
53impl BluetoothUuid {
54    /// Get the uuid as a str reference
55    pub fn as_str(&self) -> &str {
56        match self {
57            BluetoothUuid::SPP => "00001101-0000-1000-8000-00805F9B34FB",
58            BluetoothUuid::A2dpSource => "0000110a-0000-1000-8000-00805f9b34fb",
59            BluetoothUuid::HfpHs => "0000111e-0000-1000-8000-00805f9b34fb",
60            BluetoothUuid::ObexOpp => "00001105-0000-1000-8000-00805f9b34fb",
61            BluetoothUuid::ObexFtp => "00001106-0000-1000-8000-00805f9b34fb",
62            BluetoothUuid::ObexSync => "00001104-0000-1000-8000-00805f9b34fb",
63            BluetoothUuid::A2dpSink => "0000110b-0000-1000-8000-00805f9b34fb",
64            BluetoothUuid::AvrcpRemote => "0000110e-0000-1000-8000-00805f9b34fb",
65            BluetoothUuid::ObexPse => "0000112f-0000-1000-8000-00805f9b34fb",
66            BluetoothUuid::HfpAg => "0000111f-0000-1000-8000-00805f9b34fb",
67            BluetoothUuid::ObexMas => "00001132-0000-1000-8000-00805f9b34fb",
68            BluetoothUuid::ObexMns => "00001133-0000-1000-8000-00805f9b34fb",
69            BluetoothUuid::Base => "00000000-0000-1000-8000-00805f9b34fb",
70            BluetoothUuid::NetworkingNap => "00001116-0000-1000-8000-00805f9b34fb",
71            BluetoothUuid::HspHs => "00001108-0000-1000-8000-00805f9b34fb",
72            BluetoothUuid::HspAg => "00001112-0000-1000-8000-00805f9b34fb",
73            BluetoothUuid::AndroidAuto => "4de17a00-52cb-11e6-bdf4-0800200c9a66",
74            BluetoothUuid::Unknown(s) => s,
75        }
76    }
77}
78
79impl std::str::FromStr for BluetoothUuid {
80    type Err = ();
81    fn from_str(s: &str) -> Result<Self, Self::Err> {
82        Ok(match s {
83            "00001101-0000-1000-8000-00805F9B34FB" => BluetoothUuid::SPP,
84            "0000110a-0000-1000-8000-00805f9b34fb" => BluetoothUuid::A2dpSource,
85            "0000111e-0000-1000-8000-00805f9b34fb" => BluetoothUuid::HfpHs,
86            "00001105-0000-1000-8000-00805f9b34fb" => BluetoothUuid::ObexOpp,
87            "00001106-0000-1000-8000-00805f9b34fb" => BluetoothUuid::ObexFtp,
88            "00001104-0000-1000-8000-00805f9b34fb" => BluetoothUuid::ObexSync,
89            "0000110b-0000-1000-8000-00805f9b34fb" => BluetoothUuid::A2dpSink,
90            "0000110e-0000-1000-8000-00805f9b34fb" => BluetoothUuid::AvrcpRemote,
91            "0000112f-0000-1000-8000-00805f9b34fb" => BluetoothUuid::ObexPse,
92            "0000111f-0000-1000-8000-00805f9b34fb" => BluetoothUuid::HfpAg,
93            "00001132-0000-1000-8000-00805f9b34fb" => BluetoothUuid::ObexMas,
94            "00001133-0000-1000-8000-00805f9b34fb" => BluetoothUuid::ObexMns,
95            "00000000-0000-1000-8000-00805f9b34fb" => BluetoothUuid::Base,
96            "00001116-0000-1000-8000-00805f9b34fb" => BluetoothUuid::NetworkingNap,
97            "00001108-0000-1000-8000-00805f9b34fb" => BluetoothUuid::HspHs,
98            "00001112-0000-1000-8000-00805f9b34fb" => BluetoothUuid::HspAg,
99            "4de17a00-52cb-11e6-bdf4-0800200c9a66" => BluetoothUuid::AndroidAuto,
100            _ => BluetoothUuid::Unknown(s.to_string()),
101        })
102    }
103}
104
105#[cfg(target_os = "android")]
106impl From<ParcelUuid> for BluetoothUuid {
107    fn from(value: ParcelUuid) -> Self {
108        use std::str::FromStr;
109        BluetoothUuid::from_str(&value.to_string().unwrap()).unwrap()
110    }
111}
112
113#[cfg(target_os = "android")]
114pub struct ParcelUuid {
115    internal: jni::objects::GlobalRef,
116    java: Arc<Mutex<Java>>,
117}
118
119#[cfg(target_os = "android")]
120impl std::fmt::Display for ParcelUuid {
121    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122        match self.to_string() {
123            Ok(s) => f.write_str(&s),
124            Err(e) => f.write_str(&format!("ERR: {}", e)),
125        }
126    }
127}
128
129#[cfg(target_os = "android")]
130impl ParcelUuid {
131    pub fn new(uuid: jni::objects::GlobalRef, java: Arc<Mutex<Java>>) -> Self {
132        Self {
133            internal: uuid,
134            java,
135        }
136    }
137
138    pub fn to_string(&self) -> Result<String, std::io::Error> {
139        let mut java = self.java.lock().unwrap();
140        java.use_env(|env, _context| {
141            let dev_name = env
142                .call_method(&self.internal, "toString", "()Ljava/lang/String;", &[])
143                .get_object(env)
144                .map_err(|e| jerr(env, e))?;
145            if dev_name.is_null() {
146                return Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
147            }
148            dev_name.get_string(env).map_err(|e| jerr(env, e))
149        })
150    }
151}