2017-09-25 37 views
0

我正在一個項目中,我必須創建一個移動應用程序,通過藍牙讀出傳感器。我已經編碼了藍牙連接,我可以成功連接到我的傳感器並打印出接收到的數據。只要我連接,它就會一直監聽輸入流,如下面的代碼所示。Android編程發送連續的數據從類到片段

我的問題是,我不知道如何正確地將這些數據發送到我的片段。通常我會使用intent來發送數據,但是由於我連續收到數據,所以我不能使用這種方法。我一直在努力尋找這個解決方案几天,所以任何解決方案或建議非常讚賞。

目前的項目結構:

MainActivity.class,創建SensorConnector.class的實例。

SensorConnector.class,創建一個讀出傳感器的線程。像這樣:

 public void run() { 
     byte[] buffer = new byte[1024]; 
     int bytes; 
     // Keep listening to the InputStream while connected 
     while (true) { 
      try { 
       // Read from the InputStream 
       bytes = mmInStream.read(buffer); 
       String incomingMessage = new String(buffer, 0, bytes); 

       // Code to send incomingMessage to Dataview 

      } catch (IOException e) { 
       Log.d(TAG, "disconnected " + e); 
       break; 
      } 
     } 
    } 

DataviewFragment.class,該片段在這裏我想送我sensordata。包含一個文本框,我想持續更新讀取傳感器數據。

DataviewActivity.class,實現Dataview片段。

回答

2

有很多方法可以做到這一點,但我會使用簡單的接口和Observer模式來完成這項工作。

1)定義的接口:

public interface SensorListener { 
    void onUpdate(String incomingMessage); 
} 

2)請您MainActivityDataView片段實現的接口:

片段:

public class DataView extends Fragment implements SensorListener { 
    // or however its setup 
    @Override public void onUpdate(String incomingMessage) { 
     // Do whatever you need - use runOnUiThread if touching views 
    } 
} 

活動:(注意:FragmentManager也許支持版本,findFragmentByTag也許findFragmentById,再次不知道你的設置)

public class MainActivity extends Activity implements SensorListener { 
// or whatever your current setup is 

    @Override public void onUpdate(String incomingMessage) { 
     final DataView fragment = (DataView) getFragmentManager().findFragmentByTag("DataView"); 
     if(fragment != null){ 
      fragment.onUpdate(incomingMessage); 
     } 
    } 
} 

3)更新您的傳感器類:一是直接到片段前sensorConnector.setListener(this);

我選擇去通過在MainActivity:

public class SensorConnector { 


    private SensorListener listener; 

    public void setSensorListener(SensorListener listener){ 
     this.listener = listener; 
    } 

    public void removeListener(){ 
     this.listener = null; 
    } 

    public void startThread(){ 

     new Thread(new Runnable() { 
      @Override public void run() { 

        byte[] buffer = new byte[1024]; 
        int bytes; 
        // Keep listening to the InputStream while connected 
        while (true) { 
         try { 
          // Read from the InputStream 
          bytes = mmInStream.read(buffer); 
          String incomingMessage = new String(buffer, 0, bytes); 

          // Code to send incomingMessage to Dataview 
          if(listener != null) { 
           listener.onUpdate(incomingMessage); 
          } 

         } catch (IOException e) { 
          Log.d(TAG, "disconnected " + e); 
          break; 
         } finally { 
          removeListener(); 
         } 
        } 
      } 
     }).start(); 
    } 
} 

4)在您的Activity設置監聽器因爲您可能需要多個Fragment來觀察來自Activity的更新 - 這很容易適用於執行此操作,或實際上實現接口的任何操作。 removeListener()也不是排除在SensorConnector這應該被刪除當Activity被銷燬,刪除任何參考(不知道你的SensorConnector類的範圍/生命週期)。

2

你可以嘗試一個Listener模式。你的片段將實現一個監聽器接口,並且你的連接器將實現一個notifier接口。將您的片段註冊到通知程序,並在連接器收到數據時通知您的監聽器。最重要的是要記住,監聽器在被通知器調用時必須在片段所在的同一線程上完成更新工作。你可以在你的分片線程上使用Handler對象來實現。

例如,您可能具有片段實現以下接口:

interface ListenerInterface { 
    void update(Object data); 
} 

public class MyFragment extends Fragment implements ListenerInterface { 
    private Handler mHandler = new Handler(); 

    @Override 
    public void update(final Object data) { 
     //handle the notification here, use a Handler to do the work on 
     // the ui thread 
     mHandler.post(new Runnable() { 
      @Override 
      public void run() { 
       // this runs on the fragment's ui thread 
      } 
     }); 
    } 
    // ... 
} 

在您連接器的通知界面可能是這樣的......

interface NotifyInterface { 
    void registerListener(ListenerInterface listener) { 
} 

public class MyConnector implements NotifyInterface { 
    ListenerInterface mListener = null; 

    @Override 
    public void registerListener(ListenerInterface listener) { 
     mListener = listener; 
    } 

    private void doUpdate(Object data) { 
     if(mListener != null) { 
      mListener.update(data); 
     } 
    } 
    // then in your data generation code, call doUpdate as needed 
} 

用這種方法你會得到加在UI中保持UI登錄和連接器中的數據邏輯的好處。

相關問題