2017-07-26 18 views
0

我很擔心Android手機和Arduino藍牙模塊(HC-06)之間的藍牙通信。我在代碼的連續性方面遇到了問題。如果我開始調試,那麼在程序到達mConnectThread.start()之前根本沒有問題。線。此時它會「停止」:停留在調試模式下,但我徒勞地按下Step,不會繼續。顯然它不會跳轉到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

沒問題,很高興我能幫上忙。 –

相關問題