Bluetooth (Linux)
The bluetooth feature connects over RFCOMM through BlueZ, the Linux
Bluetooth stack, via bluer. On other operating
systems, use a serial port instead.
[dependencies]
nxt-rs = { git = "https://forge.wvffle.net/wvffle/nxt-rs.git", features = ["bluetooth"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
Pairing
Pair the brick once with bluetoothctl. On the brick, make sure Bluetooth is
switched on and the brick is visible (Bluetooth → Visibility → Visible).
$ bluetoothctl
[bluetooth]# scan on
[NEW] Device 00:16:53:0A:0B:0C NXT
[bluetooth]# pair 00:16:53:0A:0B:0C
The brick beeps and shows a passkey prompt. The default passkey is 1234.
Confirm it on the brick and enter the same passkey on the computer when
asked. Finally:
[bluetooth]# trust 00:16:53:0A:0B:0C
Connecting
use nxt_rs::{Brick, transport::Bluetooth};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
for nxt in Bluetooth::paired().await? {
println!("{} at {}", nxt.name, nxt.address);
}
let brick = Brick::bluetooth("00:16:53:0A:0B:0C".parse()?).await?;
println!("battery: {}", brick.battery().await?);
Ok(())
}
Bluetooth::paired() lists the paired devices whose Bluetooth class is that
of an NXT.
Things to know
- tokio is required.
bluerruns on tokio. Inside#[tokio::main]everything works..block()starts a shared tokio runtime for you when thebluetoothfeature is on. - Latency. Each request/reply round trip takes about 60 ms, because the
brick’s Bluetooth chip needs time to switch direction. Batch work into
fewer commands, use
send_no_replywhere you don’t need an answer, or run time-critical loops on the brick and talk to it with messages. - Default timeout: 3 seconds.
- One connection at a time. The brick accepts a single Bluetooth connection from a computer.
- The
bluetoothfeature is Linux-only. On Windows and macOS, use a serial port.