2017-07-26 1 views
0

私は、Android携帯とArduino Bluetoothモジュール(HC-06)の間のブルートゥース通信に苦労しています。私はコードの連続性に問題があります。デバッグを開始すると、プログラムがmConnectThread.start()に達するまで問題はありません。ライン。その時点で、「停止」します:デバッグモードに留まりますが、ステップオーバーを無駄に押しています。進まない。どうやらそれはConnectThreadクラスにはジャンプしませんし、なぜこのようなことが起こるのかわかりません。なぜmConnectThread.start()はプログラムをConnectThreadクラスに進めませんか?

ご協力いただきありがとうございます。

public class MainActivity extends AppCompatActivity { 

private BluetoothAdapter mBluetoothAdapter; 
private BluetoothDevice mDevice; 
private ConnectThread mConnectThread; 
private String MAC = "30:14:10:17:06:93"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TextView supportsBTorNot = (TextView) findViewById(R.id.supportsBTorNot); 
    TextView listPairedDevices = (TextView) findViewById(R.id.listPairedDevices); 

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    if (mBluetoothAdapter == null) { 
     supportsBTorNot.setText("The device does not support bluetooth."); 
    } 
    else{ 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, 1); 
     } 
     else{ 
      supportsBTorNot.setText("The device supports bluetooth."); 
     } 
    } 

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
    listPairedDevices.setText(pairedDevices.toString()); 
    if (pairedDevices.size() > 0) { 
     for (BluetoothDevice device : pairedDevices) { 
      if(device.getAddress().equals(MAC)) { 
       mDevice = device; 
       break; 
      } 
     } 
    } 
    if (mDevice == null) { 
     //Device is not paired yet 
     //Need to initiate a connection request 
    } 

    mConnectThread = new ConnectThread(mDevice); 
    mConnectThread.start(); 
} 


private class ConnectThread extends Thread { 

    private final BluetoothSocket mmSocket; 
    private ConnectedThread mConnectedThread; 
    private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 

    TextView socketConnected = (TextView) findViewById(R.id.socketConnected); 

    public ConnectThread(BluetoothDevice device) { 
     BluetoothSocket tmp = null; 
     try { 
      tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { } 
     mmSocket = tmp; 
    } 

    public void run() { 
     mBluetoothAdapter.cancelDiscovery(); 
     try { 
      mmSocket.connect(); 
     } catch (IOException connectException) { 
      try { 
       mmSocket.close(); 
      } catch (IOException closeException) { } 
      return; 
     } 
     if (mmSocket.isConnected()) { 
      socketConnected.setText("The socket is established successfully."); 
     } 
     else { 
      socketConnected.setText("The socket could not be stablished."); 
     } 

     mConnectedThread = new ConnectedThread(mmSocket); 
     mConnectedThread.start(); 
    } 
} 

答えて

0

Thread.start()を呼び出すと、単にデバッガは、実行の新しいスレッドのコンテキストにジャンプしないであろう、そのスレッドを開始します。あなたはそれを踏むためにスレッドのrun()メソッドでブレークポイントを設定する必要があります。 onCreate()メソッドの最後にstart()を呼び出しています。これはフレームワークで定義されたコールバックです。 start()への呼び出しをステップ実行するか実行を続行すると、onCreate()メソッドが返され、別のブレークポイントがヒットするまでデバッガは再び停止しません。

+0

ありがとうございました! :) – lazarea

+0

問題はありません、うれしい私は助けることができます。 –

関連する問題