Android 蓝牙打印的动态数据

Android 蓝牙打印的动态数据,android,charts,bluetooth,plot,Android,Charts,Bluetooth,Plot,首先,我对Java编程语言非常陌生, 我试着寻找我想要的东西,但没有什么是具体的。 第二,我正在尝试从蓝牙设备绘制动态数据 只需使用Sensorgraph应用程序(开源应用程序)即可绘制数据,但绘图不太清晰(我无法放大或缩小,没有x轴或y轴…)。因此,我需要另一种方法来尝试绘制数据,以更好地显示它。 以下是SensorGraph代码: public class SensorGraph extends Activity { Button Start, Stop, Pause; private s

首先,我对Java编程语言非常陌生, 我试着寻找我想要的东西,但没有什么是具体的。 第二,我正在尝试从蓝牙设备绘制动态数据

只需使用
Sensorgraph
应用程序(开源应用程序)即可绘制数据,但绘图不太清晰(我无法放大或缩小,没有x轴或y轴…)。因此,我需要另一种方法来尝试绘制数据,以更好地显示它。 以下是
SensorGraph
代码:

public class SensorGraph extends Activity {
Button Start, Stop, Pause;

private static final String TAG = "SensorGraph";

// change this to your Bluetooth device address 
private static final String DEVICE_ADDRESS =  "00:06:66:4B:E2:B7"; //"00:06:66:03:73:7B";

private GraphView mGraph; 
private TextView mValueTV;
private ArduinoReceiver arduinoReceiver = new ArduinoReceiver();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // get handles to Views defined in our layout file
    mGraph = (GraphView)findViewById(R.id.graph);
    mValueTV = (TextView) findViewById(R.id.value);
    mGraph.setMaxValue(1024);


}

@Override
protected void onStart() {
    super.onStart();
    // in order to receive broadcasted intents we need to register our receiver
    registerReceiver(arduinoReceiver, new IntentFilter(AmarinoIntent.ACTION_RECEIVED));

    // this is how you tell Amarino to connect to a specific BT device from within your own code
    Amarino.connect(this, DEVICE_ADDRESS);
}


@Override
protected void onStop() {
    super.onStop();

    // if you connect in onStart() you must not forget to disconnect when your app is closed
    Amarino.disconnect(this, DEVICE_ADDRESS);

    // do never forget to unregister a registered receiver
    unregisterReceiver(arduinoReceiver);
}


/**
 * ArduinoReceiver is responsible for catching broadcasted Amarino
 * events.
 * 
 * It extracts data from the intent and updates the graph accordingly.
 */
public class ArduinoReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String data = null;

        // the device address from which the data was sent, we don't need it here but to demonstrate how you retrieve it
        final String address = intent.getStringExtra(AmarinoIntent.EXTRA_DEVICE_ADDRESS);

        // the type of data which is added to the intent
        final int dataType = intent.getIntExtra(AmarinoIntent.EXTRA_DATA_TYPE, -1);

        // we only expect String data though, but it is better to check if really string was sent
        // later Amarino will support differnt data types, so far data comes always as string and
        // you have to parse the data to the type you have sent from Arduino, like it is shown below
        if (dataType == AmarinoIntent.STRING_EXTRA){
            data = intent.getStringExtra(AmarinoIntent.EXTRA_DATA);

            if (data != null){
                mValueTV.setText(data);
                try {
                    // since we know that our string value is an int number we can parse it to an integer
                    final int sensorReading = Integer.parseInt(data);
                    mGraph.addDataPoint(sensorReading);
                }
                catch (NumberFormatException e) { /* oh data was not an integer */ }
            }
        }
    }
}

 }
graphview代码如下所示:

 public class GraphView extends View {

private Bitmap  mBitmap;
private Paint   mPaint = new Paint();
private Canvas  mCanvas = new Canvas();

private float   mSpeed = 1.0f;
private float   mLastX;
private float   mScale;
private float   mLastValue;
private float   mYOffset;
private int     mColor;
private float   mWidth;
private float   maxValue = 1024f;

public GraphView(Context context) {
    super(context);
    init();
}

public GraphView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init(){
    mColor = Color.argb(192, 64, 128, 64);
    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
}

public void addDataPoint(float value){
    final Paint paint = mPaint;
    float newX = mLastX + mSpeed;
    final float v = mYOffset + value * mScale;

    paint.setColor(mColor);
    mCanvas.drawLine(mLastX, mLastValue, newX, v, paint);
    mLastValue = v;
    mLastX += mSpeed;

    invalidate();
}

public void setMaxValue(int max){
    maxValue = max;
    mScale = - (mYOffset * (1.0f / maxValue));
}

public void setSpeed(float speed){
    mSpeed = speed;
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
    mCanvas.setBitmap(mBitmap);
    mCanvas.drawColor(0xFFFFFFFF);
    mYOffset = h;
    mScale = - (mYOffset * (1.0f / maxValue));
    mWidth = w;
    mLastX = mWidth;
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    synchronized (this) {
        if (mBitmap != null) {
            if (mLastX >= mWidth) {
                mLastX = 0;
                final Canvas cavas = mCanvas;
                cavas.drawColor(0xFFFFFFFF);
                mPaint.setColor(0xFF777777);
                cavas.drawLine(0, mYOffset, mWidth, mYOffset, mPaint);
            }
            canvas.drawBitmap(mBitmap, 0, 0, null);
        }
    } 
}
}

我需要更改的部分主要是传感器读数,并将其绘制在另一个图形上! 非常感谢你,我感谢你的帮助。 如果您有问题,请告诉我。


试试这个,这是一个使用Android Plot库的Android Plot示例代码

请在答案中包含该链接的有用部分,并在其中提供参考。因为如果或者当链接失效时,这个答案将是无用的。