前提・実現したいこと
Android-Arduino間でのBluetooth SPP通信。
Androidは8.0(Oreo),ArduinoはArduino Uno,BluetoothモジュールとしてUSB Host Shield+BTハブであらかじめペアリング済み。
Arduino側のプログラムはUSB Host Shieldライブラリのサンプルプログラム、「Bluetooth-SPP」を使用。
発生している問題・エラーメッセージ
Android-Arduino間の接続がかなり不安定で、接続成功する確率がかなり低いです。
ソースコードでは「ボタン1と2が押されたとき」⇒「Androidから値を送信」、「ボタン3が押されたとき」⇒「値を読み取る」という風に書いているつもりですが、Arduinoと接続した場合ボタンを押しても何も起こらなくなります。
(PCとの接続では思うように動いていました。)
該当のソースコード
Java
1package com.example.2209.testbt_2; 2 3import android.support.v7.app.AppCompatActivity; 4import android.os.Bundle; 5import android.bluetooth.BluetoothAdapter; 6import android.bluetooth.BluetoothDevice; 7import android.bluetooth.BluetoothSocket; 8import android.widget.Button; 9import android.widget.TextView; 10import android.widget.Toast; 11import android.view.View; 12import android.view.View.OnClickListener; 13import android.util.Log; 14import android.content.Intent; 15import android.os.Handler; 16import android.os.Message; 17 18import java.io.OutputStream; 19import java.io.InputStream; 20import java.util.UUID; 21import java.io.IOException; 22 23public class MainActivity extends AppCompatActivity{ 24 25 private final static int REQUEST_ENABLE_BT = 1; // BT権限 26 private BluetoothAdapter mBluetoothAdapter; // BTアダプタ 27 private BluetoothDevice mBtDevice; // BTデバイス 28 private BluetoothSocket mBtSocket; // BTソケット 29 private OutputStream mOutput; // 出力ストリーム 30 private InputStream mInput; //受信ストリーム 31 private Button btn1; // 送信ボタン1 32 private Button btn2; // 送信ボタン2 33 private Button btn3; // 受信ボタン 34 private Button btn4; // 接続ボタン 35 private TextView tx1; // 受信テキスト表示 36 private static final String TAG = "BluetoothSample"; // 受信テキスト格納 37 private static final int VIEW_INPUT = 1; // 取得文字列 38 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.activity_main); 43 44 // インスタンスを取得(必要ない?) 45 btn1 = (Button) findViewById(R.id.button1); 46 btn2 = (Button) findViewById(R.id.button2); 47 btn3 = (Button) findViewById(R.id.button3); 48 btn4 = (Button) findViewById(R.id.button4); 49 tx1 = (TextView) findViewById(R.id.text1); 50 tx2 = (TextView) findViewById(R.id.text2); 51 52 // BTの準備 -------------------------------------------------------------- 53 // BTアダプタのインスタンスを取得 54 if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) { 55 Toast.makeText(this, "Please enable to Bluetooth", Toast.LENGTH_LONG).show(); 56 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 57 startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 58 } else { 59 //BTが有効 60 } 61 62 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 63 64 // 相手先BTデバイスのインスタンスを取得 65 mBtDevice = mBluetoothAdapter.getRemoteDevice("00:1A:7D:DA:71:11"); 66 // Arduino 00:1A:7D:DA:71:11 67 68 // BTソケットのインスタンスを取得 69 try { 70 // 接続に使用するプロファイルを指定 71 mBtSocket = mBtDevice.createRfcommSocketToServiceRecord( 72 UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 73 } catch (IOException e) { 74 e.printStackTrace(); 75 } 76 77 btn4.setOnClickListener(new OnClickListener(){ 78 @Override 79 public void onClick(View v) { 80 Toast.makeText(getBaseContext(), "Connecting...", Toast.LENGTH_LONG).show(); 81 try { 82 mBtSocket.connect(); 83 mOutput = mBtSocket.getOutputStream(); // 出力ストリームオブジェクトを得る 84 mInput = mBtSocket.getInputStream(); // 入力 85 Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_LONG).show(); 86 } catch (IOException e) { 87 Toast.makeText(getBaseContext(), "Connection failed", Toast.LENGTH_LONG).show(); 88 e.printStackTrace(); 89 } 90 } 91 }); 92 93 btn3.setOnClickListener(new OnClickListener(){ 94 @Override 95 public void onClick(View v) { 96 //描画処理準備 97 Message valueMsg = new Message(); 98 99 try { 100 byte[] buffer = new byte[1024]; 101 int bytes; 102 bytes = mInput.read(buffer); 103 Log.i(TAG,"bytes="+bytes); 104 String readMsg = new String(buffer, 0, bytes); 105 106 if(readMsg.trim() != null && !readMsg.trim().equals("")){ 107 Log.i(TAG,"value="+readMsg.trim()); 108 109 valueMsg = new Message(); 110 valueMsg.what = VIEW_INPUT; 111 valueMsg.obj = readMsg; 112 mHandler.sendMessage(valueMsg); 113 } 114 else{ 115 // Log.i(TAG,"value=nodata"); 116 } 117 }catch (IOException e) { 118 e.printStackTrace(); 119 } 120 } 121 }); 122 123 //ボタンの動作 124 btn1.setOnClickListener(new OnClickListener(){ //ボタン1が押されたとき 125 @Override 126 public void onClick(View v) { 127 try { 128 mOutput.write(49); 129 } catch (IOException e) { 130 e.printStackTrace(); 131 } 132 } 133 }); 134 135 btn2.setOnClickListener(new OnClickListener(){ //ボタン2が押されたとき 136 @Override 137 public void onClick(View v) { 138 try { 139 mOutput.write(50); 140 } catch (IOException e) { 141 e.printStackTrace(); 142 } 143 } 144 }); 145 } 146 147 Handler mHandler = new Handler() { 148 @Override 149 public void handleMessage(Message msg) { 150 int action = msg.what; 151 String msgStr = (String)msg.obj; 152 if(action == VIEW_INPUT){ 153 tx1.setText(msgStr); 154 } 155 } 156 }; 157 158 @Override 159 protected void onPause(){ 160 super.onPause(); 161 try { 162 mBtSocket.close(); 163 } catch (IOException e) { 164 e.printStackTrace(); 165 } 166 } 167 168 @Override 169 protected void onDestroy() { 170 super.onDestroy(); 171 // ソケットを閉じる 172 try { 173 mBtSocket.close(); 174 } catch (IOException e) { 175 e.printStackTrace(); 176 } 177 } 178}
試したこと
PCとの接続(MACアドレスにPCのものを設定、Tera TermでCOMポート監視)では全く問題なく、接続・値の送受信が行えました。
S2 Terminal for Bluetoothを使用したArduinoとの接続・値の送受信は可能でした。(接続は不安定ですが)
補足情報(FW/ツールのバージョンなど)
C++,VBnetは扱えますが、javaは初心者のうえ、いろいろなサイトを参考にしながら書いているので至らぬ点が大量にあると思いますが、ご容赦ください...
したがって不足している情報があれば補足させていただきますので、ご教授頂けると幸いです。
あなたの回答
tips
プレビュー