Struct bluer::rfcomm::SocketAddr
source · pub struct SocketAddr {
pub addr: Address,
pub channel: u8,
}
Available on crate feature
rfcomm
only.Expand description
An RFCOMM socket address.
§String representation
The string representation is of the form
[01:23:45:67:89:0a]:75
where 01:23:45:67:89:0a
is the Bluetooth address
and 75
is the channel number.
Fields§
§addr: Address
Device address.
When listening or binding, specify Address::any for any local adapter address.
channel: u8
Channel number.
Implementations§
source§impl SocketAddr
impl SocketAddr
sourcepub const fn new(addr: Address, channel: u8) -> Self
pub const fn new(addr: Address, channel: u8) -> Self
Creates a new RFCOMM socket address.
Examples found in repository?
examples/rfcomm_server.rs (line 17)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
async fn main() -> bluer::Result<()> {
env_logger::init();
let session = bluer::Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;
adapter.set_discoverable(true).await?;
let adapter_addr = adapter.address().await?;
let local_sa = SocketAddr::new(adapter_addr, CHANNEL);
let listener = Listener::bind(local_sa).await?;
println!(
"Listening on {} channel {}. Press enter to quit.",
listener.as_ref().local_addr()?.addr,
listener.as_ref().local_addr()?.channel
);
let stdin = BufReader::new(tokio::io::stdin());
let mut lines = stdin.lines();
loop {
println!("\nWaiting for connection...");
let (mut stream, sa) = tokio::select! {
l = listener.accept() => {
match l {
Ok(v) => v,
Err(err) => {
println!("Accepting connection failed: {}", &err);
continue;
}}
},
_ = lines.next_line() => break,
};
println!("Accepted connection from {:?}", &sa);
println!("Sending hello");
if let Err(err) = stream.write_all(HELLO_MSG).await {
println!("Write failed: {}", &err);
continue;
}
loop {
let buf_size = 1024;
let mut buf = vec![0; buf_size as _];
let n = match stream.read(&mut buf).await {
Ok(0) => {
println!("Stream ended");
break;
}
Ok(n) => n,
Err(err) => {
println!("Read failed: {}", &err);
break;
}
};
let buf = &buf[..n];
println!("Echoing {} bytes", buf.len());
if let Err(err) = stream.write_all(buf).await {
println!("Write failed: {}", &err);
continue;
}
}
}
Ok(())
}
More examples
examples/rfcomm_client.rs (line 27)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
async fn main() -> bluer::Result<()> {
env_logger::init();
let session = bluer::Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;
let args: Vec<_> = env::args().collect();
if args.len() != 2 {
eprintln!("Specify target Bluetooth address as argument");
exit(1);
}
let target_addr: Address = args[1].parse().expect("invalid address");
let target_sa = SocketAddr::new(target_addr, CHANNEL);
println!("Connecting to {:?}", &target_sa);
let mut stream = Stream::connect(target_sa).await.expect("connection failed");
println!("Local address: {:?}", stream.as_ref().local_addr()?);
println!("Remote address: {:?}", stream.peer_addr()?);
println!("Security: {:?}", stream.as_ref().security()?);
println!("\nReceiving hello");
let mut hello_buf = [0u8; HELLO_MSG.len()];
stream.read_exact(&mut hello_buf).await.expect("read failed");
println!("Received: {}", String::from_utf8_lossy(&hello_buf));
if hello_buf != HELLO_MSG {
panic!("Wrong hello message");
}
let (mut rh, mut wh) = stream.into_split();
let mut rng = rand::thread_rng();
for i in 0..15 {
let len = rng.gen_range(0..50000);
let data: Vec<u8> = (0..len).map(|_| rng.gen()).collect();
println!("\nTest iteration {i} with data size {len}");
// We must read back the data while sending, otherwise the connection
// buffer will overrun and we will lose data.
let read_task = tokio::spawn(async move {
let mut echo_buf = vec![0u8; len];
let res = match rh.read_exact(&mut echo_buf).await {
Ok(_) => Ok(echo_buf),
Err(err) => Err(err),
};
(rh, res)
});
// Note that write_all will automatically split the buffer into
// multiple writes of MTU size.
wh.write_all(&data).await.expect("write failed");
println!("Waiting for echo");
let (rh_back, res) = read_task.await.unwrap();
rh = rh_back;
let echo_buf = res.expect("read failed");
if echo_buf != data {
panic!("Echoed data does not match sent data");
}
println!("Data matches");
}
println!("Done");
Ok(())
}
sourcepub const fn any() -> Self
pub const fn any() -> Self
When specified to Socket::bind binds to any local adapter address and a dynamically allocated channel.
Trait Implementations§
source§impl Clone for SocketAddr
impl Clone for SocketAddr
source§fn clone(&self) -> SocketAddr
fn clone(&self) -> SocketAddr
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for SocketAddr
impl Debug for SocketAddr
source§impl Default for SocketAddr
impl Default for SocketAddr
source§fn default() -> SocketAddr
fn default() -> SocketAddr
Returns the “default value” for a type. Read more
source§impl<'de> Deserialize<'de> for SocketAddr
impl<'de> Deserialize<'de> for SocketAddr
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl Display for SocketAddr
impl Display for SocketAddr
source§impl FromStr for SocketAddr
impl FromStr for SocketAddr
§type Err = InvalidSocketAddr
type Err = InvalidSocketAddr
The associated error which can be returned from parsing.
source§impl Hash for SocketAddr
impl Hash for SocketAddr
source§impl Ord for SocketAddr
impl Ord for SocketAddr
source§fn cmp(&self, other: &SocketAddr) -> Ordering
fn cmp(&self, other: &SocketAddr) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl PartialEq for SocketAddr
impl PartialEq for SocketAddr
source§fn eq(&self, other: &SocketAddr) -> bool
fn eq(&self, other: &SocketAddr) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for SocketAddr
impl PartialOrd for SocketAddr
source§fn partial_cmp(&self, other: &SocketAddr) -> Option<Ordering>
fn partial_cmp(&self, other: &SocketAddr) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for SocketAddr
impl Serialize for SocketAddr
impl Copy for SocketAddr
impl Eq for SocketAddr
impl StructuralPartialEq for SocketAddr
Auto Trait Implementations§
impl Freeze for SocketAddr
impl RefUnwindSafe for SocketAddr
impl Send for SocketAddr
impl Sync for SocketAddr
impl Unpin for SocketAddr
impl UnwindSafe for SocketAddr
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more