我是java的新手,我想如何返回并显示多个值。
这是我的代码。
MainActivity.java
private void updatePressureValue(BluetoothGattCharacteristic characteristic) {
if (mPressureCals == null)
return;
double pressure = SensorTagData.extractBarometer(characteristic, mPressureCals);
double temp = SensorTagData.extractBarTemperature(characteristic, mPressureCals);
mTemperature.setText(String.format("%.1f\u00B0C", temp));
mPressure.setText(String.format("%.2f", pressure));
}
private void updateAccelerometerValue(BluetoothGattCharacteristic characteristic ){
double accelerometerx = SensorTagData.extractAccelerometer(characteristic, mAccelerometerx);
double accelerometery = SensorTagData.extractAccelerometer(characteristic, mAccelerometery);
double accelerometerz = SensorTagData.extractAccelerometer(characteristic, mAccelerometerz);
mAccelerometerx.setText(String.format("%.4f", accelerometerx));
mAccelerometery.setText(String.format("%.4f", accelerometery));
mAccelerometerz.setText(String.format("%.4f", accelerometerz));
}
}
SensorData.java
public class SensorData {
public static double extractHumAmbientTemperature(BluetoothGattCharacteristic c) {
int rawT = shortSignedAtOffset(c, 0);
return -46.85 + 175.72/65536 *(double)rawT;
}
public static double extractAccelerometer(BluetoothGattCharacteristic c) {
Integer x = c.getIntValue(FORMAT_SINT8, 0);
Integer y = c.getIntValue(FORMAT_SINT8, 1);
Integer z = c.getIntValue(FORMAT_SINT8, 2) * -1;
double scaledX = x / 64.0;
double scaledY = y / 64.0;
double scaledZ = z / 64.0;
return scaledX; scaledY; scaledZ ;
}
我从互联网上获取了代码,只显示了传感器的值,我想添加加速度计,但是鼻子如何。有人能帮助我吗?
感谢。
答案 0 :(得分:2)
Java中没有元组表面上容易让你这么做。与此相伴,函数参数没有参考传递。
在Java中执行此操作的常规方法是创建一个包含与返回值相关的字段的类,并返回该类的实例。这是一种深思熟虑的语言设计选择:返回类实例往往会产生良好的可维护代码。
答案 1 :(得分:1)
你可以返回一个数组......
public static double[] extractAccelerometer(BluetoothGattCharacteristic c) {
Integer x = c.getIntValue(FORMAT_SINT8, 0);
Integer y = c.getIntValue(FORMAT_SINT8, 1);
Integer z = c.getIntValue(FORMAT_SINT8, 2) * -1;
double scaledX = x / 64.0;
double scaledY = y / 64.0;
double scaledZ = z / 64.0;
return new double[] {scaledX, scaledY, scaledZ};
}
答案 2 :(得分:1)
列出双打并将其返回
public class SensorData {
public static double extractHumAmbientTemperature(BluetoothGattCharacteristic c) {
int rawT = shortSignedAtOffset(c, 0);
return -46.85 + 175.72/65536 *(double)rawT;
}
public static List<Double> extractAccelerometer(BluetoothGattCharacteristic c) {
Integer x = c.getIntValue(FORMAT_SINT8, 0);
Integer y = c.getIntValue(FORMAT_SINT8, 1);
Integer z = c.getIntValue(FORMAT_SINT8, 2) * -1;
double scaledX = x / 64.0;
double scaledY = y / 64.0;
double scaledZ = z / 64.0;
List<Double> list=new ArrayList<Double>();
list.add(scaledX);
list.add(scaledY);
list.add(scaledZ);
return list ;
}
答案 3 :(得分:0)
创建一个您想要返回的类:
public class myScaleData {
public double scaledX,scaledY,scaledZ;
}
答案 4 :(得分:0)
使用您需要的参数创建一个Object:
答案 5 :(得分:0)
尝试javatuples它比每次返回多个值时更容易上课,例如:
public static Triplet<Double,Double,Double> extractAccelerometer(BluetoothGattCharacteristic c) {
...
return new Triplet<Double,Double,Double>(scaledX,scaledY,scaledZ);
}