実現したいこと
最終的にはandroid studio(android端末アプリ)を使って、Bluetooth経由でLEDライトをつけたり消したりするアプリを作ろうと思っております。
その為に、最初にArduinoからデータを送り、Android側でデータを受け取るというところを下記URLを参考に見ているのですが、
https://fabkura.gitbooks.io/android-docs/content/article2-2.html
コードのところで詰まってしまったのどうしたら動くか教えて頂きたいです。
MainActivity.javaのコードでエラーがでて上手くいきません。
前提
使用しているものは
Android studio, Bluetooth(HC05),Arduino UNO,LED,抵抗(330Ω)です。
これを配線しました。
発生している問題・エラーメッセージ
エラー: シンボルを見つけられません mmOutputStream = mSocket.getOutputStream(); ^ シンボル: 変数 mmOutputStream 場所: クラス MainActivity
該当のソースコード
MainActivity.Java
1package com.wabsarva.wings.android.bluetoothledarduinoandroid; 2 3import androidx.appcompat.app.AppCompatActivity; 4 5import android.bluetooth.BluetoothAdapter; 6import android.bluetooth.BluetoothDevice; 7import android.bluetooth.BluetoothSocket; 8import android.os.Bundle; 9import android.os.Handler; 10import android.os.Message; 11import android.util.Log; 12import android.view.View; 13import android.widget.Button; 14import android.widget.TextView; 15 16import java.io.IOException; 17import java.io.InputStream; 18import java.io.OutputStream; 19import java.util.Set; 20import java.util.TreeMap; 21import java.util.UUID; 22 23public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 24 25 //tag 26 private static final String TAG = "BluetoothSample"; 27 //Bluetooth Adapter 28 private BluetoothAdapter mAdapter; 29 30 //Bluetooth device 31 private BluetoothDevice mDevice; 32 33 //Bluetooth UUID(固定) 34 private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 35 36 //デバイス名 37 private final String DEVICE_NAME = "HC-05"; 38 39 //Soket 40 private BluetoothSocket mSocket; 41 42 //Thread 43 private Thread mThread; 44 45 //Threadの状態を表す 46 private boolean isRunning; 47 48 //接続ボタン 49 private Button connectButton; 50 51 //書き込みボタン 52 private Button writeButton; 53 54 //Status 55 private TextView mStatusTextView; 56 57 //Bluetoothから受信した値 58 private TextView mInputTextView; 59 60 //Action(ステータス表示) 61 private static final int VIEW_STATUS = 2; 62 63 //Action(取得文字列) 64 private static final int VIEW_INPUT = 3; 65 66 //Connect確認フラグ 67 private boolean connectFlg = false; 68 69 //BluetoothのOUTputStream 70 OutputStream mmOutputStream = null; 71 72 73 74 @Override 75 protected void onCreate(Bundle savedInstanceState) { 76 super.onCreate(savedInstanceState); 77 //Layoutにて設定したビューの表示 78 setContentView(R.layout.activity_main); 79 80 // TextViewの設定 81 mInputTextView = (TextView)findViewById(R.id.inputValue); 82 mStatusTextView = (TextView)findViewById(R.id.statusValue); 83 84 // Buttonの設定 85 connectButton = (Button)findViewById(R.id.connectButton); 86 writeButton = (Button)findViewById(R.id.writeButton); 87 88 // ボタンのイベント設定 89 connectButton.setOnClickListener(this); 90 writeButton.setOnClickListener(this); 91 92 // Bluetoothのデバイス名を取得 93 // DVICE_NAMEでデバイス名を定義 94 mAdapter = BluetoothAdapter.getDefaultAdapter(); 95 mStatusTextView.setText("SearchDevice"); 96 Set< BluetoothDevice > devices = mAdapter.getBondedDevices(); 97 for ( BluetoothDevice device : devices){ 98 99 if(device.getName().equals(DEVICE_NAME)){ 100 mStatusTextView.setText("find: " + device.getName()); 101 mDevice = device; 102 } 103 } 104} 105 // 別のアクティビティが起動した場合の処理 106 @Override 107 protected void onPause(){ 108 super.onPause(); 109 110 isRunning = false; 111 connectFlg = false; 112 113 try{ 114 mSocket.close(); 115 } 116 catch(Exception e){} 117 } 118 119 // スレッド処理(connectボタン押下後に実行) 120 @Override 121 public void run() { 122 InputStream mmInStream = null; 123 124 Message valueMsg = new Message(); 125 valueMsg.what = VIEW_STATUS; 126 valueMsg.obj = "connecting..."; 127 mHandler.sendMessage(valueMsg); 128 129 try{ 130 131 // 取得したデバイス名を使ってBluetoothでSocket接続 132 mSocket = mDevice.createRfcommSocketToServiceRecord(MY_UUID); 133 mSocket.connect(); 134 mmInStream = mSocket.getInputStream(); 135 mmOutputStream = mSocket.getOutputStream(); 136 137 // InputStreamのバッファを格納 138 byte[] buffer = new byte[1024]; 139 // 取得したバッファのサイズを格納 140 int bytes; 141 valueMsg = new Message(); 142 valueMsg.what = VIEW_STATUS; 143 valueMsg.obj = "connected."; 144 mHandler.sendMessage(valueMsg); 145 146 connectFlg = true; 147 148 while(isRunning){ 149 150 // InputStreamの読み込み 151 bytes = mmInStream.read(buffer); 152 Log.i(TAG,"bytes="+bytes); 153 // String型に変換 154 String readMsg = new String(buffer, 0, bytes); 155 156 // null以外なら表示 157 if(readMsg.trim() != null && !readMsg.trim().equals("")){ 158 Log.i(TAG,"value="+readMsg.trim()); 159 160 valueMsg = new Message(); 161 valueMsg.what = VIEW_INPUT; 162 valueMsg.obj = readMsg; 163 mHandler.sendMessage(valueMsg); 164 } 165 } 166 } 167 // エラー処理 168 catch(Exception e){ 169 170 valueMsg = new Message(); 171 valueMsg.what = VIEW_STATUS; 172 valueMsg.obj = "Error1:" + e; 173 mHandler.sendMessage(valueMsg); 174 175 try{ 176 mSocket.close(); 177 }catch(Exception ee){} 178 isRunning = false; 179 connectFlg = false; 180 } 181 } 182 183 // ボタン押下時の処理 184 @Override 185 public void onClick(View v) { 186 if(v.equals(connectButton)) { 187 // 接続されていない場合のみ 188 if (!connectFlg) { 189 mStatusTextView.setText("try connect"); 190 191 mThread = new Thread(this); 192 // Threadを起動し、Bluetooth接続 193 isRunning = true; 194 mThread.start(); 195 } 196 } else if(v.equals(writeButton)) { 197 // 接続中のみ書込みを行う 198 if (connectFlg) { 199 try { 200 // Writeボタン押下時、'2'を送信 201 mmOutputStream.write("2".getBytes()); 202 // 画面上に"Write:"を表示 203 mStatusTextView.setText("Write:"); 204 } catch (IOException e) { 205 Message valueMsg = new Message(); 206 valueMsg.what = VIEW_STATUS; 207 valueMsg.obj = "Error2:" + e; 208 mHandler.sendMessage(valueMsg); 209 } 210 } else { 211 mStatusTextView.setText("Please push the connect button"); 212 } 213 } 214 } 215 216 //描画処理はHandlerでおこなう 217 Handler mHandler = new Handler() { 218 @Override 219 public void handleMessage(Message msg) { 220 int action = msg.what; 221 String msgStr = (String)msg.obj; 222 if(action == VIEW_INPUT){ 223 mInputTextView.setText(msgStr); 224 } 225 else if(action == VIEW_STATUS){ 226 mStatusTextView.setText(msgStr); 227 } 228 } 229 }; 230}
その他のAndroid studio側のソースコード
AndroidManifext.xml
1 2<?xml version="1.0" encoding="utf-8"?> 3<manifest xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools"> 5 6 <uses-permission android:name="android.permission.BLUETOOTH" /> 7 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 8 9 <application 10 android:allowBackup="true" 11 android:dataExtractionRules="@xml/data_extraction_rules" 12 android:fullBackupContent="@xml/backup_rules" 13 android:icon="@mipmap/ic_launcher" 14 android:label="@string/app_name" 15 android:supportsRtl="true" 16 android:theme="@style/Theme.BluetoothLEDArduinoAndroid" 17 tools:targetApi="31"> 18 <activity 19 android:name=".MainActivity" 20 android:exported="true"> 21 <intent-filter> 22 <action android:name="android.intent.action.MAIN" /> 23 24 <category android:name="android.intent.category.LAUNCHER" /> 25 </intent-filter> 26 </activity> 27 </application> 28 29</manifest>
activity_main.xml
1 2<?xml version="1.0" encoding="utf-8"?> 3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:orientation="vertical" > 7 8 <Button android:id="@+id/connectButton" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="Connect" /> 12 13 <TextView 14 android:id="@+id/statusValue" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" /> 17 18 <TextView 19 android:id="@+id/inputValue" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 /> 23 24 <Button android:id="@+id/writeButton" 25 android:layout_width="fill_parent" 26 android:layout_height="wrap_content" 27 android:text="Write" /> 28 29</LinearLayout>
Arduino UNO側のソースコード
JAVA
1 2#include <SoftwareSerial.h> 3 4SoftwareSerial android(2,3); 5 6 7void setup() { 8 // put your setup code here, to run once: 9 //bluetooth用シリアルポートを設定 10 android.begin(9600); 11 //arduinoのシリアルモニタ用 12 Serial.begin(9600); 13 14 Serial.write("init"); 15} 16 17void loop() { 18 // put your main code here, to run repeatedly: 19 20 if(android.available()){ 21 Serial.println(android.read()); 22 } 23 24 if(Serial.available()){ 25 android.write(Serial.read()); 26 } 27 28}

回答2件
あなたの回答
tips
プレビュー