2014-02-19 201 views
3

我目前正在嘗試使用Android連接藍牙HC-05模塊,以便它通過模塊將字節發送到Android設備。我幾乎直接從android開發者頁面中盜取了一些代碼: http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection管理藍牙連接Android

這裏是Main。我遇到的問題是,mhandler沒有收到MESSAGE_READ的情況,這意味着我沒有收到來自我的模塊的數據。我想知道我需要做些什麼才能讓數據發送到運行MESSAGE_READ的情況?到目前爲止,該程序將設備配對併發送「成功連接」到我的arduino。

這也是一個以前問過的人誰可能比我更好措辭它沒有回答,所以我想我不是唯一一個。

https://stackoverflow.com/questions/20088856/no-data-buffered-from-bluetooth-module

我在我們的代碼中看到的差別主要是,他開始()他connectedThread()。感謝您的幫助!

public class Main_Activity extends Activity implements OnItemClickListener { 

ArrayAdapter<String> listAdapter; 

ListView listView; 
BluetoothAdapter btAdapter; 
Set<BluetoothDevice> devicesArray; 
ArrayList<String> pairedDevices; 
ArrayList<BluetoothDevice> devices; 
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
protected static final int SUCCESS_CONNECT = 0; 
protected static final int MESSAGE_READ = 1; 
IntentFilter filter; 
BroadcastReceiver receiver; 
String tag = "debugging"; 
Handler mHandler = new Handler(){ 
    @Override 
    public void handleMessage(Message msg) { 
     // TODO Auto-generated method stub 
     Log.i(tag, "in handler"); 
     super.handleMessage(msg); 
     switch(msg.what){ 
     case SUCCESS_CONNECT: 
      // DO something 
      ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj); 
      //Toast.makeText(getApplicationContext(), "CONNECT", 0).show(); 
      String s = "successfully connected"; 
      connectedThread.write(s.getBytes());     
      Log.i(tag, "connected"); 

      break; 
     case MESSAGE_READ: 
      byte[] readBuf = (byte[])msg.obj; 
      String string = new String(readBuf); 
      Toast.makeText(getApplicationContext(), string, 0).show();    
      break; 

     } 
    } 

}; 

這裏是應該送處理了代碼:我記得有與處理類似的問題

private class ConnectThread extends Thread { 

     private final BluetoothSocket mmSocket; 
     private final BluetoothDevice mmDevice; 

     public ConnectThread(BluetoothDevice device) { 
      // Use a temporary object that is later assigned to mmSocket, 
      // because mmSocket is final 
      BluetoothSocket tmp = null; 
      mmDevice = device; 
      Log.i(tag, "construct"); 
      // Get a BluetoothSocket to connect with the given BluetoothDevice 
      try { 
       // MY_UUID is the app's UUID string, also used by the server code 
       tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
      } catch (IOException e) { 
       Log.i(tag, "get socket failed"); 

      } 
      mmSocket = tmp; 
     } 

     public void run() { 
      // Cancel discovery because it will slow down the connection 
      btAdapter.cancelDiscovery(); 
      Log.i(tag, "connect - run"); 
      try { 
       // Connect the device through the socket. This will block 
       // until it succeeds or throws an exception 
       mmSocket.connect(); 
       Log.i(tag, "connect - succeeded"); 
      } catch (IOException connectException) { Log.i(tag, "connect failed"); 
       // Unable to connect; close the socket and get out 
       try { 
        mmSocket.close(); 
       } catch (IOException closeException) { } 
       return; 
      } 

      // Do work to manage the connection (in a separate thread) 

      mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget(); 
     } 



     /** Will cancel an in-progress connection, and close the socket */ 
     public void cancel() { 
      try { 
       mmSocket.close(); 
      } catch (IOException e) { } 
     } 
    } 

    private class ConnectedThread extends Thread { 
     private final BluetoothSocket mmSocket; 
     private final InputStream mmInStream; 
     private final OutputStream mmOutStream; 

     public ConnectedThread(BluetoothSocket socket) { 
      mmSocket = socket; 
      InputStream tmpIn = null; 
      OutputStream tmpOut = null; 

      // Get the input and output streams, using temp objects because 
      // member streams are final 
      try { 
       tmpIn = socket.getInputStream(); 
       tmpOut = socket.getOutputStream(); 
      } catch (IOException e) { } 

      mmInStream = tmpIn; 
      mmOutStream = tmpOut; 
     } 

     public void run() { 
      byte[] buffer; // buffer store for the stream 
      int bytes; // bytes returned from read() 

      // Keep listening to the InputStream until an exception occurs 
      while (true) { 


       try { 

        // Read from the InputStream 
        buffer = new byte[1024]; 
        bytes = mmInStream.read(buffer); 
        // Send the obtained bytes to the UI activity 
        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget(); 

       } catch (IOException e) { 
        break; 
       } 
      } 
     } 

回答

0

。很久以前,所以我不記得每一個細節,但它與mHandler.sendToTarget有關。我認爲有一個mHandler.dispatchMessage方法(或像這樣smoething - 我不在我的編程電腦上,所以我現在無法驗證)。 你應該試試看。

0

你確定字符串「成功連接」確實收到arduino?代碼片段不會顯示連接線程的寫入方法。請參閱android sdk中提供的bluetoothchat源代碼。

夫婦的區域,爲您找尋:

1)的連接線程不是盯着連接線前關閉。

2)常數的定義SUCCESS_CONNECT & MESSAGE_READ。他們是私人的,但從不同的班級訪問。在bluetoothchat源中,它們被定義爲公共。

此外,雖然來自不同的類,我們需要參考在那裏它們被定義的類,也就是指他們指這些常數,如下圖所示:

Main_Activity.SUCCESS_CONNECT 
Main_Activity.MESSAGE_READ. 
1

你使用線程是完全錯誤的。

創建線程對象不會啓動物理線程!

因此,正如你所做的那樣,在 中放置一個可能很慢的套接字創建器函數,線程對象的構造函數再次出錯。

而且,由於你還沒有開始() - 你的線程,socket connect()永遠不會被調用!

請嘗試一些沿着這些路線:

class ConnectThread extends Thread { 

    public ConnectThread(BluetoothDevice device) { 
      // nothing long running here! 
    } 

    public void run() { 
     device.createRfcommSocketToServiceRecord(MY_UUID); 

     // followed by connect() etc. 
    } 
} 

記住調用

connectedThread.start(); 

否則什麼也不會發生啓動線程。