336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

http://www.instructables.com/id/Android-Bluetooth-Control-LED-Part-2/?ALLSTEPS


LED Control Using Android.rar


This is a step-by-step tutorial for making an android apk 

using bluetooth.

Before start coding,

이건 step-by-step 튜토리얼이다. for 만들기위한 an 안드로이드 apk를

사용하며 블루투스를.

before 시작하하기전에 코딩을,

Download Android Studio IDE and update Java.

●Java and C programming skills will help.

●This tutorial will not explain Java Programming.

If you want to code using Eclipse IDE, it is almost the same.

The apk will send commands to turn on/turn off a LED and controls the brightness.

●You can download everything in the last step.


Step 1: Android Studio: New Project

●Open Android Studio and create a new Project: File > New Project.

새 프로젝트를 생성한다.

A pop up Windows will appear. Change the Application Name and Company Name.

팝업창이 나타나면 바꿔준다. Application Name과 Company Name을.

Click next to choose the target of the application. The default is Android 4.0 (IceCream Sandwich)

타겟 디바이스의 OS를 선택한다. Default는 IceCream Sandwitch이다.

Click next and choose a Blank Activity.

선택한다. Blank Activity를.

Click next and rename the Activity Name to “DeviceList”.

클릭한다. Next를 and 이름을 재설정한다. DeviceList라고. 

Now click “finish” and the Project will be create.




Step 2: Android: Layout Part 1.

When the build is finished, a “Hello world!” screen will be open. 

To create the layout of the apk, 

we need to add:

구축이 끝났으면, Hello world 스크린이 열리게 될것이다.

To 만들기위해 the 레이아웃을 of the apk의,

우린 추가할 필요가 있다.(아래의 것들을)

TextView to display some hint to the user;

Click twice the TextView to change the text.

텍스트뷰 to 나타내기위해 some 힌트를 to the 유저에게

클릭해라. 두번 the TextView를 to 바꾸기위해 the 텍스트를.

A box will appear:
Text = The text to be displayed.
Id = the id of this widget


Button to show the paired devices.

버튼 to 보여주기위해 the 짝이 맞는 디바이스들을.

Click twice the Button to change the text. 

A box will appear:
Text = The text to be displayed.
Id = the id of this widget.

ListView to show the paired devices;

Now the main activity is finished, 

you can see that all widget used are shown 

on Components Tree.

이제 메인 activity가 끝났다,

넌 can 볼수있다. that 모든 사용된 위젯은 보인다. 

on Components Tree상에서



Step 3: Android: .Class Code Part 1.

On the left side there’s a folder called “app “, 

open it and you’ll see other folder called “java”.

왼쪽편에 there's a 폴더가 있다. 불리는 "app"이라고,

열어라. 그걸 and 넌 볼것이다. 다른 폴더를 불리는 "java"라고.
Java folder contains the package of the apk (com.led.led), 

and all the source code.

Java 폴더는 포함한다. the 패키지를 of the apk의

and 모든 소스코드를.

Open DeviceList class;


Import the followings packages:

import android.widget.Button;
import android.widget.ListView;

Create widgets variables to “call” the widgets used to create the layout:

Button btnPaired;
ListView devicelist;

Initialize the variables.

btnPaired = (Button)findViewById(R.id.button);
devicelist = (ListView)findViewById(R.id.listView);

Import the following packages:

import java.util.Set;
import java.util.ArrayList;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import android.widget.AdapterView
import android.widget.AdapterView.OnClickListener
import android.widget.TextView;
import android.content.Intent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

Create variables to control bluetooth:

private BluetoothAdapter myBluetooth = null;
private Set pairedDevices;

Writing a stable code avoids weird erros, so it’s good to check if the device has bluetooth adapter and whether it’s activated.

myBluetooth = BluetoothAdapter.getDefaultAdapter();
if(myBluetooth == null)
{
//Show a mensag. that thedevice has no bluetooth adapter
Toast.makeText(getApplicationContext(), "Bluetooth Device Not Available", Toast.LENGTH_LONG).show();
//finish apk
finish();
} else
{
if (myBluetooth.isEnabled())
{ }
else
{ //Ask to the user turn the bluetooth on Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnBTon,1);
}
}

According to Android documents, an Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use-cases:

  • To start an activity:

An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.

  • To start a service:

A Service is a component that performs operations in the background without a user interface. You can start a service to perform a one-time operation (such as download a file) by passing an Intent to startService(). The Intent describes the service to start and carries any necessary data.

  • To deliver a broadcast:

A broadcast is a message that any app can receive. The system delivers various broadcasts for system events, such as when the system boots up or the device starts charging. You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

We need to “listen” when the button is clicked to show paired devices. So OnClickListener Api will handle it



.

.

.


Step 8: Aruino Code

The arduino C code is very simple, no need to explain it:

char command;

String string;

boolean ledon = false;

#define led 5

char command;

String string;

boolean ledon = false;

#define led 5

void setup()

{

Serial.begin(9600);

pinMode(led, OUTPUT);

}

void loop()

{

if (Serial.available() > 0)

{string = "";}

while(Serial.available() > 0)

{command = ((byte)Serial.read());

if(command == ':')

{

break;

}

else

{

string += command;

}

delay(1);

}

if(string == "TO")

{

ledOn();

ledon = true;

}

if(string =="TF")

{

ledOff();

ledon = false;

Serial.println(string); //debug

}

if ((string.toInt()>=0)&&(string.toInt()<=255))

{

if (ledon==true)

{

analogWrite(led, string.toInt());

Serial.println(string); //debug

delay(10);

}

}

}

void ledOn()

{

analogWrite(led, 255);

delay(10);

}

void ledOff()

{

analogWrite(led, 0);

delay(10);

}

Posted by ElectricShock
:
BLOG main image
잡동사니들(지극히 개인취향인...) (다른글에도 댓글 부탁해요♥) You May Leave English Messages on GuestBook. by ElectricShock

공지사항

카테고리

분류 전체보기 (782)
Programming(=프로그래밍) (3)
MiDi (2)
Animation (4)
Blender (3D Graphic Program.. (10)
Blendtuts.com (Series) (1)
Blender 기초 팁들 (2)
Processing (디지털미디어과) (2)
Music (1)
Books in the world (0)
Communication(CAN, UART, et.. (12)
MCU Examples (PIC 기반) (7)
Transistor (1)
Mikro C Pro (11)
Mikro Pascal (1)
Proton IDE (0)
Robot (0)
Swift 3D (1)
Dummies Series (1)
All about Hacking (0)
제2 외국어 (1)
PIC 해외서적들 (3)
AVR (25)
PIC (MikroC) (MPLAB) (4)
Assembly (2)
ARM (3)
Arduino (26)
PSpice (1)
Proteus ISIS (14)
CodeVision (2)
FPGA (15)
MPLAB (24)
PCB (the Procedure) (15)
3D Printer (5)
PICKIT3 (6)
Matlab (11)
RaspBerry PI (15)
BeagleBone (1)
Android Studio (17)
졸업작품 (172)
Korea History (0)
Issue(사회) (73)
Multimeter 리뷰 (1)
Oscilloscope (1)
A (34)
B (19)
J (6)
C (32)
P (12)
T (37)
H (12)
I (12)
M (44)
R (5)
E (5)
F (2)
D (9)
O (2)
L (7)
S (9)
W (2)
V (6)
G (14)
Visual C++ or Visual Studio (2)
Android App Development (0)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백