아두이노 블루투스 모듈을 이용하여 스마트폰과 연결하여 아두이노에 연결된 LED 제어


activity_main.xml

------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>

<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">



<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/entry"
android:layout_centerHorizontal="true"
android:text="Send" />
</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="25dp"
android:text=""
android:id="@+id/recv" />

</LinearLayout>


</LinearLayout>


--------------------------------------------------------------------------------------------------





AndroidManifest.xml

-------------------------------------------------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kimeunchan.lastlast">

<uses-permission android:name="android.permission.BLUETOOTH" />        // 두줄 추가 !
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />        // 두줄 추가 !

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


---------------------------------------------------------------------------------


MainActivity

-----------------------------------------------------------------------------------------


package com.example.kimeunchan.lastlast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{
private final static int DEVICES_DIALOG = 1;
private final static int ERROR_DIALOG = 2;

public static Context mContext;
public static AppCompatActivity activity;

TextView myLabel, mRecv;
EditText myTextbox;
static BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Button sendButton = (Button)findViewById(R.id.send);
myLabel = (TextView)findViewById(R.id.label);
myTextbox = (EditText)findViewById(R.id.entry);
mRecv = (TextView)findViewById(R.id.recv);

mContext = this;
activity=this;

//1.블루투스 사용 가능한지 검사합니다.
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
ErrorDialog("This device is not implement Bluetooth.");
return;
}

if (!mBluetoothAdapter.isEnabled()) {
ErrorDialog("This device is disabled Bluetooth.");
return;
}
else
//2. 페어링 되어 있는 블루투스 장치들의 목록을 보여줍니다.
//3. 목록에서 블루투스 장치를 선택하면 선택한 디바이스를 인자로 하여 doConnect 함수가 호출됩니다.
DeviceDialog();


//11. Send 버튼을 누르면 sendData함수가 호출됩니다.
sendButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
sendData();
}
catch (IOException ex) { }
}
});

}

static public Set<BluetoothDevice> getPairedDevices() {
return mBluetoothAdapter.getBondedDevices();
}

@Override
public void onBackPressed() {
doClose();
super.onBackPressed();
}


//13. 백버튼이 눌러지거나, ConnectTask에서 예외발생시
//데이터 수신을 위한 스레드를 종료시키고 CloseTask를 실행하여 입출력 스트림을 닫고,
//소켓을 닫아 통신을 종료합니다.
public void doClose() {
workerThread.interrupt();
new CloseTask().execute();
}



public void doConnect(BluetoothDevice device) {
mmDevice = device;

//Standard SerialPortService ID
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

try {
// 4. 지정한 블루투스 장치에 대한 특정 UUID 서비스를 하기 위한 소켓을 생성합니다.
// 여기선 시리얼 통신을 위한 UUID를 지정하고 있습니다.
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
// 5. 블루투스 장치 검색을 중단합니다.
mBluetoothAdapter.cancelDiscovery();
// 6. ConnectTask를 시작합니다.
new ConnectTask().execute();
} catch (IOException e) {
Log.e("", e.toString(), e);
ErrorDialog("doConnect "+e.toString());
}
}

private class ConnectTask extends AsyncTask<Void, Void, Object> {
@Override
protected void onPreExecute() {

}

@Override
protected Object doInBackground(Void... params) {
try {
//7. 블루투스 장치로 연결을 시도합니다.
mmSocket.connect();

//8. 소켓에 대한 입출력 스트림을 가져옵니다.
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();

//9. 데이터 수신을 대기하기 위한 스레드를 생성하여 입력스트림로부터의 데이터를 대기하다가
// 들어오기 시작하면 버퍼에 저장합니다.
// '\n' 문자가 들어오면 지금까지 버퍼에 저장한 데이터를 UI에 출력하기 위해 핸들러를 사용합니다.
beginListenForData();


} catch (Throwable t) {
Log.e( "", "connect? "+ t.getMessage() );
doClose();
return t;
}
return null;
}


@Override
protected void onPostExecute(Object result) {
//10. 블루투스 통신이 연결되었음을 화면에 출력합니다.
myLabel.setText("Bluetooth Opened");
if (result instanceof Throwable)
{
Log.d("","ConnectTask "+result.toString() );
ErrorDialog("ConnectTask "+result.toString());

}
}
}
private class CloseTask extends AsyncTask<Void, Void, Object> {
@Override
protected Object doInBackground(Void... params) {
try {
try{mmOutputStream.close();}catch(Throwable t){/*ignore*/}
try{mmInputStream.close();}catch(Throwable t){/*ignore*/}
mmSocket.close();
} catch (Throwable t) {
return t;
}
return null;
}

@Override
protected void onPostExecute(Object result) {
if (result instanceof Throwable) {
Log.e("",result.toString(),(Throwable)result);
ErrorDialog(result.toString());
}
}
}



public void DeviceDialog()
{
if (activity.isFinishing()) return;

FragmentManager fm = MainActivity.this.getSupportFragmentManager();
MyDialogFragment alertDialog = MyDialogFragment.newInstance(DEVICES_DIALOG, "");
alertDialog.show(fm, "");
}



public void ErrorDialog(String text)
{
if (activity.isFinishing()) return;

FragmentManager fm = MainActivity.this.getSupportFragmentManager();
MyDialogFragment alertDialog = MyDialogFragment.newInstance(ERROR_DIALOG, text);
alertDialog.show(fm, "");
}


void beginListenForData()
{
final Handler handler = new Handler(Looper.getMainLooper());

stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == '\n')
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");

readBufferPosition = 0;

handler.post(new Runnable()
{
public void run()
{
mRecv.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});

workerThread.start();
}

//12. UI에 입력된 문자열이 있다면 출력 스트림에 기록하고
//화면에 "Data Sent"를 출력해줍니다.
void sendData() throws IOException
{
String msg = myTextbox.getText().toString();
if ( msg.length() == 0 ) return;

msg += "\n";
Log.d(msg, msg);
mmOutputStream.write(msg.getBytes());
myLabel.setText("Data Sent");
myTextbox.setText(" ");
}
}

-------------------------------------------------------------------------------



MaDialogFragment


-------------------------------------------------------------------------------

package com.example.kimeunchan.lastlast;

import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import java.util.Set;

public class MyDialogFragment extends DialogFragment {
private final static int DEVICES_DIALOG = 1;
private final static int ERROR_DIALOG = 2;



public MyDialogFragment() {

}

public static MyDialogFragment newInstance(int id, String text) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("content", text);
args.putInt("id", id);

frag.setArguments(args);
return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String content = getArguments().getString("content");
int id = getArguments().getInt("id");
AlertDialog.Builder alertDialogBuilder = null;

switch(id)
{
case DEVICES_DIALOG:
alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("Select device");

Set<BluetoothDevice> pairedDevices = MainActivity.getPairedDevices();
final BluetoothDevice[] devices = pairedDevices.toArray(new BluetoothDevice[0]);
String[] items = new String[devices.length];
for (int i=0;i<devices.length;i++) {
items[i] = devices[i].getName();
}

alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
((MainActivity)MainActivity.mContext).doConnect(devices[which]);
}
});
alertDialogBuilder.setCancelable(false);
break;


case ERROR_DIALOG:
alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("ERROR");
alertDialogBuilder.setMessage(content);
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
((MainActivity)MainActivity.mContext).finish();
}
});
break;


/* alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
getActivity().finish();
}
});
*/

}

return alertDialogBuilder.create();
}
}


----------------------------------------------------------------------------------


아두이노 소스


----------------------------------------------------------------------------------



#include <SoftwareSerial.h> SoftwareSerial swSerial(2, 3); // rx,tx int Led=13; int ledStatus = LOW; void setup() { swSerial.begin(9600); pinMode(Led, OUTPUT); } void loop() { char ch=' '; digitalWrite( Led, ledStatus ); if ( swSerial.available() > 0 ) { ch = swSerial.read(); delay(100); if (ch=='2') { swSerial.println("2 Turn Off Led"); ledStatus = LOW; digitalWrite(13, LOW); } else if (ch=='1') { swSerial.println("1 Turn On Led"); ledStatus = HIGH; digitalWrite(13, HIGH); } } }


----------------------------------------------------------


+ Recent posts