ボタンを押したらActivityを移動しBluetoothでシリアル通信を開始、PCから情報を受信した場合か、移動先のActivity(B)のボタンを押した場合に別Activity(C)に移動する動きを再現したいです。
以下のプログラムだと、Activity(B)が表示され、ボタンを押すとActivity(C)に移動します。しかし、PCから情報を受信出来ません。
そこでコメント文になっているtry.chathを外すとPCから受信されるようになり、Activity(C)に移動されますが、Activity(B)が表示されずに画面が真っ暗で待機状態になります。
何が原因なのでしょか?
package com.example.ae1502.myapplication; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; public class Stop extends Activity { private BluetoothAdapter mBluetoothAdapter; // BTアダプタ private BluetoothDevice mBtDevice; // BTデバイス private BluetoothSocket mBtSocket; // BTソケット private InputStream mInput; // 出力ストリーム private OutputStream mOutput; // 出力ストリーム @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stop); // BTアダプタのインスタンスを取得 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 相手先BTデバイスのインスタンスを取得 BluetoothDevice mBtDevice = mBluetoothAdapter.getRemoteDevice("00:1B:DC:06:4C:53"); // BTソケットのインスタンスを取得 try { // 接続に使用するプロファイルを指定 mBtSocket = mBtDevice.createRfcommSocketToServiceRecord( UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } catch (IOException e) { e.printStackTrace(); } // ソケットを接続する try { mBtSocket.connect(); mInput = mBtSocket.getInputStream(); // 入力ストリームオブジェクトを得る mOutput = mBtSocket.getOutputStream(); // 出力ストリームオブジェクトを得る } catch (IOException e) { e.printStackTrace(); } /* try { byte[] buffer = new byte[1024]; int bytes; bytes = mInput.read(buffer); if (bytes == 1){ Intent intent = new Intent(Stop.this,MainActivity.class); startActivity(intent); } } catch (IOException e) { e.printStackTrace(); } */ Button start =(Button)findViewById(R.id.st); start.setOnClickListener(new Teisi()); } private class Teisi implements OnClickListener{ public void onClick(View v){ try { mOutput.write('5'); } catch (IOException e) { e.printStackTrace(); } Intent intent = new Intent(Stop.this,MainActivity.class); startActivity(intent); } } }
A,B,CのどのActivityに原因があるかわからないので全て追記してください。また、質問の画面遷移の部分を見直してください。他人でもわかるように書いてください。