Android外部存储数据

时间:2019-03-03 11:10:37

标签: android arduino

我正在尝试使用蓝牙将arduino板中的数据保存到手机中的外部存储中。实际上,我可以在手机中找到一个文件,但是我的问题是我只能找到最后一个值,这是接收和存储数据的一部分:

bluetoothIn = new Handler() {
    public void handleMessage(android.os.Message msg) {
        if (msg.what == handlerState) {
            String readMessage = (String) msg.obj;
            DataStringIN.append(readMessage);

            int endOfLineIndex = DataStringIN.indexOf("#");

            if (endOfLineIndex > 0) {
                String dataInPrint = DataStringIN.substring(0, endOfLineIndex);
                IdBufferIn.setText("Dato: " + dataInPrint);
             //   savefile(fileName,dataInPrint);
               String state;
               state = Environment.getExternalStorageState();
               if(Environment.MEDIA_MOUNTED.equals(state)) {
                   File Root = Environment.getExternalStorageDirectory();
                   File Dir = new File(Root.getAbsolutePath() + "/MyAirfile");
                   if (!Dir.exists()) {
                       Dir.mkdir();
                   }
                   File file = new File(Dir, "MysensorData.txt");
                   String data = dataInPrint.toString();
                   try {
                       FileOutputStream fileOutputStream = new FileOutputStream(file);
                       fileOutputStream.write(data.getBytes());
                       fileOutputStream.close();
                       Toast.makeText(getApplicationContext(),"saved!!!",Toast.LENGTH_SHORT).show();
                   } catch (FileNotFoundException e) {
                       e.printStackTrace();
                   } catch (IOException e) {
                       e.printStackTrace();
                   }

               }

                DataStringIN.delete(0, DataStringIN.length());

            }
        }
    }
};

我的arduino代码很简单:

if(c=='1'){
 digitalWrite(LED_PIN, HIGH);          
 Serial.println("connect");
 bluetoothSerial.print("led open"); 
 bluetoothSerial.print("#");
}
if(c=='0'){
  digitalWrite(LED_PIN, LOW);
  Serial.println("led off#");
  bluetoothSerial.print("LED closed#"); 
}

} 对不起,我的英文。

0 个答案:

没有答案