简体   繁体   English

Android频率振动

[英]Android frequency vibration

I am working on an android application which detect the hand shaking (through accelerometer) and it modulates the vibration frequency based on shaking intensity. 我正在开发一个Android应用程序,该程序可以检测手部震动(通过加速度计),并根据震动强度来调节振动频率。

I am able to get the tremor intensity through standard deviation (code below): 我可以通过标准偏差(以下代码)获得震颤强度:

public void regressionCalc() {
    //Calculating sum values
    double sumZ;
    double meanZ;
    sumZ = 0;

    final TextView accZText = (TextView) findViewById(R.id.accelText);
    accZText.setText("");

    for (int r = 0; r < recArray.length; r++) {
        sumZ = sumZ + recArray[r];
    }
    meanZ = sumZ/recArray.length;

    //Calculating standard deviation
    double sumXu2; //this represents E((x-xbar)^2)
    sumXu2 = 0;
    for (int x = 0; x < recArray.length; x++) {
        sumXu2 += ((recArray[x]-meanZ)*(recArray[x]-meanZ));
    }
    double SD;
    SD = Math.sqrt(sumXu2/(recArray.length-1));
    tremorIntensity = SD;

    accZText.setText(String.format("%.2f", tremorIntensity) + "");
    accZText.setTextSize(80);
}

but I don't know how to work with vibration. 但是我不知道如何振动。 I mean, I would like the phone vibration intensity decrease or increase based on the regression value. 我的意思是,我希望手机的振动强度根据回归值降低或增加。

Could anyone address me to a correct way? 有人可以用正确的方式向我讲话吗? Thanks in advance. 提前致谢。

The answer In my comment* shows how to make the device buzz according to a pattern: 我的评论*中的答案显示了如何根据模式使设备发出嗡嗡声:

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100};
v.vibrate(pattern, -1);

Since you already have a value for shaking intensity, the rest of the problem is just using your intensity value to generate values for pattern . 由于您已经有了抖动强度的值,因此剩下的问题只是使用强度值生成pattern值。

On the hardware level the buzzer is just on or off, so deciding what kind of changes to pattern best simulate changing intensity is a matter of trial and error. 在硬件级别上,蜂鸣器处于打开或关闭状态,因此要确定pattern哪种变化最好地模拟变化的强度是一个反复试验的问题。 It might be as simple as making each on value proportional to tremorIntensity : 这可能是为使每个简单on价值正比于tremorIntensity

long[] pattern = {0, tremorItensity * factor, 100}

* Here it is. * 在这里。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM