์๋ ํ์ธ์! ์๋์ด๋ ธ, ์๋๋ก์ด๋ ๋ธ๋ฃจํฌ์ค ์ค์ต ๋๋ฒ ์งธ ์๊ฐ์ ๋๋ค.
์ค๋์ ์๋๋ก์ด๋ ์คํ๋์ค๋ก ์ผ์ ๊ฐ์ ๋ฐ์์ค๋ ์ดํ์ ๋ง๋ค์ด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์๋ง ๋ง์ ๋ถ๋ค๊ป์ Code-jun๋์ ๋ธ๋ฃจํฌ์ค ์์ ๋ฅผ ๋ณด์ จ์๊ฑฐ๋ผ๊ณ ์๊ฐํฉ๋๋ค.
์ค์ ๋ก๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋นํ ์ ๋ง๋ค์ด์ ธ ์์ด ์ฌ์ฉํ๋๋ฐ ์ด๋ ค์์ด ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
์ ๋ ์ด๋ฒ์ Code-jun๋์ด ์ฌ๋ฆฌ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ ๋ฅผ ํตํด ๋ธ๋ฃจํฌ์ค ๋ฌด์ ํต์ ์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค.
์ ์ฌ์ดํธ ๋ฐฉ๋ฒ๋๋ก ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ ํ ์ด์ ์ ๋ฐฉ์๋๋ก ์ฝ๋ฉ์ ํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
1. ๋์์ธ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize ="40sp"
android:layout_gravity = "center"
android:text="์จ์ต๋์ผ์('C, %)" />
<Button
android:id="@+id/btnConnect"
android:layout_gravity = "center"
android:layout_width="300dp"
android:layout_height="50dp"
android:text="์ฐ๊ฒฐ"
/>
<TextView
android:id="@+id/temp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textSize ="50sp"
android:gravity="center_horizontal"
android:hint="์จ๋" />
<TextView
android:id="@+id/humd"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textSize="50sp"
android:gravity="center_horizontal"
android:hint="์ต๋" />
</LinearLayout>
๋์์ธ์ ์ ๊ฒฝ์ฐ์๋ ๋ถ๋ค๊ป์๋ ์์ ์ ์ ๋ง๋๋ก ๊ณ ์ณ ์ฐ์๋ฉด ๋ฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ฌ์ฉํ์๊ธฐ ์ ์ ์ฃผ์ํ ์ ์ด ์์ต๋๋ค. ์ ๊ฐ ๊ตต๊ฒ ํ์ํ android.hint ๋ถ๋ถ์ ๋ธ๋ฃจํฌ์ค์์ ๊ฐ์ด ์ ๋ฌ๋๊ธฐ
์์ํ๋ฉด textview ๋ถ๋ถ์ด ๋ฐ๊ปด์ผ ํ๊ธฐ ๋๋ฌธ์ android.text๋ก ํ์๋ฉด ์๋ฉ๋๋ค.
๊ฐ์ด ์ฌ๋ฐ๋ฅด๊ฒ ๊ฐ๋๋ผ๋ ๊ฐ์ด ๋ณํ์ง ์์ต๋๋ค.
2. ๋ฐ์ดํฐ ์์
bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() { //๋ฐ์ดํฐ ์์
public void onDataReceived(byte[] data, String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
์ถ์ฒ: https://blog.codejun.space/13 [CodeJUN]
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ์ฒ : https://github.com/akexorcist/Android-BluetoothSPPLibrary
์ ํจ์๋ ๋ฐ์ดํฐ๋ฅผ ์์ ๋ฐ๊ณ ์์ต๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น์?
๋ฐฉ๋ฒ์ ๋ ๊ฐ์ง์ ๋๋ค. ์๋์ด๋ ธ์์ ๋ฌธ์๋ก ๋ณด๋ด๋ ๋ฐฉ๋ฒ, ๋ฐ์ดํธ๋ก ๋ณด๋ด๋ ๋ฐฉ๋ฒ. ๋๊ฐ์ง ์ ๋๋ค.
์ฐ๋ฆฌ๋ ๋ฐ์ดํธ๋ก ๊ณ์ฐ์ ํด์ ๊ฐ์ ๋ณด๋ด๋ ๊ฒ์ ์ด๋ ค์ฐ๋ ์ ํฌ๋ ๋ฌธ์๋ก ๋ณด๋ด๋๋ก ํ๊ฒ ์ต๋๋ค.
ํ์ง๋ง ์๊ฐํด ๋ด์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
์ฐ๋ฆฌ๊ฐ ์๋์ด๋ ธ์์ ์๋๋ก์ด๋๋ก ๋ณด๋ผ ๊ฐ์ ์จ๋, ์ต๋ ๋ ๊ฐ์ง์ธ๋ฐ ์ด๋ป๊ฒ ํด์ผํ๋.
์ ๋ต์ ํ ๋ฒ์ ๋ฌธ์์ด๋ก ๋ณด๋ธ ํ, ์์ ๋ถ์์ ์ชผ๊ฐ๋ฉด ๋ฉ๋๋ค.
bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
TextView temp = findViewById(R.id.temp);
TextView humd = findViewById(R.id.humd);
public void onDataReceived(byte[] data, String message) {
String[] array = message.split(",");
temp.setText(array[0].concat("C"));
humd.setText(array[1].concat("%") );
}
});
๊ทธ๋ฆฌ๊ณ ๋ฌธ์์ด์ ๋์ ๊ฐํ๋ฌธ์๋ก ๋์ด ๋ฉ๋๋ค. (์ค์ ์ค์)
๊ฐํ๋ฌธ์?? new line(๋์ด์ฐ๊ธฐ)์ ๋งํฉ๋๋ค.
๊ทธ๋ผ ์๋์ด๋ ธ ์ก์ ์ค์ผ์น๋ฅผ ๋ค์ ํ๋ฒ ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์! ๋ค์ ๋ณด๋ ์ดํด๊ฐ ๊ฐ์๋์?
์๋์ด๋ ธ์์๋ ๋ธ๋ฃจํฌ์ค๋ก
[temp(value), humd(value) /n] ์ด๋ฌํ ํํ์ ๋ฌธ์์ด๋ก 1์ด์ ํ ๋ฒ์ฉ ๋ธ๋ฃจํฌ์ค์๊ฒ ๊ฐ์ ๋ณด๋ด์ค๋๋ค.
์ด? ๋ง์ฝ ์๋๋ก์ด๋ ์คํ๋์ค์์ String์ผ๋ก ๊ฐ์ ๋ฐ์๋ค?
๋๋ ์ซ์๋ก ๋ฐ์์ ์๋๋ก์ด๋ ์คํ๋์ค์์ ๊ณ์ฐํด์ ๊ฐ์ ์ง์ด๋ฃ๊ณ ์ถ์๋ฐ?
๋ฐฉ๋ฒ : ์๋๋ก์ด๋ ์คํ๋์ค : String์์ doubleํํ๋ก ๋ฐ๊พธ๊ธฐ
double dTemp = Double.parseDouble(array[0]);
double dHumd = Double.parseDouble(array[1]);
๊ฐ๋จํ์ฃ ?
์ฌ๋ฌ๋ถ๋ค๋ ์จ์ต๋ ์ผ์๋ง๊ณ ๋ ์ํ๋ ์ผ์๋ฅผ ๋ฌ์ ๋ณด์๋ฉด ์๋นํ ์ฌ๋ฏธ์์ต๋๋ค.
์ ๊ฐ์ ๊ฒฝ์ฐ๋
์์ = (331.5 + (0.6*celsius)) m/s
์๋ณต๊ฑฐ๋ฆฌ = ์์ * ์๊ฐ
๊ฑฐ๋ฆฌ = (์์ * ์๊ฐ)/2
์
๊ณต์์ ์ฌ์ฉํ์ฌ ์จ๋์ ๋ฐ๋ฅธ ์ข ๋ ์ ํํ ์ด์ํ ์ผ์๋ฅผ ๋ง๋ค์ด ๋ณด์์ต๋๋ค.
์ด๊ฒ์ผ๋ก ๋ธ๋ฃจํฌ์ค ๋ชจ๋์ ๊ดํ ๋ ๋ฒ์งธ ๊ธ์ ๋ง์น๋๋ก ํ๊ฒ ์ต๋๋ค.
Copyright (c) 2014 Akexorcist
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
'๐ Embeded SW > Arduino' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
PWM์ผ๋ก ์๋ ๋ก๊ทธ ๊ฐ์ ์ถ๋ ฅํด๋ณด์! 1ํธ (0) | 2020.01.28 |
---|---|
Pull up(ํ์ ) , Pull down(ํ๋ค์ด) ์ ํญ์ ๋ํด ์์๋ณด์! (0) | 2020.01.26 |
์๋์ด๋ ธ, ์๋๋ก์ด๋๋ฅผ ๋ธ๋ฃจํฌ์ค๋ก ์ฐ๊ฒฐํด์ ์ผ์๊ฐ ์ฝ์ด์ค๊ธฐ!(1) (1) | 2020.01.21 |
millis()ํจ์๋ฅผ ํตํด LED ๋์์ ์ผ๋ณด์! (0) | 2020.01.20 |
์๋์ด๋ ธ delay() VS millis() ์ ํํ ์๊ณ ์ฐ์! (2) | 2020.01.19 |