简体   繁体   English

在实现主UI线程中可运行的类的方法上设置计时器

[英]Set timer on a method of class implementing runnable in main UI thread

I'm an absolute beginner in android. 我是android的绝对初学者。 Currently, I'm trying to make an app that sends output data to arduino via bluetooth. 目前,我正在尝试制作一个通过蓝牙将输出数据发送到arduino的应用程序。 For that purpose, I have created a class as follows. 为此,我创建了一个如下的类。

private class SendReceiveBytes implements Runnable {
    private BluetoothSocket btSocket;
    private InputStream inputStream;
    private OutputStream outputStream;
    String TAG = "SendReceiveBytes";

    public SendReceiveBytes(BluetoothSocket socket) {
        btSocket = socket;
        try {
            inputStream = btSocket.getInputStream();
            outputStream = btSocket.getOutputStream();
        }
        catch (IOException streamError) {
            Log.e(TAG, "Error when getting input or output Stream");
        }
    }

    @Override
    public void run() {
        // buffer store for the stream.
        byte[] buffer = new byte[1024];
        // bytes returned from the stream.
        int bytes;

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = inputStream.read(buffer);
                // Send the obtained bytes to the UI activity
                socketHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
            }
            catch (IOException e) {
                Log.e(TAG, "Error reading from inputStream");
                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(String outputData) {
        try {
            outputStream.write(outputData.getBytes(Charset.forName("UTF-8")));
        }
        catch (IOException e) {
            Log.e(TAG, "Error when writing to outputStream");
        }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            btSocket.close();
        }
        catch (IOException e) {
            Log.e(TAG, "Error when closing the btSocket");
        }
    }
}

In my OnCreate() method, I'm doing as follows. 在我的OnCreate()方法中,我正在执行以下操作。

final SendReceiveBytes sendBytes = new SendReceiveBytes(bluetoothSocket);
final Handler moveHandler = new Handler();
View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
        public boolean onLongClick(View view) {
            switch (view.getId()) {
                case R.id.driveFwd:
                    moveHandler.postDelayed(sendBytes.write("MOVE_FORWARD"), 250);
                    sendBytes.write("MOVE_FORWARD");
                    break;

The problem I'm currently having is with the right procedure for invoking method write(). 我目前遇到的问题是调用方法write()的正确过程。 What would be the right way to go on with that? 正确的做法是什么?

Look at this class that connects to an arduino. 看一下连接到arduino的此类。 I wrote this a while ago. 我前一段时间写的。 The problem is that if you do not know android starting here is very difficult. 问题是,如果您不知道android从这里开始是非常困难的。 Look at the BluetoothHandlerClass 看一下BluetoothHandlerClass

https://github.com/rush2sk8/BluetoothHandler https://github.com/rush2sk8/BluetoothHandler

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

相关问题 从实现 Runnable 的外部类访问 UI 线程或主线程 - Accessing UI Thread or Main Thread from External class implementing Runnable 使用对UI主线程使用回调后执行的方法创建Runnable类 - Creating Runnable class with method that using callback post execution to UI main thread 可以让一个实现Runnable()的类有一个方法将自己用于新的Thread吗? - Possible to have a class implementing Runnable() to have a method that uses itself for a new Thread? class正在实现runnable接口,但没有定义run方法 - class is implementing runnable interface but not defining the run method Java驱动程序类,Runnable和main方法 - Java driver class, Runnable, and main method jUnit-为具有无限循环的主包类方法设置计时器 - jUnit - Set timer for a main package class method having an infinite loop 在您自己的类中实现Runnable并使用Thread的方法 - Implementing Runnable and using Thread's methods within your own class 性能明智,可以更快地实现Runnable或扩展Thread类 - Performance wise which is faster implementing Runnable or extending a Thread class 在Blackberry中从主线程调用扩展UiApplication并实现Runnable的类 - invoke class that extends UiApplication and implements Runnable from main thread in Blackberry 实现Runnable接口的线程的子类 - subclass of thread implementing Runnable interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM