Java Android编程将连续数据从类发送到片段

Java Android编程将连续数据从类发送到片段,java,android,android-fragments,bluetooth,Java,Android,Android Fragments,Bluetooth,我正在做一个项目,我必须创建一个通过蓝牙读取传感器的移动应用程序。我已经对蓝牙连接进行了编码,我可以成功连接到传感器并打印出接收到的数据。一旦连接,它就会继续监听输入流,如下面的代码所示 我的问题是,我不知道如何正确地将这些数据发送到我的片段。通常我会使用意向发送数据,但由于我连续接收数据,因此无法使用此方法。这几天来,我一直在努力寻找解决方案,因此非常感谢任何解决方案或建议 目前的项目结构: MainActivity.class,创建SensorConnector.class的实例 Senso

我正在做一个项目,我必须创建一个通过蓝牙读取传感器的移动应用程序。我已经对蓝牙连接进行了编码,我可以成功连接到传感器并打印出接收到的数据。一旦连接,它就会继续监听输入流,如下面的代码所示

我的问题是,我不知道如何正确地将这些数据发送到我的片段。通常我会使用意向发送数据,但由于我连续接收数据,因此无法使用此方法。这几天来,我一直在努力寻找解决方案,因此非常感谢任何解决方案或建议

目前的项目结构:

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,我想在其中发送传感器数据的片段。包含一个文本框,我想用读取的传感器数据不断更新该文本框


DataviewActivity.class,实现Dataview片段。

您可以尝试侦听器模式。您的片段将实现一个侦听器接口,您的连接器将实现一个通知器接口。向通知程序注册片段,当连接器收到数据时,通知侦听器。需要记住的主要一点是,当通知程序调用侦听器时,它必须在与片段相同的线程上执行更新工作。您可以使用片段线程上的
处理程序
对象来实现这一点

例如,您可以让片段实现以下接口:

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中,将数据逻辑保持在连接器中。

您可以尝试一种侦听器模式。您的片段将实现一个侦听器接口,您的连接器将实现一个通知器接口。向通知程序注册片段,当连接器收到数据时,通知侦听器。需要记住的主要一点是,当通知程序调用侦听器时,它必须在与片段相同的线程上执行更新工作。您可以使用片段线程上的
处理程序
对象来实现这一点

例如,您可以让片段实现以下接口:

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中,将数据逻辑保持在连接器中。

有很多方法可以做到这一点,但是我会使用简单的界面和观察者模式来完成这项工作

1) 定义一个接口:

public interface SensorListener {
    void onUpdate(String incomingMessage);
}
2) 使您的
MainActivity
DataView
片段实现接口:

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
            }
        });
    }
    // ...
}
片段:

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
,同样不知道您的设置)

3) 更新您的传感器类别:

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) 在
活动中
设置侦听器:
sensorConnector.setListener(此)


我选择在直接进入片段之前先查看MainActivity,因为您可能需要多个
片段
来观察
活动
中的更新-这很容易适应于执行此操作,或者任何实现接口的操作。另外,
removeListener()
并非传感器连接器的专有功能,当
活动
被销毁时,应将其删除,以删除任何引用(不知道您的
传感器连接器
类的范围/生命周期)。

有很多方法可以做到这一点,不过,我会使用简单的界面和观察者模式来完成这项工作

1) 定义一个接口:

public interface SensorListener {
    void onUpdate(String incomingMessage);
}
2) 使您的
MainActivity
DataView
片段实现接口:

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
            }
        });
    }
    // ...
}
片段:

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
,同样不知道您的设置)

3) 更新您的传感器类别:

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) 在
活动中
设置侦听器:
sensorConnector.setListener(此)

我选择在直接进入片段之前先查看MainActivity,因为您可能需要多个
片段
来观察
活动
中的更新-这很容易适应于执行此操作,或者任何实现接口的操作。另外,
removeListener()
并非传感器连接器的专有功能,当
活动
被销毁时,应删除该功能,以删除任何引用(不知道
传感器连接器
类的范围/生命周期)