initial
7
src-tauri/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Generated by Tauri
|
||||
# will have schema files for capabilities auto-completion
|
||||
/gen/schemas
|
||||
4098
src-tauri/Cargo.lock
generated
Normal file
25
src-tauri/Cargo.toml
Normal file
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "heartbeat-cat"
|
||||
version = "0.0.0"
|
||||
description = "A Tauri App"
|
||||
authors = ["you"]
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "1", features = [] }
|
||||
|
||||
[dependencies]
|
||||
tauri = { version = "1", features = [ "window-show", "window-hide", "window-maximize", "window-unmaximize", "window-unminimize", "window-start-dragging", "window-minimize", "window-close", "shell-open"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
btleplug = { version = "0.11.5", features = ["serde"] }
|
||||
tokio = { version = "1.38.0", features = ["rt", "rt-multi-thread", "macros"] }
|
||||
futures = "0.3.30"
|
||||
uuid = "1.10.0"
|
||||
lazy_static = "1.5.0"
|
||||
|
||||
[features]
|
||||
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
|
||||
custom-protocol = ["tauri/custom-protocol"]
|
||||
3
src-tauri/build.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
tauri_build::build()
|
||||
}
|
||||
BIN
src-tauri/icons/128x128.png
Normal file
|
After Width: | Height: | Size: 7.7 KiB |
BIN
src-tauri/icons/128x128@2x.png
Normal file
|
After Width: | Height: | Size: 27 KiB |
BIN
src-tauri/icons/32x32.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
src-tauri/icons/Square107x107Logo.png
Normal file
|
After Width: | Height: | Size: 9.1 KiB |
BIN
src-tauri/icons/Square142x142Logo.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
src-tauri/icons/Square150x150Logo.png
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
src-tauri/icons/Square284x284Logo.png
Normal file
|
After Width: | Height: | Size: 40 KiB |
BIN
src-tauri/icons/Square30x30Logo.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
src-tauri/icons/Square310x310Logo.png
Normal file
|
After Width: | Height: | Size: 45 KiB |
BIN
src-tauri/icons/Square44x44Logo.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
src-tauri/icons/Square71x71Logo.png
Normal file
|
After Width: | Height: | Size: 5.4 KiB |
BIN
src-tauri/icons/Square89x89Logo.png
Normal file
|
After Width: | Height: | Size: 7.1 KiB |
BIN
src-tauri/icons/StoreLogo.png
Normal file
|
After Width: | Height: | Size: 3.4 KiB |
BIN
src-tauri/icons/icon.icns
Normal file
BIN
src-tauri/icons/icon.ico
Normal file
|
After Width: | Height: | Size: 40 KiB |
BIN
src-tauri/icons/icon.png
Normal file
|
After Width: | Height: | Size: 80 KiB |
132
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use tauri::{AppHandle, Manager, State};
|
||||
use tokio;
|
||||
use tokio::sync::{Mutex};
|
||||
use btleplug::api::{Central, ScanFilter, Manager as _, Peripheral as _};
|
||||
use btleplug::api::bleuuid::uuid_from_u16;
|
||||
use btleplug::platform::{Adapter, Manager as BtleManager, Peripheral};
|
||||
use futures::StreamExt;
|
||||
|
||||
#[derive(serde::Serialize, Clone)]
|
||||
struct BleDevice {
|
||||
name: String,
|
||||
address: String,
|
||||
}
|
||||
|
||||
struct BleConnection {
|
||||
manager: Mutex<Option<BtleManager>>,
|
||||
adapter: Mutex<Option<Adapter>>,
|
||||
peripheral: Mutex<Option<Peripheral>>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn scan_devices(
|
||||
connection: State<'_, BleConnection>
|
||||
) -> Result<String, String> {
|
||||
let adapter = connection.adapter.lock().await;
|
||||
let adapter = adapter.as_ref().unwrap();
|
||||
adapter.start_scan(ScanFilter::default()).await.unwrap();
|
||||
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
||||
|
||||
let mut devices = vec![];
|
||||
for p in adapter.peripherals().await.unwrap() {
|
||||
devices.push(BleDevice {
|
||||
name: p.properties().await.unwrap().unwrap().local_name.unwrap_or("Unknown".to_string()),
|
||||
address: p.address().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(serde_json::to_string(&devices).unwrap())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn connect(
|
||||
address: String,
|
||||
connection: State<'_, BleConnection>,
|
||||
app_handle: AppHandle,
|
||||
) -> Result<bool, String> {
|
||||
let adapter = connection.adapter.lock().await;
|
||||
let adapter = adapter.as_ref().unwrap();
|
||||
// adapter.start_scan(ScanFilter::default()).await.unwrap();
|
||||
// tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
||||
|
||||
let peripheral = adapter.peripherals().await.unwrap().into_iter().find(|p| p.address().to_string() == address).unwrap().clone();
|
||||
peripheral.connect().await.unwrap();
|
||||
|
||||
*connection.peripheral.lock().await = Some(peripheral.clone());
|
||||
|
||||
peripheral.discover_services().await.unwrap_or_else(|_| {
|
||||
println!("Failed to discover services");
|
||||
});
|
||||
let service = peripheral.services()
|
||||
.into_iter()
|
||||
.find(|s| s.uuid == uuid_from_u16(0x180D))
|
||||
.unwrap();
|
||||
let characteristic = service.characteristics
|
||||
.into_iter()
|
||||
.find(|c| c.uuid == uuid_from_u16(0x2A37))
|
||||
.unwrap();
|
||||
|
||||
peripheral.subscribe(&characteristic).await.unwrap_or_else(|_| {
|
||||
println!("Failed to subscribe to characteristic");
|
||||
});
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut notification_stream = peripheral.notifications().await.unwrap();
|
||||
while let Some(notification) = notification_stream.next().await {
|
||||
if notification.uuid == uuid_from_u16(0x2A37) {
|
||||
app_handle.emit_all("heart-rate", notification.value[1]).unwrap();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn disconnect(
|
||||
connection: State<'_, BleConnection>
|
||||
) -> Result<bool, String> {
|
||||
let connection = connection.peripheral.lock().await;
|
||||
let connection = connection.as_ref().unwrap();
|
||||
|
||||
connection.disconnect().await.unwrap();
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn get_connected_device(
|
||||
connection: State<'_, BleConnection>
|
||||
) -> Result<String, String> {
|
||||
let connection = connection.peripheral.lock().await;
|
||||
let connection = connection.as_ref().unwrap();
|
||||
|
||||
Ok(serde_json::to_string(&BleDevice {
|
||||
name: connection.properties().await.unwrap().unwrap().local_name.unwrap_or("Unknown".to_string()),
|
||||
address: connection.address().to_string(),
|
||||
}).unwrap())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let ble_manager = BtleManager::new().await.unwrap();
|
||||
let adapter = ble_manager.adapters().await.unwrap().into_iter().next().unwrap();
|
||||
|
||||
tauri::Builder::default()
|
||||
.manage(BleConnection {
|
||||
manager: Mutex::new(Some(ble_manager)),
|
||||
adapter: Mutex::new(Some(adapter)),
|
||||
peripheral: Default::default(),
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
scan_devices,
|
||||
connect,
|
||||
disconnect,
|
||||
get_connected_device
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
52
src-tauri/tauri.conf.json
Normal file
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"build": {
|
||||
"beforeDevCommand": "pnpm dev",
|
||||
"beforeBuildCommand": "pnpm build",
|
||||
"devPath": "http://localhost:1420",
|
||||
"distDir": "../dist"
|
||||
},
|
||||
"package": {
|
||||
"productName": "heartbeat-cat",
|
||||
"version": "0.0.1"
|
||||
},
|
||||
"tauri": {
|
||||
"allowlist": {
|
||||
"all": false,
|
||||
"shell": {
|
||||
"all": false,
|
||||
"open": true
|
||||
},
|
||||
"window": {
|
||||
"all": false,
|
||||
"close": true,
|
||||
"hide": true,
|
||||
"show": true,
|
||||
"maximize": true,
|
||||
"minimize": true,
|
||||
"unmaximize": true,
|
||||
"unminimize": true,
|
||||
"startDragging": true
|
||||
}
|
||||
},
|
||||
"windows": [
|
||||
{
|
||||
"title": "Heartbeat Cat",
|
||||
"minWidth": 800,
|
||||
"minHeight": 600,
|
||||
"width": 800,
|
||||
"height": 600
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": null
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"targets": "all",
|
||||
"identifier": "ga.bh8.heartbeat-cat",
|
||||
"icon": [
|
||||
"icons/icon.ico"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||