use btleplug::api::BDAddr;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BluetoothAdapter {
pub id: String,
pub name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BluetoothDevice {
pub address: String,
pub name: Option<String>,
pub rssi: Option<i16>,
pub services: Vec<Uuid>,
pub kind: BluetoothDeviceKind,
}
impl BluetoothDevice {
pub fn from_addr(addr: BDAddr) -> Self {
Self {
address: addr.to_string(),
name: None,
rssi: None,
services: Vec::new(),
kind: BluetoothDeviceKind::BlePeripheral,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BluetoothDeviceKind {
BlePeripheral,
Classic,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn adapter_serde_roundtrip() {
let adapter = BluetoothAdapter {
id: "hci0".to_string(),
name: "Intel Bluetooth".to_string(),
};
let json = serde_json::to_string(&adapter).unwrap();
let back: BluetoothAdapter = serde_json::from_str(&json).unwrap();
assert_eq!(back.id, "hci0");
assert_eq!(back.name, "Intel Bluetooth");
}
#[test]
fn device_kind_serde_roundtrip() {
for kind in [
BluetoothDeviceKind::BlePeripheral,
BluetoothDeviceKind::Classic,
] {
let json = serde_json::to_string(&kind).unwrap();
let back: BluetoothDeviceKind = serde_json::from_str(&json).unwrap();
assert_eq!(back, kind);
}
}
#[test]
fn device_serde_roundtrip() {
let device = BluetoothDevice {
address: "AA:BB:CC:DD:EE:FF".to_string(),
name: Some("My Headphones".to_string()),
rssi: Some(-70),
services: vec![],
kind: BluetoothDeviceKind::BlePeripheral,
};
let json = serde_json::to_string(&device).unwrap();
let back: BluetoothDevice = serde_json::from_str(&json).unwrap();
assert_eq!(back.address, "AA:BB:CC:DD:EE:FF");
assert_eq!(back.name.as_deref(), Some("My Headphones"));
assert_eq!(back.rssi, Some(-70));
assert_eq!(back.kind, BluetoothDeviceKind::BlePeripheral);
}
#[test]
fn device_optional_fields_when_none() {
let device = BluetoothDevice {
address: "11:22:33:44:55:66".to_string(),
name: None,
rssi: None,
services: vec![],
kind: BluetoothDeviceKind::Classic,
};
let json = serde_json::to_string(&device).unwrap();
let back: BluetoothDevice = serde_json::from_str(&json).unwrap();
assert!(back.name.is_none());
assert!(back.rssi.is_none());
}
}