LoginSignup
2
0

WearOSでRustからHello World

Posted at

前置き

TicWatch E2というスマートウォッチ(WearOS)を手に入れたので、この前作ったクロスプラットフォームライブラリのNickname1を対応させようとした結果、そもそもRustでWearOS向けにビルドする方法が出なかったので、行き当たりばったりで試したら一応うまくいった。
リポはここ

技術的な話

なんかないかと調べたのだが、FlutterでWearOS向けに作れるという話や、Android向けとほとんど同じように作れるという話から、バイナリ的にはAndroidと同じだろうと予想していた。

やってみたこと

まずはadbで接続した(WiFi経由)。
次に、コードを用意

src/main.rs
fn main() {
    println!("Hello, world!");
}

最後に、デバイスを確認してGO!!

$ adb devices
List of devices attached
192.168.11.32:5555      device

$ cargo dinghy -d 192.168.11.32:5555 run

image.png

なんか出てますがうまく行きました!!

最後に、確認のためライブラリを試しに使ってみます。
getrandomreqwestrustls+blockingを使って、こんな複雑でネットを使うライブラリなどでも行けるのか試しましょう。

コードはこんな感じ

use reqwest::blocking::*;

fn main() {
    println!("Hello, world!");

    let mut buff = [0; 8];
    getrandom::getrandom(&mut buff).unwrap();
    println!("{:?}", buff);

    let body = get("https://www.rust-lang.org").unwrap().text().unwrap();

    #[cfg(debug_assertions)]
    println!("Debug mode");

    #[cfg(not(debug_assertions))]
    println!("Release mode");

    println!("body = {:#?}", body.lines().collect::<Vec<_>>()[0..10].to_vec());

    println!("Goodbye, world!")
}

image.png

上手く動きましたね。
つまりAndroid互換ということ。
ということで、cargo-apkの後継のxbuild君には、是非WearOS向けのビルドを実装してほしいところですね。

  1. ユーザ名を取得したりするライブラリ

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0