Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aug 5870 #30

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blurz"
version = "0.3.0"
version = "0.4.0"
description = "Bluetooth lib for Rust using blueZ/dbus"
readme = "README.md"
authors = ["Attila Dusnoki <[email protected]>"]
Expand All @@ -13,5 +13,5 @@ name = "blurz"
path = "src/lib.rs"

[dependencies]
dbus = "0.6"
hex = "0.3"
dbus = "= 0.6.4"
hex = "= 0.3.2"
10 changes: 6 additions & 4 deletions examples/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ use std::error::Error;

use blurz::bluetooth_adapter::BluetoothAdapter as Adapter;
use blurz::bluetooth_device::BluetoothDevice as Device;
use blurz::bluetooth_session::BluetoothSession as Session;

fn test() -> Result<(), Box<Error>> {
let adapter: Adapter = try!(Adapter::init());
let session = &Session::create_session(None).unwrap();
let adapter: Adapter = try!(Adapter::init(session));
let device: Device = try!(adapter.get_first_device());
println!("{:?}", device);
Ok(())
}

fn main() {
match test() {
Ok(_) => (),
Err(e) => println!("{:?}", e),
}
Ok(_) => (),
Err(e) => println!("{:?}", e),
}
}
37 changes: 20 additions & 17 deletions examples/test2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ static BATTERY_SERVICE_UUID: &'static str = "0000180f-0000-1000-8000-00805f9b34f
static COLOR_PICKER_SERVICE_UUID: &'static str = "00001812-0000-1000-8000-00805f9b34fb";

use std::error::Error;
use std::time::Duration;
use std::thread;
use std::time::Duration;

use blurz::bluetooth_adapter::BluetoothAdapter as Adapter;
use blurz::bluetooth_device::BluetoothDevice as Device;
use blurz::bluetooth_gatt_service::BluetoothGATTService as Service;
use blurz::bluetooth_discovery_session::BluetoothDiscoverySession as DiscoverySession;
use blurz::bluetooth_gatt_characteristic::BluetoothGATTCharacteristic as Characteristic;
use blurz::bluetooth_gatt_descriptor::BluetoothGATTDescriptor as Descriptor;
use blurz::bluetooth_discovery_session::BluetoothDiscoverySession as DiscoverySession;
use blurz::bluetooth_gatt_service::BluetoothGATTService as Service;
use blurz::bluetooth_session::BluetoothSession as Session;

fn test2() -> Result<(), Box<Error>> {
let adapter: Adapter = try!(Adapter::init());
let session = try!(DiscoverySession::create_session(adapter.get_id()));
let bt_session = &Session::create_session(None)?;
let adapter: Adapter = try!(Adapter::init(bt_session));
let session = try!(DiscoverySession::create_session(
&bt_session,
adapter.get_id()
));
try!(session.start_discovery());
//let mut devices = vec!();
for _ in 0..5 {
Expand All @@ -32,18 +37,17 @@ fn test2() -> Result<(), Box<Error>> {
return Err(Box::from("No device found"));
}
println!("{} device(s) found", devices.len());
let mut device: Device = Device::new("".to_string());
let mut device: Device = Device::new(bt_session, "".to_string());
'device_loop: for d in devices {
device = Device::new(d.clone());
device = Device::new(bt_session, d.clone());
println!("{} {:?}", device.get_id(), device.get_alias());
let uuids = try!(device.get_uuids());
println!("{:?}", uuids);
'uuid_loop: for uuid in uuids {
if uuid == COLOR_PICKER_SERVICE_UUID ||
uuid == BATTERY_SERVICE_UUID {
if uuid == COLOR_PICKER_SERVICE_UUID || uuid == BATTERY_SERVICE_UUID {
println!("{:?} has a service!", device.get_alias());
println!("connect device...");
device.connect().ok();
device.connect(10000).ok();
if try!(device.is_connected()) {
println!("checking gatt...");
// We need to wait a bit after calling connect to safely
Expand All @@ -66,20 +70,19 @@ fn test2() -> Result<(), Box<Error>> {
}
let services = try!(device.get_gatt_services());
for service in services {
let s = Service::new(service.clone());
let s = Service::new(bt_session, service.clone());
println!("{:?}", s);
let characteristics = try!(s.get_gatt_characteristics());
for characteristic in characteristics {
let c = Characteristic::new(characteristic.clone());
let c = Characteristic::new(bt_session, characteristic.clone());
println!("{:?}", c);
println!("Value: {:?}", c.read_value(None));
let descriptors = try!(c.get_gatt_descriptors());
for descriptor in descriptors {
let d = Descriptor::new(descriptor.clone());
let d = Descriptor::new(bt_session, descriptor.clone());
println!("{:?}", d);
println!("Value: {:?}", d.read_value(None));
}

}
}
try!(device.disconnect());
Expand All @@ -88,7 +91,7 @@ fn test2() -> Result<(), Box<Error>> {

fn main() {
match test2() {
Ok(_) => (),
Err(e) => println!("{:?}", e),
}
Ok(_) => (),
Err(e) => println!("{:?}", e),
}
}
26 changes: 18 additions & 8 deletions examples/test3.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
extern crate blurz;

use std::error::Error;
use std::time::Duration;
use std::thread;
use std::time::Duration;

use blurz::bluetooth_adapter::BluetoothAdapter as Adapter;
use blurz::bluetooth_device::BluetoothDevice as Device;
use blurz::bluetooth_discovery_session::BluetoothDiscoverySession as DiscoverySession;
use blurz::bluetooth_session::BluetoothSession as Session;

fn test3() -> Result<(), Box<Error>> {
let adapter: Adapter = try!(Adapter::init());
let bt_session = &Session::create_session(None)?;
let adapter: Adapter = try!(Adapter::init(bt_session));
try!(adapter.set_powered(true));
loop {
let session = try!(DiscoverySession::create_session(adapter.get_id()));
let session = try!(DiscoverySession::create_session(
&bt_session,
adapter.get_id()
));
thread::sleep(Duration::from_millis(200));
try!(session.start_discovery());
thread::sleep(Duration::from_millis(800));
let devices = try!(adapter.get_device_list());

println!("{} device(s) found", devices.len());
'device_loop: for d in devices {
let device = Device::new(d.clone());
println!("{} {:?} {:?}", device.get_id(), device.get_address(),device.get_rssi());
let device = Device::new(bt_session, d.clone());
println!(
"{} {:?} {:?}",
device.get_id(),
device.get_address(),
device.get_rssi()
);
try!(adapter.remove_device(device.get_id()));
}
try!(session.stop_discovery());
Expand All @@ -30,7 +40,7 @@ fn test3() -> Result<(), Box<Error>> {

fn main() {
match test3() {
Ok(_) => (),
Err(e) => println!("{:?}", e),
}
Ok(_) => (),
Err(e) => println!("{:?}", e),
}
}
12 changes: 6 additions & 6 deletions examples/test4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ use std::path::Path;

use blurz::bluetooth_adapter::BluetoothAdapter as Adapter;
use blurz::bluetooth_device::BluetoothDevice as Device;
use blurz::bluetooth_obex::open_bus_connection;
use blurz::bluetooth_obex::{
BluetoothOBEXSession as OBEXSession, BluetoothOBEXTransfer as OBEXTransfer,
};
use blurz::bluetooth_session::BluetoothSession as Session;

fn test_obex_file_transfer() -> Result<(), Box<Error>> {
let adapter: Adapter = Adapter::init()?;
let session = &Session::create_session(None)?;
let adapter: Adapter = Adapter::init(session)?;
let devices: Vec<String> = adapter.get_device_list()?;

let filtered_devices = devices
.iter()
.filter(|&device_id| {
let device = Device::new(device_id.to_string());
let device = Device::new(session, device_id.to_string());
device.is_ready_to_receive().unwrap()
}).cloned()
.collect::<Vec<String>>();

let device_id: &str = &filtered_devices[0];
let device = Device::new(device_id.to_string());
let device = Device::new(session, device_id.to_string());

let connection = open_bus_connection()?;
let session = OBEXSession::new(connection, &device)?;
let session = OBEXSession::new(session, &device)?;

let mut empty_file = File::create("./test.png")?;
empty_file.write_all(b"1111")?;
Expand Down
22 changes: 22 additions & 0 deletions examples/test5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extern crate blurz;

use std::error::Error;

use blurz::bluetooth_event::BluetoothEvent;
use blurz::bluetooth_session::BluetoothSession as Session;

fn test5() -> Result<(), Box<Error>> {
let session = &Session::create_session(Some("/org/bluez/hci0")).unwrap();
loop {
for event in session.incoming(1000).map(BluetoothEvent::from) {
println!("{:?}", event);
}
}
}

fn main() {
match test5() {
Ok(_) => (),
Err(e) => println!("{:?}", e),
}
}
Loading