Skip to content

Android Studio で開発を始める

Android Studio で開発するアプリケーションに Brother Print SDK を組み込む方法を紹介します。

SDK を追加する

SDK を Android Studio に追加する方法は2つあります。

Android Studio のウィザードに従う

  1. File > New > New Module > Import .JAR/.AAR Package と選択し、BrotherPrintLibrary.aar を追加する
  2. settings.gradle が次のように記述されていることを確認する
include ':app', ':BrotherPrintLibrary'
  1. アプリケーションの build.gradle に以下を追加する
dependencies {
    implementation project(":BrotherPrintLibrary")
}

手動で build.gradle に依存ライブラリを追記する

  1. プロジェクトルートディレクトリ以下の任意の場所に libs ディレクトリを作成し、そのディレクトリの中に BrotherPrintLibrary.aar を置く
    例:<#Repository Root#>/app/libs/BrotherPrintLibrary.aar

  2. プロジェクトルートの build.gradle に以下のように flatDir{} を追加する

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}
  1. アプリルートの build.gradle に以下のように dependencies{} を追加する
dependencies {
    implementation(name:'esprintsdk-lib-android-release', ext:'aar')
}

パーミッション設定を追加する

Manifest.xml に次のようにパーミッション設定を記述する。

<!-- For Wi-Fi -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- For Bluetooth -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<!-- For Bluetooth Low Energy -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

アプリケーションが PrinterInfo.workPath を利用しない場合は android.permission.WRITE_EXTERNAL_STORAGE を設定する必要はありません。

SDK の機能を実装する

プリンターと通信するためのサンプルコードです。詳細は各ガイドを参照してください。

import com.brother.sdk.lmprinter.Channel;
import com.brother.sdk.lmprinter.OpenChannelError;
import com.brother.sdk.lmprinter.PrinterDriver;
import com.brother.sdk.lmprinter.PrinterDriverGenerateResult;
import com.brother.sdk.lmprinter.PrinterDriverGenerator;

void yourGreatFeature() {
    Channel channel = Channel.newWifiChannel("IPAddress.of.your.printer");

    PrinterDriverGenerateResult result = PrinterDriverGenerator.openChannel(channel);
    if (result.getError().getCode() != OpenChannelError.ErrorCode.NoError) {
        Log.e("", "Error - Open Channel: " + result.getError().getCode());
        return;
    }
    Log.d("", "Success - Open Channel");

    PrinterDriver printerDriver = result.getDriver();
    //
    // Put any code to use printer
    //

    printerDriver.closeChannel();
}

プリンターと USB 接続する場合、Android 端末で通信を許可する必要があります。