use crate::DriverCallback;
use crate::DriverContext;
use crate::{
DriverCategory, DriverError, DriverResult,
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::process::Command;
use tracing::{debug, info, warn};
#[derive(Debug)]
pub struct BluetoothLeAdvertiseStartDriver;
#[async_trait::async_trait]
impl Driver for BluetoothLeAdvertiseStartDriver {
fn name(&self) -> &str {
"bluetooth_le_advertise_start"
}
fn description(&self) -> &str {
"Start BLE advertising to make the device discoverable to BLE scanners"
}
fn usage_hint(&self) -> &str {
"Use this skill to broadcast your device's presence to BLE devices. Useful for IoT and sensor applications."
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![
DriverParameter {
name: "service_uuid".to_string(),
param_type: "string".to_string(),
description: "UUID of the service to advertise (optional)".to_string(),
required: false,
default: None,
example: Some(Value::String("0000180f-0000-1000-8000-00805f9b34fb".to_string())),
enum_values: None,
},
DriverParameter {
name: "manufacturer_data".to_string(),
param_type: "string".to_string(),
description: "Manufacturer specific data (hex format)".to_string(),
required: false,
default: None,
example: Some(Value::String("0x01020304".to_string())),
enum_values: None,
},
];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "bluetooth_le_advertise_start",
"parameters": {
"service_uuid": "0000180f-0000-1000-8000-00805f9b34fb"
}
}));
}
fn example_output(&self) -> String {
"BLE advertising started".to_string()
}
fn category(&self) -> DriverCategory {
DriverCategory::Bluetooth
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
callback: Option<&dyn DriverCallback>,
context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing bluetooth_le_advertise_start driver");
let service_uuid = parameters.get("service_uuid").and_then(|v| v.as_str());
debug!("Starting BLE advertising, service_uuid: {:?}", service_uuid);
#[cfg(target_os = "linux")]
{
debug!("Executing bluetoothctl advertise on");
let output = crate::common::hidden_cmd("bluetoothctl")
.args(["advertise", "on"])
.output()
.map_err(|e| DriverError::execution(format!("Failed to execute bluetoothctl: {}", e)))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
warn!("bluetoothctl advertise on failed: {}", stderr);
return Err(DriverError::execution(format!("Failed to start advertising: {}", stderr)));
}
if let Some(uuid) = service_uuid {
debug!("Setting advertise service: {}", uuid);
let _ = crate::common::hidden_cmd("bluetoothctl").args(["advertise", "service", uuid]).output().ok();
}
}
info!("BLE advertising started");
return Ok("BLE advertising started".to_string());
}
}