|
| 1 | +use super::peripheral::Peripheral; |
| 2 | +use crate::api::{BDAddr, Central, CentralEvent, Peripheral as _, PeripheralId}; |
| 3 | +use crate::{Error, Result}; |
| 4 | + |
| 5 | +use crate::common::adapter_manager::AdapterManager; |
| 6 | +use async_trait::async_trait; |
| 7 | +use std::pin::Pin; |
| 8 | + |
| 9 | +use futures::channel::oneshot; |
| 10 | +use futures::stream::Stream; |
| 11 | +use wasm_bindgen::prelude::*; |
| 12 | + |
| 13 | +use js_sys::Array; |
| 14 | +use wasm_bindgen_futures::{spawn_local, JsFuture}; |
| 15 | +use web_sys::{BluetoothDevice, RequestDeviceOptions}; |
| 16 | + |
| 17 | +/// Implementation of [api::Central](crate::api::Central). |
| 18 | +#[derive(Clone, Debug)] |
| 19 | +pub struct Adapter { |
| 20 | + manager: AdapterManager<Peripheral>, |
| 21 | +} |
| 22 | + |
| 23 | +fn bluetooth() -> Option<web_sys::Bluetooth> { |
| 24 | + web_sys::window().unwrap().navigator().bluetooth() |
| 25 | +} |
| 26 | + |
| 27 | +#[macro_export] |
| 28 | +macro_rules! spawn_local_and_wait { |
| 29 | + ($x:expr) => { |
| 30 | + let (sender, receiver) = oneshot::channel(); |
| 31 | + spawn_local(async move { |
| 32 | + let _ = sender.send($x); |
| 33 | + }); |
| 34 | + receiver.await.unwrap() |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +impl Adapter { |
| 39 | + pub(crate) fn new() -> Option<Self> { |
| 40 | + if let Some(_) = bluetooth() { |
| 41 | + let manager = AdapterManager::default(); |
| 42 | + Some(Self { manager }) |
| 43 | + } else { |
| 44 | + None |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pub(crate) async fn add_inital_peripherals(&self) { |
| 49 | + let manager = self.manager.clone(); |
| 50 | + spawn_local_and_wait!({ |
| 51 | + if let Ok(devices) = JsFuture::from(bluetooth().unwrap().get_devices()).await { |
| 52 | + let devices = Array::from(&devices); |
| 53 | + for device in devices.iter() { |
| 54 | + let p = Peripheral::new(BluetoothDevice::from(device), manager.clone()); |
| 55 | + if !manager.has_peripheral(&p.id()) { |
| 56 | + manager.add_peripheral(p.id(), p); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[async_trait] |
| 65 | +impl Central for Adapter { |
| 66 | + type Peripheral = Peripheral; |
| 67 | + |
| 68 | + async fn events(&self) -> Result<Pin<Box<dyn Stream<Item = CentralEvent> + Send>>> { |
| 69 | + Ok(self.manager.event_stream()) |
| 70 | + } |
| 71 | + |
| 72 | + async fn start_scan(&self) -> Result<()> { |
| 73 | + let manager = self.manager.clone(); |
| 74 | + |
| 75 | + spawn_local_and_wait!({ |
| 76 | + let mut options = RequestDeviceOptions::new(); |
| 77 | + // let mut filters = Array::new(); |
| 78 | + let services = Array::new(); |
| 79 | + // let mut optional_services = Array::new(); |
| 80 | + // for (protocol_name, configs) in config_manager.config.protocols { |
| 81 | + // if let Some(btle) = configs.btle { |
| 82 | + // for name in btle.names { |
| 83 | + // let mut filter = web_sys::BluetoothLeScanFilterInit::new(); |
| 84 | + // if name.contains("*") { |
| 85 | + // let mut name_clone = name.clone(); |
| 86 | + // name_clone.pop(); |
| 87 | + // filter.name_prefix(&name_clone); |
| 88 | + // } else { |
| 89 | + // filter.name(&name); |
| 90 | + // } |
| 91 | + // filters.push(&filter.into()); |
| 92 | + // } |
| 93 | + // for (service, _) in btle.services { |
| 94 | + // optional_services.push(&service.to_string().into()); |
| 95 | + // } |
| 96 | + // } |
| 97 | + // } |
| 98 | + |
| 99 | + for uuid in Peripheral::known_services().iter() { |
| 100 | + services.push(&uuid.to_string().into()); |
| 101 | + } |
| 102 | + |
| 103 | + // services.push(&"00001801-0000-1000-8000-00805f9b34fb".to_string().into()); |
| 104 | + // services.push(&"000000ff-0000-1000-8000-00805f9b34fb".to_string().into()); |
| 105 | + // services.push(&"000000ee-0000-1000-8000-00805f9b34fb".to_string().into()); |
| 106 | + // services.push(&"955a180a-0fe2-f5aa-a094-84b8d4f3e8ad".to_string().into()); |
| 107 | + // services.push(&"955a180b-0fe2-f5aa-a094-84b8d4f3e8ad".to_string().into()); |
| 108 | + // services.push(&"f55a180b-0fe2-f5aa-a094-84b8d4f3e8ad".to_string().into()); |
| 109 | + // services.push(&"0000180d-0000-1000-8000-00805f9b34fb".to_string().into()); |
| 110 | + |
| 111 | + // let mut filter = web_sys::BluetoothLeScanFilterInit::new(); |
| 112 | + // filter.services(&services.into()); |
| 113 | + // filters.push(&filter.into()); |
| 114 | + // options.filters(&filters.into()); |
| 115 | + options.optional_services(&services.into()); |
| 116 | + options.accept_all_devices(true); |
| 117 | + |
| 118 | + web_sys::console::dir_1(&options); |
| 119 | + |
| 120 | + match JsFuture::from(bluetooth().unwrap().request_device(&options)).await { |
| 121 | + Ok(device) => { |
| 122 | + println!("GOT DEVICE"); |
| 123 | + let p = Peripheral::new(BluetoothDevice::from(device), manager.clone()); |
| 124 | + let id = p.id(); |
| 125 | + manager.add_peripheral(p.id(), p); |
| 126 | + manager.emit(CentralEvent::DeviceDiscovered(id)); |
| 127 | + } |
| 128 | + Err(e) => web_sys::console::error_1(&JsValue::from(e)), |
| 129 | + } |
| 130 | + }); |
| 131 | + Ok(()) |
| 132 | + } |
| 133 | + |
| 134 | + async fn stop_scan(&self) -> Result<()> { |
| 135 | + Ok(()) |
| 136 | + } |
| 137 | + |
| 138 | + async fn peripherals(&self) -> Result<Vec<Peripheral>> { |
| 139 | + Ok(self.manager.peripherals()) |
| 140 | + } |
| 141 | + |
| 142 | + async fn peripheral(&self, address: PeripheralId) -> Result<Peripheral> { |
| 143 | + self.manager |
| 144 | + .peripheral(address) |
| 145 | + .ok_or(Error::DeviceNotFound) |
| 146 | + } |
| 147 | + |
| 148 | + async fn add_peripheral(&self, _address: BDAddr) -> Result<Peripheral> { |
| 149 | + Err(Error::NotSupported( |
| 150 | + "Can't add a Peripheral from a BDAddr".to_string(), |
| 151 | + )) |
| 152 | + } |
| 153 | +} |
0 commit comments