如何将串行通信与arduino同步到平板电脑的垂直刷新?

时间:2019-05-31 17:27:01

标签: android bluetooth arduino serial-port

我正在尝试将Android平板电脑(Android 6.0 Marshmallow)和arduino(Mega 2560)之间的串行通信(通过蓝牙)同步到平板电脑的垂直刷新。我一直在使用Choreographer(https://developer.android.com/reference/android/view/Choreographer)来控制显示更新的时间,但是串行通信未同步到显示更新(并且它是可变的)。

有人知道如何进行这项工作吗?我已经在我的应用中添加了相关的android代码部分。

public void armVSyncHandler() {
    if(!frameCallbackPending) {
        frameCallbackPending = true;
        if(frameCallback == null)
        {
            frameCallback = new Choreographer.FrameCallback() {
                @Override
                public void doFrame(long frameTimeNanos) {
                    frameCallbackPending = false;

                    if (blackView.getVisibility() == View.INVISIBLE) {
                        blackView.setVisibility(View.VISIBLE);
                    } else {
                        blackView.setVisibility(View.INVISIBLE);
                    }
                    armVSyncStrobe();                    
                }
            };
        }
        Choreographer.getInstance().postFrameCallback(frameCallback);
    }
}

public void armVSyncStrobe() {
    if(!frameCallbackPendingS) {
        frameCallbackPendingS = true;
        if (frameCallbackS == null)
        {
            frameCallbackS = new Choreographer.FrameCallback() {
                @Override
                public void doFrame(long frameTimeNanos) {
                    frameCallbackPendingS = false;

                    String strobe = getBinaryTime();
                    try {
                        sendData(strobe);
                        Log.d(TAG,"data sent");
                        timeRecordDb.insertData(strobe, "receive: "+tvReceived.getText());
                    }
                    catch (IOException ex) {
                        Log.d(TAG, "data not sent");
                    }
                }
            };
        }
        Choreographer.getInstance().postFrameCallback(frameCallbackS);
    }
}

 public void sendData(String msg) throws IOException
    {
    try {
        mmOutputStream = mmSocket.getOutputStream();
    } catch (IOException e) {}

    msg += "\n";
    mmOutputStream.write(msg.getBytes());
}

0 个答案:

没有答案