前提・実現したいこと
Android Studioを使用して、AndroidからPCへ以下の条件に沿った出力値をUDP通信で連続して出力するシステムを作成したいです。
・Button1が1度押下された場合、「A」が出力され、この値は0.1秒間保持される
・Button2が1度押下された場合、「B」が出力され、この値は0.1秒間保持される
・Button3が1度押下された場合、「C」が出力され、この値は0.1秒間保持される
・Buttonが押下されていない場合、「1」が出力され続ける
現状のプログラムとして
・アプリケーションが起動したときに、「1」が出力
・Button1が1度押下された場合、「A」が出力
・Button2が1度押下された場合、「B」が出力
・Button3が1度押下された場合、「C」が出力
までUDP通信で出力値を送信することができましたが、連続で出力する方法や出力値を保持する方法が分からず困っております。
※出力値はUdpIpToolで見ています。
該当のソースコード
MainActivity
Java
public class MainActivity extends AppCompatActivity { private static int PORT = 34567; //ポート宣言 DatagramSocket soc; //ソケット PrintWriter printWriter; //ソケット送信用 String text; int a = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //サーバー用にソケットを作成し、ポートを設定する try { soc = new DatagramSocket(34567); soc.bind(new InetSocketAddress(PORT)); } catch (Exception e) { e.printStackTrace(); } //接続されたら"1"を出力 SendSocTask sendSocTask = new SendSocTask(10); sendSocTask.execute(); text ="1"; Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SendSocTask sendSocTask = new SendSocTask(1); sendSocTask.execute(); text ="A"; } }); Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SendSocTask sendSocTask = new SendSocTask(2); sendSocTask.execute(); text ="B"; } }); Button button3 = findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SendSocTask sendSocTask = new SendSocTask(3); sendSocTask.execute(); text ="C"; } }); } class SendSocTask extends AsyncTask<InetSocketAddress, Void, Void> { private int sendValue; //コンストラクタ public SendSocTask(int value) { sendValue = value; } @Override protected Void doInBackground(InetSocketAddress... inetSocketAddresses) { try { byte[] ms = text.getBytes(); DatagramPacket packet = new DatagramPacket(ms, ms.length, new InetSocketAddress(InetAddress.getByName("PCのIPアドレス"),PORT)); soc.send(packet); } catch (Exception e) { } finally { try { //通信後、Socket接続を閉じ、関連付けられたすべてのリソース解放 if (a == 0) { printWriter.close(); } if (a==0) { } } catch (Exception e) { e.printStackTrace(); } } return null; } } }
activity_main
xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="10dp" android:text="Button" app:layout_constraintBaseline_toBaselineOf="@+id/button2" app:layout_constraintEnd_toStartOf="@+id/button2" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="10dp" android:layout_marginBottom="324dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/button3" app:layout_constraintStart_toEndOf="@+id/button1" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="1dp" android:text="Button" app:layout_constraintBaseline_toBaselineOf="@+id/button2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/button2" /> </androidx.constraintlayout.widget.ConstraintLayout>
試したこと
soc.send(packet);やclass SendSocTask内のコードにwhile(true)を挟み込み、無理に無限ループで出力してみましたが、
タブレットの画面は真っ白になり(正常に表示される場合もありました)、UdpIpToolのログも当たり前ですが「1」だけ出力され、「切断」をクリックしても無限ループし続けていました。
補足情報(FW/ツールのバージョンなど)
上記にも記載しましたが、Android studioを使用し、javaで作成しています。
どうにか実現したい事ができないかインターネットで調べてみましたが、
まったく実現方法がわかりませんでした…
お手数をおかけしますが、プログラミングの力も未熟なため丁寧に説明もいただけると大変ありがたいです。
何か不明点あれば、ご質問いただけますと幸いです。
よろしくお願いします。
まだ回答がついていません
会員登録して回答してみよう