简体   繁体   English

如何将 BLE 设备在片段中发送的数据发送到新的活动以在应用程序中显示和绘制图形?

[英]How to send data sent by BLE Device in a fragment to a new activity for display and plotting graph in app?

I am completely new to Android and just learned Object-oriented programming.我对 Android 完全陌生,刚刚学习了面向对象的编程。 My project requires me to build something on open-source code.我的项目要求我在开源代码上构建一些东西。 I am really struggling with this special case.我真的在这个特殊情况下苦苦挣扎。 Two fragments are under activity_main, one of them is TerminalFragment. activity_main 下有两个 Fragment,其中一个是 TerminalFragment。 I added a menuItem (R.id.plot) in it and set if the user clicks this Item that will lead him from TerminalFragment to activity_main2.我在其中添加了一个 menuItem (R.id.plot),并设置了用户是否单击此 Item,它将引导他从 TerminalFragment 到 activity_main2。 Due to receive(byte data) and TerminalFragment still on active, the data is still printing out by receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0));由于接收(字节数据)和 TerminalFragment 仍处于活动状态,数据仍在通过 receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0)); 打印出来in activity_main.在活动主。

Now, I want to convert the data to String and send it to activity_main2 for display in recyclerview and plotting graph.现在,我想将数据转换为 String 并将其发送到 activity_main2 以在 recyclerview 中显示和绘图。 I am trying to use putextra and getextra but not sure how to access data from getextra in activity_main2.我正在尝试使用 putextra 和 getextra,但不确定如何从 activity_main2 中的 getextra 访问数据。 In my testing, the recyclerview just showed "John".在我的测试中,recyclerview 只显示“John”。 Is something missing in my method?我的方法中缺少什么吗? Does anyone have a better idea?有没有人有更好的主意? Much Appreciated.非常感激。

TerminalFragment终端片段

package de.kai_morich.simple_bluetooth_le_terminal;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class TerminalFragment extends Fragment implements ServiceConnection, SerialListener {

    private MenuItem menuItem;
    private enum Connected { False, Pending, True }

    private String deviceAddress;
    private SerialService service;

    private TextView receiveText;
    private TextView sendText;
    private TextUtil.HexWatcher hexWatcher;

    private Connected connected = Connected.False;
    private boolean initialStart = true;
    private boolean hexEnabled = false;
    private boolean pendingNewline = false;
    private String newline = TextUtil.newline_crlf;
    private String output;

    /*
     * Lifecycle
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        //Register with activity
        // You must inform the system that your app bar fragment is participating in the population of the options menu.
        // tells the system that your fragment would like to receive menu-related callbacks.

        setRetainInstance(true);
        deviceAddress = getArguments().getString("device");
    }

    @Override
    public void onDestroy() {
        if (connected != Connected.False)
            disconnect();
        getActivity().stopService(new Intent(getActivity(), SerialService.class));
        super.onDestroy();
    }

    @Override
    public void onStart() {
        super.onStart();
        if(service != null)
            service.attach(this);
        else
            getActivity().startService(new Intent(getActivity(), SerialService.class)); // prevents service destroy on unbind from recreated activity caused by orientation change
    }

    @Override
    public void onStop() {
        if(service != null && !getActivity().isChangingConfigurations())
            service.detach();
        super.onStop();
    }

    @SuppressWarnings("deprecation") // onAttach(context) was added with API 23. onAttach(activity) works for all API versions
    @Override
    public void onAttach(@NonNull Activity activity) {
        super.onAttach(activity);
        getActivity().bindService(new Intent(getActivity(), SerialService.class), this, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void onDetach() {
        try { getActivity().unbindService(this); } catch(Exception ignored) {}
        super.onDetach();
    }

    @Override
    public void onResume() {
        super.onResume();
        if(initialStart && service != null) {
            initialStart = false;
            getActivity().runOnUiThread(this::connect);
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        service = ((SerialService.SerialBinder) binder).getService();
        service.attach(this);
        if(initialStart && isResumed()) {
            initialStart = false;
            getActivity().runOnUiThread(this::connect);
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        service = null;
    }

    /*
     * UI
     */
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_terminal, container, false);
        receiveText = view.findViewById(R.id.receive_text);                          // TextView performance decreases with number of spans
        receiveText.setTextColor(getResources().getColor(R.color.colorRecieveText)); // set as default color to reduce number of spans
        receiveText.setMovementMethod(ScrollingMovementMethod.getInstance());

        sendText = view.findViewById(R.id.send_text);
        hexWatcher = new TextUtil.HexWatcher(sendText);
        hexWatcher.enable(hexEnabled);
        sendText.addTextChangedListener(hexWatcher);
        sendText.setHint(hexEnabled ? "HEX mode" : "");

        View sendBtn = view.findViewById(R.id.send_btn);
        sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));
        return view;
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_terminal, menu);
        menu.findItem(R.id.hex).setChecked(hexEnabled);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.clear) {
            receiveText.setText("");
            return true;
        } if (id == R.id.plot){
            Intent intent = new Intent(getActivity(), MainActivity2.class);
            startActivity(intent);
            //receive.;
            return true;
        }else if (id == R.id.newline) {
            String[] newlineNames = getResources().getStringArray(R.array.newline_names);
            String[] newlineValues = getResources().getStringArray(R.array.newline_values);
            int pos = java.util.Arrays.asList(newlineValues).indexOf(newline);
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Newline");
            builder.setSingleChoiceItems(newlineNames, pos, (dialog, item1) -> {
                newline = newlineValues[item1];
                dialog.dismiss();
            });
            builder.create().show();
            return true;
        } else if (id == R.id.hex) {
            hexEnabled = !hexEnabled;
            sendText.setText("");
            hexWatcher.enable(hexEnabled);
            sendText.setHint(hexEnabled ? "HEX mode" : "");
            item.setChecked(hexEnabled);
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    /*
     * Serial + UI
     */
    private void connect() {
        try {
            BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
            status("connecting...");
            connected = Connected.Pending;
            SerialSocket socket = new SerialSocket(getActivity().getApplicationContext(), device);
            service.connect(socket);
        } catch (Exception e) {
            onSerialConnectError(e);
        }
    }

    private void disconnect() {
        connected = Connected.False;
        service.disconnect();
    }

    private void send(String str) {
        if(connected != Connected.True) {
            Toast.makeText(getActivity(), "not connected", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            String msg;
            byte[] data;
            if(hexEnabled) {
                StringBuilder sb = new StringBuilder();
                TextUtil.toHexString(sb, TextUtil.fromHexString(str));
                TextUtil.toHexString(sb, newline.getBytes());
                msg = sb.toString();
                data = TextUtil.fromHexString(msg);
            } else {
                msg = str;
                data = (str + newline).getBytes();
            }
            SpannableStringBuilder spn = new SpannableStringBuilder(msg + '\n');
            spn.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorSendText)), 0, spn.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            receiveText.append(spn);
            service.write(data);
        } catch (Exception e) {
            onSerialIoError(e);
        }
    }

    private void receive(byte[] data) {
        if(hexEnabled) {
            receiveText.append("Hello" + TextUtil.toHexString(data) + '\n');
        } else {
            String msg = new String(data);
            if(newline.equals(TextUtil.newline_crlf) && msg.length() > 0) {
                // don't show CR as ^M if directly before LF
                msg = msg.replace(TextUtil.newline_crlf, TextUtil.newline_lf);
                // special handling if CR and LF come in separate fragments
                if (pendingNewline && msg.charAt(0) == '\n') {
                    Editable edt = receiveText.getEditableText();
                    if (edt != null && edt.length() > 1)
                        edt.replace(edt.length() - 2, edt.length(), "");
                }
                pendingNewline = msg.charAt(msg.length() - 1) == '\r';
            }
            receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0)); //print out data
            output = receiveText.toString(); // CharSequence to String
            Intent intent = new Intent(getActivity(), MainActivity2.class);
            intent.putExtra("output",output); // send data to next activity, MainActivity2
        }
    }

    private void status(String str) {
        SpannableStringBuilder spn = new SpannableStringBuilder(str + '\n');
        spn.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorStatusText)), 0, spn.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        receiveText.append(spn);
    }

    /*
     * SerialListener
     */
    @Override
    public void onSerialConnect() {
        status("connected");
        connected = Connected.True;
    }

    @Override
    public void onSerialConnectError(Exception e) {
        status("connection failed: " + e.getMessage());
        disconnect();
    }

    @Override
    public void onSerialRead(byte[] data) {// receive data
        receive(data); // send data to printout
    }

    @Override
    public void onSerialIoError(Exception e) {
        status("connection lost: " + e.getMessage());
        disconnect();
    }

}

MainActivity2.java (new activity) MainActivity2.java(新活动)

package de.kai_morich.simple_bluetooth_le_terminal;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity2 extends AppCompatActivity {

    Fragment CustomFragment;
    private ArrayList<Data> dataList;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar);
        recyclerView = findViewById(R.id.dataflow);
        dataList = new ArrayList<>();
        String data;
        if (savedInstanceState == null) {
            Bundle extra = getIntent().getExtras();
            if (extra == null) {
                data = null;
                receiveData(data);
            } else {
                data = extra.getString("output");
                receiveData(data);
            }
        } else {
            data = (String) savedInstanceState.getSerializable("output");
            receiveData(data);
        }
        setAdapter();
    }

    private void receiveData(String data){
        String Data = data;
        dataList.add(new Data("John"));
        dataList.add(new Data(Data));
    }

    private void setAdapter() {
        recyclerAdapter adapter = new recyclerAdapter(dataList);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_plot, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.dataplot:
                Toast.makeText(this, "dataplot", Toast.LENGTH_SHORT).show();
                replaceFragment(new DataPlotFragment());
                return true;
            case R.id.fft:
                Toast.makeText(this, "FFT", Toast.LENGTH_SHORT).show();
                replaceFragment(new FftFragment());
                return true;
            case R.id.data:
                Toast.makeText(this, "DATA", Toast.LENGTH_SHORT).show();
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    private void replaceFragment(Fragment fragment){

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.plotframelayout,fragment);
        fragmentTransaction.commit();
    }


}

I realized I also need to add getText() in receive().我意识到我还需要在receive() 中添加getText()。 However, the recycler view won't update itself, it just captures what it has in the recyclerview of activity_main with TerminalFragment when the user click the menuItem (id,plot).但是,回收器视图不会自行更新,它只是在用户单击 menuItem (id,plot) 时使用 TerminalFragment 捕获它在 activity_main 的回收器视图中的内容。 Either getextrastring() or using bundle able to pass the data to Mainactivity2. getextrastring() 或使用能够将数据传递给 Mainactivity2 的包。 I think I need to add something else to keep adding data to the recyclerview of activity_main2 from activity_main.我想我需要添加其他东西来继续从activity_main 向activity_main2 的recyclerview 添加数据。

onOptionsItemSelected of TerminalFragment TerminalFragment 的 onOptionsItemSelected

if (id == R.id.plot){
            Intent intent = new Intent(getActivity(), MainActivity2.class);
            intent.putExtra("output",output); //output
            startActivity(intent);
            return true;
        }

receive() of TerminalFragment终端片段的接收()

private void receive(byte[] data) {
        if(hexEnabled) {
            receiveText.append("Hello" + TextUtil.toHexString(data) + '\n');
        } else {
            String msg = new String(data);
            if(newline.equals(TextUtil.newline_crlf) && msg.length() > 0) {
                // don't show CR as ^M if directly before LF
                msg = msg.replace(TextUtil.newline_crlf, TextUtil.newline_lf);
                // special handling if CR and LF come in separate fragments
                if (pendingNewline && msg.charAt(0) == '\n') {
                    Editable edt = receiveText.getEditableText();
                    if (edt != null && edt.length() > 1)
                        edt.replace(edt.length() - 2, edt.length(), "");
                }
                pendingNewline = msg.charAt(msg.length() - 1) == '\r';
            }
            receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0)); //print out data
            output = receiveText.getText().toString(); // CharSequence to String
        }
    }

OnCreate of mainActivity2 mainActivity2 的 OnCreate

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar);
        recyclerView = findViewById(R.id.dataflow);
        dataList = new ArrayList<>();
        String data;
        Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
            data = extras.getString("output");
            receiveData(data);
        }
        setAdapter();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过BLE设备在当前活动中发送的数据流在下一个活动中不断更新recyclerview? - How to keep updating recyclerview in the next activity by the data flow sent by BLE device in current activity? 如何在相同的片段活动中将数据从片段发送到片段? - How to send data from fragment to fragment within same fragment activity? 如何将片段edittext数据发送到片段中没有按钮的活动 - How to send fragment edittext data to activity without buttons in the fragment 如何在 android 应用程序上从 BLE 设备接收数据 - How to receive data from BLE device on android app 如何连续显示BLE设备的RSSI? - How to display the RSSI of a BLE device continously? 如何将数据发送到新的 Android 活动 - How to send data to new Android activity 如何在片段中找到主机活动的按钮ID并将数据发送到片段中的主机活动 - how to find button id of host activity in the fragment and send data to the host activity in the fragment Android-如何将数据从主要活动发送到加载的片段 - Android - How to send data from main activity to loaded fragment 如何使用接口将数据从活动发送到Android中的Fragment? - How to send data from an activity to a Fragment in Android using interfaces? 如何将数据从 ViewModel 内的线程发送到主 Activity/Fragment? - How to send data from thread inside a ViewModel to main Activity/Fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM