Android编程发送连续的数据从类到片段
我正在一个项目中,我必须创建一个移动应用程序,通过蓝牙读出传感器。我已经编码了蓝牙连接,我可以成功连接到我的传感器并打印出接收到的数据。只要我连接,它就会一直监听输入流,如下面的代码所示。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片段。
有很多方法可以做到这一点,但我会使用简单的接口和Observer模式来完成这项工作。
1)定义的接口:
public interface SensorListener {
void onUpdate(String incomingMessage);
}
2)请您MainActivity
和DataView
片段实现的接口:
片段:
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
类的范围/生命周期)。
你可以尝试一个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登录和连接器中的数据逻辑的好处。