refactor Connect/Disconnect calls

This commit is contained in:
Zakor Gyula 2016-11-29 08:45:46 +01:00
parent 6efea399ed
commit 6e02cb2eb9
24 changed files with 649 additions and 33 deletions

View file

@ -247,6 +247,12 @@ impl BluetoothManager {
BluetoothRequest::Test(data_set_name, sender) => {
let _ = sender.send(self.test(data_set_name));
}
BluetoothRequest::SetRepresentedToNull(service_ids, characteristic_ids, descriptor_ids) => {
self.remove_ids_from_caches(service_ids, characteristic_ids, descriptor_ids)
}
BluetoothRequest::IsRepresentedDeviceNull(id, sender) => {
let _ = sender.send(!self.device_is_cached(&id));
}
BluetoothRequest::Exit => {
break
},
@ -273,6 +279,26 @@ impl BluetoothManager {
}
}
fn remove_ids_from_caches(&mut self,
service_ids: Vec<String>,
characteristic_ids: Vec<String>,
descriptor_ids: Vec<String>) {
for id in service_ids {
self.cached_services.remove(&id);
self.service_to_device.remove(&id);
}
for id in characteristic_ids {
self.cached_characteristics.remove(&id);
self.characteristic_to_service.remove(&id);
}
for id in descriptor_ids {
self.cached_descriptors.remove(&id);
self.descriptor_to_characteristic.remove(&id);
}
}
// Adapter
pub fn get_or_create_adapter(&mut self) -> Option<BluetoothAdapter> {
@ -613,20 +639,19 @@ impl BluetoothManager {
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect
fn gatt_server_disconnect(&mut self, device_id: String) -> BluetoothResult<bool> {
fn gatt_server_disconnect(&mut self, device_id: String) -> BluetoothResult<()> {
let mut adapter = try!(self.get_adapter());
match self.get_device(&mut adapter, &device_id) {
Some(d) => {
// Step 2.
if !d.is_connected().unwrap_or(true) {
return Ok(false);
return Ok(());
}
let _ = d.disconnect();
for _ in 0..MAXIMUM_TRANSACTION_TIME {
match d.is_connected().unwrap_or(true) {
true => thread::sleep(Duration::from_millis(CONNECTION_TIMEOUT_MS)),
false => return Ok(false),
false => return Ok(()),
}
}
return Err(BluetoothError::Network);

View file

@ -222,21 +222,23 @@ fn create_heart_rate_service(device: &BluetoothDevice,
try!(create_characteristic_with_value(&heart_rate_service,
HEART_RATE_MEASUREMENT_CHARACTERISTIC_UUID.to_owned(),
vec![0]));
try!(heart_rate_measurement_characteristic.set_flags(vec![NOTIFY_FLAG.to_string()]));
try!(heart_rate_measurement_characteristic.set_flags(vec![NOTIFY_FLAG.to_string(),
READ_FLAG.to_string(),
WRITE_FLAG.to_string()]));
// Body Sensor Location Characteristic 1
let body_sensor_location_characteristic_1 =
try!(create_characteristic_with_value(&heart_rate_service,
BODY_SENSOR_LOCATION_CHARACTERISTIC_UUID.to_owned(),
vec![49]));
try!(body_sensor_location_characteristic_1.set_flags(vec![READ_FLAG.to_string()]));
try!(body_sensor_location_characteristic_1.set_flags(vec![READ_FLAG.to_string(), WRITE_FLAG.to_string()]));
// Body Sensor Location Characteristic 2
let body_sensor_location_characteristic_2 =
try!(create_characteristic_with_value(&heart_rate_service,
BODY_SENSOR_LOCATION_CHARACTERISTIC_UUID.to_owned(),
vec![50]));
try!(body_sensor_location_characteristic_2.set_flags(vec![READ_FLAG.to_string()]));
try!(body_sensor_location_characteristic_2.set_flags(vec![READ_FLAG.to_string(), WRITE_FLAG.to_string()]));
Ok(heart_rate_service)
}