在日常生活中超聲波的應用還是很多的例如用於清洗眼鏡,探測距離.在探測距離的時候 兩個超聲波會有一定的夾角所以發出的聲波必定會相交於一點.這就形成了一個三角形,接下來就是計算的過程了.
具體實現如下:驅動開發者已位我們提供了驅動節點,我們只要去讀取驅動節點就能獲取超聲波探測的距離:
寫入工具:
public static void WriteData(String path, String content) {
FileOutputStream fos = null;
File file = new File(path);
if (file.exists()) {
try {
//注意new FileOutputStream的時候如果後面還需添加數據而不是覆蓋數據就因該在後面加true
fos = new FileOutputStream(file);
Log.e("File", "FileWriter");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] array = content.getBytes();
try {
fos.write(array);
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
讀取工具:
public static String Redata(String path) {
String content = new String();
String line = new String();
InputStreamReader is = null;
File file = new File(path);
if (file.exists()) {
try {
is = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(is);
try {
while ((line = bufferedReader.readLine()) != null) {
content = content + line;
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content;
}
另外我們還可以用adb shell去cat 文件的內容: