2017-01-25 3 views
0

코드를 작성하려고합니다. 가속도계 센서의 모든 결과 값을 .txt 파일에 쓸 수 있습니다. 어떻게 든 모든 데이터를 쓸 수는 없습니다. 내 루프에 문제가 있다고 생각합니다. 약 10-15 개의 샘플을 읽는 것입니다. 버튼을 멈추기 전까지 센서의 모든 값을 해당 파일에 기록하려면 어떻게해야합니까? 여기에 내가 작성한 코드가있다. 미리 감사드립니다.데이터 센서의 핸들 스트림

public class MainActivity extends Activity implements SensorEventListener { 
public SensorManager sm; 
Sensor accelermeter; 

private static final String DEBUG = "LogAccelermeter"; 
ToggleButton OnStore; 
Button OffStore; 
Button btnOn, btnOff; 
TextView txtArduino, txtString, txtStringLength, sensorView0, sensorView1, sensorView2, sensorView3; 
Handler bluetoothIn; 

final int handlerState = 0;       //used to identify handler message 
private BluetoothAdapter btAdapter = null; 
private BluetoothSocket btSocket = null; 
private StringBuilder recDataString = new StringBuilder(); 

private ConnectedThread mConnectedThread; 

// SPP UUID service - this should work for most devices 
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
TextView sensorText; 
// String for MAC address 
private static String address; 
private GestureDetectorCompat mDetector; 
FileOutputStream fileOutputStream; 

double TotalAccelerate; 
ArrayList<Double> list; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    list = new ArrayList<Double>(); 

    //Link the buttons and textViews to respective views 
    btnOn = (Button) findViewById(R.id.buttonOn); 
    btnOff = (Button) findViewById(R.id.buttonOff); 
    txtString = (TextView) findViewById(R.id.txtString); 
    txtStringLength = (TextView) findViewById(R.id.testView1); 
    sensorView0 = (TextView) findViewById(R.id.sensorView0); 
    sensorView1 = (TextView) findViewById(R.id.sensorView1); 
    sensorView2 = (TextView) findViewById(R.id.sensorView2); 
    sensorView3 = (TextView) findViewById(R.id.sensorView3); 

    //for Accelermeter 
    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    sensorText = (TextView) findViewById(R.id.sensor); 
    accelermeter = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    sm.registerListener(this, accelermeter, SensorManager.SENSOR_DELAY_NORMAL); 
    mDetector = new GestureDetectorCompat(this, new MyGestureListener()); 


    String state = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     File Root = Environment.getExternalStorageDirectory(); 
     File dir = new File(Root.getAbsolutePath() + "/MyApp"); 
     if (!dir.exists()) { 
      dir.mkdir(); 
     } 
     File file = new File(dir, "MyMessage.txt"); 
     try { 
      fileOutputStream = new FileOutputStream(file, true); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

    } else { 
     Toast.makeText(getApplicationContext(), "SDcard not found", Toast.LENGTH_LONG).show(); 
    } 

    OnStore = (ToggleButton) findViewById(R.id.onStore); 
    OnStore.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (OnStore.isChecked()){ 
       try { 
        for(double TotalAccelerate : list){ 
        // System.out.println("final"+ TotalAccelerate); 
         String space = "\n"; 
         byte[] convert = space.getBytes(); 
         fileOutputStream.write(convert); 
         String finalData; 
         finalData = String.valueOf(TotalAccelerate); 
         fileOutputStream.write(finalData.getBytes()); 
         Log.i(DEBUG, "ans: " + finalData); 
        } 
        // fileOutputStream.close(); 
        Toast.makeText(getApplicationContext(), "Message saving", Toast.LENGTH_LONG).show(); 

       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      }if (!OnStore.isChecked()){ 
       try { 
        fileOutputStream.flush(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       try { 
        fileOutputStream.close(); 
        list.clear(); 
        Collections.synchronizedList(list); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       Toast.makeText(getApplicationContext(),"Message Stopped.",Toast.LENGTH_LONG).show(); 

      } 


     } 

    }); 


    bluetoothIn = new Handler() { 
     public void handleMessage(android.os.Message msg) { 
      if (msg.what == handlerState) {          //if message is what we want 
       String readMessage = (String) msg.obj;      // msg.arg1 = bytes from connect thread 
       recDataString.append(readMessage);         //keep appending to string until ~ 
       int endOfLineIndex = recDataString.indexOf("~");     // determine the end-of-line 
       if (endOfLineIndex > 0) {           // make sure there data before ~ 
        String dataInPrint = recDataString.substring(0, endOfLineIndex); // extract string 
        txtString.setText("Data Received = " + dataInPrint); 
        int dataLength = dataInPrint.length();       //get length of data received 
        txtStringLength.setText("String Length = " + String.valueOf(dataLength)); 

        if (recDataString.charAt(0) == '#') //if it starts with # we know it is what we are looking for 
        { 
         String sensor0 = recDataString.substring(1, 5);    //get sensor value from string between indices 1-5 
         String sensor1 = recDataString.substring(6, 10);   //same again... 
         String sensor2 = recDataString.substring(11, 15); 
         String sensor3 = recDataString.substring(16, 20); 

         sensorView0.setText(" Sensor 0 Voltage = " + sensor0 + "V"); //update the textviews with sensor values 
         sensorView1.setText(" Sensor 1 Voltage = " + sensor1 + "V"); 
         sensorView2.setText(" Sensor 2 Voltage = " + sensor2 + "V"); 
         sensorView3.setText(" Sensor 3 Voltage = " + sensor3 + "V"); 
        } 
        recDataString.delete(0, recDataString.length());     //clear all string data 
        // strIncom =" "; 
        dataInPrint = " "; 
       } 
      } 
     } 
    }; 

    btAdapter = BluetoothAdapter.getDefaultAdapter();  // get Bluetooth adapter 
    checkBTState(); 

    // Set up onClick listeners for buttons to send 1 or 0 to turn on/off LED 
    btnOff.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      mConnectedThread.write("0"); // Send "0" via Bluetooth 
      Toast.makeText(getBaseContext(), "Turn off LED", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    btnOn.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      mConnectedThread.write("1"); // Send "1" via Bluetooth 
      Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

}//end OnCreate Method 

@Override 
public final void onAccuracyChanged(Sensor sensor, int accuracy) { 
    // Do something here if sensor accuracy changes. 
} 

@Override 
public final void onSensorChanged(SensorEvent event) { 
    // The light sensor returns a single value. 
    // Many sensors return 3 values, one for each axis. 
    double xx = event.values[0]; 
    double yy = event.values[1]; 
    double zz = event.values[2]; 
    TotalAccelerate = Math.round(Math.sqrt(Math.pow(xx, 2) 
      + Math.pow(yy, 2) 
      + Math.pow(zz, 2))); 
    Log.i(DEBUG, "Accelerometer = " + TotalAccelerate); 

    list.add(TotalAccelerate); 
    findPeaks(list); 
    sensorText.setText("Total: " + TotalAccelerate); 
    Log.i(DEBUG, "list values " + list); 

} 

//Find peak values. 
public static ArrayList<Double> findPeaks(List<Double> points) { 
    ArrayList<Double> peaks = new ArrayList<Double>(); 

    if (points == null || points.size() < 1) 
     return peaks; 

    Double x1_n_ref = 0.0; 
    int alpha = 0; //0=down, 1=up. 
    int size = points.size();// -1)/100; 
    for (int i = 0; i < size; i += 5) { 
     Double IndexValues = points.get(i); 
     if (IndexValues > 9) { 
      Double delta = (x1_n_ref - IndexValues); 
      if (delta < 0) { 
       x1_n_ref = IndexValues; 
       alpha = 1; 

      } else if (alpha == 1 && delta > 0) { 
       peaks.add(x1_n_ref); 

       alpha = 0; 
      } 

     } else if (alpha == 0) { 
      x1_n_ref = IndexValues; 
     } 
    } 

    return peaks; 
} 


@Override 
public boolean onTouchEvent(MotionEvent event) { 
    this.mDetector.onTouchEvent(event); 
    return super.onTouchEvent(event); 
} 

class MyGestureListener extends GestureDetector.SimpleOnGestureListener { 
    private static final String DEBUG_TAG = "Gestures"; 

    @Override 
    public boolean onDown(MotionEvent event) { 
     Log.d(DEBUG_TAG, "onDown: " + event.toString()); 
     Toast.makeText(getApplication(), "OnDown Touch Occur", Toast.LENGTH_LONG).show(); 
     if (event.getX() > 0) { 
      mConnectedThread.write("1"); 
     } 
     return true; 
    } 

    @Override 
    public void onLongPress(MotionEvent event) { 
     Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); 
     mConnectedThread.write("0"); 

    } 

private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException { 

    return device.createRfcommSocketToServiceRecord(BTMODULEUUID); 
    //creates secure outgoing connecetion with BT device using UUID 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    //Get MAC address from DeviceListActivity via intent 
    Intent intent = getIntent(); 

    //Get the MAC address from the DeviceListActivty via EXTRA 
    address = intent.getStringExtra(DeviceListActivity.EXTRA_DEVICE_ADDRESS); 

    //create device and set the MAC address 
    BluetoothDevice device = btAdapter.getRemoteDevice(address); 

    try { 
     btSocket = createBluetoothSocket(device); 
    } catch (IOException e) { 
     Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_LONG).show(); 
    } 
    // Establish the Bluetooth socket connection. 
    try { 
     btSocket.connect(); 
    } catch (IOException e) { 
     try { 
      btSocket.close(); 
     } catch (IOException e2) { 
      //insert code to deal with this 
     } 
    } 
    mConnectedThread = new ConnectedThread(btSocket); 
    mConnectedThread.start(); 

    //I send a character when resuming.beginning transmission to check device is connected 
    //If it is not an exception will be thrown in the write method and finish() will be called 
    mConnectedThread.write("x"); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    try { 
     //Don't leave Bluetooth sockets open when leaving activity 
     btSocket.close(); 
    } catch (IOException e2) { 
     //insert code to deal with this 
    } 
} 

//Checks that the Android device Bluetooth is available and prompts to be turned on if off 
private void checkBTState() { 

    if (btAdapter == null) { 
     Toast.makeText(getBaseContext(), "Device does not support bluetooth", Toast.LENGTH_LONG).show(); 
    } else { 
     if (btAdapter.isEnabled()) { 
     } else { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, 1); 
     } 
    } 
} 

//create new class for connect thread 
private class ConnectedThread extends Thread { 
    private final InputStream mmInStream; 
    private final OutputStream mmOutStream; 

    //creation of the connect thread 
    public ConnectedThread(BluetoothSocket socket) { 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     try { 
      //Create I/O streams for connection 
      tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
     } catch (IOException e) { 
     } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 
    } 


    public void run() { 
     byte[] buffer = new byte[256]; 
     int bytes; 

     // Keep looping to listen for received messages 
     while (true) { 
      try { 
       bytes = mmInStream.read(buffer);   //read bytes from input buffer 
       String readMessage = new String(buffer, 0, bytes); 
       // Send the obtained bytes to the UI Activity via handler 
       bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget(); 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

    //write method 
    public void write(String input) { 
     byte[] msgBuffer = input.getBytes();   //converts entered String into bytes 
     try { 
      mmOutStream.write(msgBuffer);    //write bytes over BT connection via outstream 
     } catch (IOException e) { 
      //if you cannot write, close the application 
      Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show(); 
      finish(); 

     } 
    } 
} 

}

+0

동봉 된 클래스 정의를 포함시켜 주시겠습니까? 이 코드 스 니펫은 사용중인 인터페이스 또는 기본 클래스를 표시하지 않습니다. 또한 버튼을 켜거나 끌 때 시간 간격은 어떻게됩니까? ** SENSOR_DELAY_NORMAL **을 (를) 사용 중이므로 상상 한만큼의 데이터 포인트를 생성하지 못할 수 있습니다. – solosodium

+0

좋아요, ** 귀하의 요청에 따라 ** ** 코드를 업데이트합니다. 블루투스 코드 부분은 관련이 없습니다. 나는 그것이 중요하다고 생각하지 않는다. –

+0

if (OnStore.isChecked())'''if (! OnStore.isChecked())'''와'''if (! OnStore .isChecked())'''를'''if (OnStore.isChecked())'''그것이 작동하는지 확인하십시오. – solosodium

답변

0

당신이 토글 버튼의 ​​onclick 콜백이 시도 수 있을까요? 선택을 취소하면 모든 데이터를 기록해야합니다.

: 기존 코드에 다음과 같이 수정

private boolean isListening = false; 

을 그리고합니다

OnStore.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     if (OnStore.isChecked()){ 
      // should do nothing 
     } else if (!OnStore.isChecked()){ 
      try { 
       for(double TotalAccelerate : list){ 
        //System.out.println("final"+ TotalAccelerate); 
        String space = "\n"; 
        byte[] convert = space.getBytes(); 
        fileOutputStream.write(convert); 
        String finalData; 
        finalData = String.valueOf(TotalAccelerate); 
        fileOutputStream.write(finalData.getBytes()); 
        Log.i(DEBUG, "ans: " + finalData); 
       } 
       fileOutputStream.flush(); 
       fileOutputStream.close(); 
       Toast.makeText(getApplicationContext(), "Message saving", Toast.LENGTH_LONG).show(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Toast.makeText(getApplicationContext(),"Message Stopped.",Toast.LENGTH_LONG).show(); 
     } 
    } 
}); 

은/시작이 클래스에 다음 변수 어딘가를 추가 센서 이벤트를 수신 중지 코드를 조절하려면

OnStore.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     if (OnStore.isChecked()){ 
      // 
      // set listening flag to true 
      // 
      isListening = true; 
     } else if (!OnStore.isChecked()){ 
      // 
      // set listening flag to false 
      // 
      isListening = false; 
      try { 
       for(double TotalAccelerate : list){ 
        //System.out.println("final"+ TotalAccelerate); 
        String space = "\n"; 
        byte[] convert = space.getBytes(); 
        fileOutputStream.write(convert); 
        String finalData; 
        finalData = String.valueOf(TotalAccelerate); 
        fileOutputStream.write(finalData.getBytes()); 
        Log.i(DEBUG, "ans: " + finalData); 
       } 
       fileOutputStream.flush(); 
       fileOutputStream.close(); 
       Toast.makeText(getApplicationContext(), "Message saving", Toast.LENGTH_LONG).show(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Toast.makeText(getApplicationContext(),"Message Stopped.",Toast.LENGTH_LONG).show(); 
     } 
    } 
}); 


@Override 
public final void onSensorChanged(SensorEvent event) { 
    // The light sensor returns a single value. 
    // Many sensors return 3 values, one for each axis. 
    // 
    // regulate 
    // 
    if (isListening) { 
     double xx = event.values[0]; 
     double yy = event.values[1]; 
     double zz = event.values[2]; 
     TotalAccelerate = Math.round(Math.sqrt(Math.pow(xx, 2) 
      + Math.pow(yy, 2) 
      + Math.pow(zz, 2))); 
     Log.i(DEBUG, "Accelerometer = " + TotalAccelerate); 

     list.add(TotalAccelerate); 
     findPeaks(list); 
     sensorText.setText("Total: " + TotalAccelerate); 
     Log.i(DEBUG, "list values " + list); 
    } 
} 
+0

그것은 작동합니다! 나는 여기에서 토론 할 충분한 포인트가 없다. 그들은 나를 막을 것이다. 그러나 마술처럼 작동합니다. 어떻게하면 코드를 승인 할 수 있습니까? –

+0

걱정할 필요없이 누군가 다른 사람이 대답을 표시합니다. 나중에 센서 데이터를 시작/중지하는 데 필요한 코드를 나중에 목록에 추가 할 것입니다. – solosodium

+0

정말 감사합니다. 나중에 어떻게 찾을 수 있을지 모르겠지만 어쨌든, 고마워. 최고의 소원. –