Android端末でUnityで作成したアプリを起動する。画面にはボタンが配置されており、そのボタンを押すと離れた位置にあるArduinoにつないだBluetoothモジュールにシリアル通信で文字列が送られ、その文字列により異なるパターンでLEDを光らせる。
こういうシステムを作りたいです。一週間ほど調べてやっていますが、UnityはBluetooth機能をサポートしていないため、Android Studioを使用しモジュールを作らなければならないことがわかりました。
今現在、モジュールの中にBluetoothの処理をまとめた関数を用意し、それをUnityから呼び出すという考えに至り挑戦中です。
いろいろ調べすぎてもはやどのページを参考にしたかを詳しく覚えていませんが、現在はこのページを参考に、以下のプログラムをAndroid Studioで書いて、
lang
1package com.hoge.huga_plugin; 2 3import android.bluetooth.BluetoothAdapter; 4import android.bluetooth.BluetoothDevice; 5import android.bluetooth.BluetoothSocket; 6import android.os.Bundle; 7import android.widget.Toast; 8 9import com.unity3d.player.UnityPlayer; 10 11import java.io.IOException; 12import java.io.OutputStream; 13import java.util.UUID; 14 15 16public class UnityAndroidBluetoothPlugin { 17 18 19 private BluetoothAdapter mBTAdapter = null;//Bluetooth通信を行うために必要な情報を格納する 20 private BluetoothDevice mBTDevice = null;//実際に通信を行うデバイスの情報を格納する 21 private BluetoothSocket mBTSocket = null;//ソケット情報を格納する 22 private OutputStream mOutputStream = null;//出力ストリーム 23 24 private static String MacAddress = "00:06:66:EB:F3:1F";//アルファベットは全て大文字出ないとエラーになる 25 private static String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";//通信規格がSPPであることを示す数字 26 27 28 public void setup(){ 29 //ソケットを確立する関数 30 //BTConnect(); 31 32 //ソケットが取得出来たら、出力用ストリームを作成する 33 //if(mBTSocket != null){ 34 // try{ 35 // mOutputStream = mBTSocket.getOutputStream(); 36 // }catch(IOException e){ 37 38 // } 39 //} 40 } 41 42 private void BTConnect(){ 43 //BTアダプタのインスタンスを取得 44 mBTAdapter = BluetoothAdapter.getDefaultAdapter(); 45 46 //相手先BTデバイスのインスタンスを取得 47 mBTDevice = mBTAdapter.getRemoteDevice(MacAddress); 48 //ソケットの設定 49 try { 50 mBTSocket = mBTDevice.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID)); 51 } catch (IOException e) { 52 mBTSocket = null; 53 } 54 55 if(mBTSocket != null) { 56 //接続開始 57 try { 58 mBTSocket.connect(); 59 } catch (IOException connectException) { 60 try { 61 mBTSocket.close(); 62 mBTSocket = null; 63 } catch (IOException closeException) { 64 return; 65 } 66 } 67 } 68 } 69 70 private void Send(byte message){ 71 //文字列を送信する 72 try { 73 //ここで送信 74 mOutputStream.write(message); 75 } catch (IOException e) { 76 try{ 77 mBTSocket.close(); 78 }catch(IOException e1){/*ignore*/} 79 } 80 } 81}
これをUnityから
lang
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.UI; 6 7 AndroidJavaClass unityAndroidBluetoothPluginClass = new AndroidJavaClass("com.hoge.huga_plugin.UnityAndroidBluetoothPlugin"); 8 9 byte data; 10 11 12 // セットアップ 13 private void Start() 14 { 15 data = 0; 16 17 // Bluetoothの初期設定 18 unityAndroidBluetoothPluginClass.Call("setup"); 19 } 20 21 // ループ 22 private void Update() { 23 24 } 25 26 27 // ボタンが押された時 28 public void onClick() 29 { 30 unityAndroidBluetoothPluginClass.Call("send", data); 31 } 32 33}
という風に呼び出しています。
しかしこれをBuild And Runで実機で実行してみると、Made with Unityの画面から動きません。setup()の中身を空にしても同じ状態になったので、unityAndroidBluetoothPluginClass.Call("setup");自体に原因があると考えられます。
質問内容です。
・unityAndroidBluetoothPluginClass.Call("setup");という呼び出し方は問題があるのか?あるとすれば、どうやってプラグイン内のstaticでない関数をUnityから呼び出せばいいのか?
・そもそもこの方法は正しいのか?正しくないとして、どうすればこのシステムを実現できるか?
Android StudioもUnityも多少かじった程度の初心者で、こんな複雑なことをするのは初めてです。よろしくお願いします。
バージョン
Unity 2018.2.11f1
Android Studio 3.2
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/10/13 09:47 編集