LoginSignup
1
2

More than 3 years have passed since last update.

AndroidでBluetooth関係のAPIを使ってPAPERANGに接続してみる

Last updated at Posted at 2020-02-08

はじめに

正月に秋葉原行ってじゃんぱらでPAPERANGを購入。
で、PAPERANGのAPIないかなと探したけど、Cordovaのプラグインしか見つからなかったので、プラグインのソースをAndroidStudioで普通にアプリに使えないか試してみる。
もし、もっと良い方法を知ってる方がいたらコメントください。

うまく使えたら、Androidの会で何か作るときや、ハッカソンなんかで使ってみたい。

CordovaのPAPERANG pluginを見つけて、その中のjarを使ってできないか試したが、なんか、メーカーからIDとかもらわないとできそうもないのであきらめ、別の方法を模索。
Androidの会 浜松支部の定例会で、話をしていたところ、よさげなページを見つたので、そこを参考に紙送りができるところまで確認できた。

良さげなページは以下のサイト
M5StickC(ESP32)からBluetooth小型ポータブルレシートプリンタ「PAPERANG」を制御する
Bluetooth小型ポータブルレシートプリンタ「PAPERANG」
miaomiaoji-tool
ちゃんと使っていくためには、miaomiaoji-toolのコードを確認していかないといけない。

AndroidManifest.xml

こんな感じ

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="krohigewagma.jp.paperangsample">
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

コード

とりあえずのお試し適当コード。
接続履歴を使って、PAPERANGを探して接続するダサいコード。
本当は、Bluetoothで接続されているデバイスの中から選ばないといけない
ボタンを押すと、紙送りされます。


public class MainActivity extends AppCompatActivity {

    // CRC
    private static byte[] crc = new byte[]{
            (byte)0x02, (byte)0x18, (byte)0x00, (byte)0x04,
            (byte)0x00, (byte)0x78, (byte)0x7A, (byte)0xCE,
            (byte)0x33, (byte)0x2C, (byte)0x89, (byte)0x80,
            (byte)0xF0, (byte)0x03
    };

    // 紙送り
    byte[] data = new byte[]{
            (byte)0x02,                                     //
            (byte)0x1a, (byte)0x00,                         // 制御コード
            (byte)0x02, (byte)0x00,                         // データ長さ
            (byte)',' , (byte)0x01,                         // 紙送り量
            (byte)0x8b, (byte)'V', (byte)'#', (byte)'T',    // CRC
            (byte)0x03                                      //
    };

    private static String SPP = "00001101-0000-1000-8000-00805F9B34FB";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.btnInit);
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Context appContext = getApplicationContext();
/*
                // Cordovaのプラグインのjarを使ってやったコード。
                // ダメだったけど。とりあえず記念の残しておくけど後で消す
                String packageName = appContext.getPackageName();
                    PaperangApi.init(appContext, packageName, new OnInitStatusListener() {
                        @Override
                        public void initStatus(boolean b) {
                            Log.e("PAPERANG", "b = " + b);
                        }
                    });
                    PaperangApi.registerBT(appContext);
                    scan();
//                PaperangApi.unregisterBT(appContext);
//                    if(register()){
//                        Log.i("PAPERANG", "register success");
//                        scan();
//                        disconnect();
//                    }else{
//                        Log.i("PAPERANG", "register faild");
//                    }
 */
                // お試しなので、イケてないコードになってしまった・・・
                BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
                if(bt.equals(null)){
                    Log.i("PAPERANG", "Bluetooth not support");
                   return false;
                }
                if(!bt.isEnabled()){
                    Log.i("PAPERANG", "disable bluetooth");
                    return false;
                }

                Set<BluetoothDevice> devices = bt.getBondedDevices();
                List<String> macAddressList = new ArrayList<String>();
                for(BluetoothDevice device : devices){
                    Log.i("PAPERANG", device.getAddress() + device.getName());
                    if(!"Paperang".equals(device.getName())){
                        continue;
                    }
                    macAddressList.add(device.getAddress());
                    BluetoothSocket socket = null;
                    try{
                        socket = device.createRfcommSocketToServiceRecord(UUID.fromString(SPP));
                        socket.connect();
                        Log.i("PAPERANG", "connected.");

                        OutputStream outst = socket.getOutputStream();
                        outst.write(crc);
                        outst.write(data);
                    }catch(IOException e){
                        Log.e("PAPERANG", e.getMessage());
                    }finally {
                        try{
                            if(socket != null){
                                socket.close();
                            }
                        }catch(IOException e2){
                            Log.e("PAPERANG", e2.getMessage());
                        }
                    }
                    return false;
                }
                return false;
            }
        });
    }
}

おわり

次は印刷したい!

1
2
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
1
2