Vous êtes sur la page 1sur 14

Conectividad Bluetooth Android-Arduino

Curso de Desarrollo en Android


Conectividad Bluetooth
Android-Arduino
Roberto Calvo Palomino
rocapal@gmail.com
@rocapal
Conectividad Bluetooth Android-Arduino
(cc) 2013 Roberto Calvo Palomino.
Some rights reserved. his document is distributed under the
Creative Commons Attribution!ShareAli"e 2.# licence$ available
in htt%&''creativecommons.org'licenses'b(!sa'2.#'
Conectividad Bluetooth Android-Arduino
)b*etivos

Comunicaci+n , desde Arduino

Comunicaci+n , desde Android

-odi.icar estado de sensores digitales

/eer estado de sensores digitales

0so del , con comunicaci+n serie


Conectividad Bluetooth Android-Arduino
-aterial (1)

Arduino 2ano

So%orte cone3i+n
serie (R4$4)

/5D

Sensor Puerta Abierta

Smart%hone Android

So%orte ,luetooth

Android 6 2.3
Conectividad Bluetooth Android-Arduino
-aterial (11)

-+dulo ,luetooth

78!-C0

9CC$ :2D$ R4 ( 4
htt%&'';;;.ama<on.com'78!-C0!Arduino!,luetooth!=ireless!Serial'd%',00>D?@A-:
Conectividad Bluetooth Android-Arduino
Arduino

5sBuemCtico de la %laca arduino&


Pin Arduino
R40 4D (,)
41 R4D (,)
DD /5D
E#9 9CC (,)
:2D :2D (,)
Conectividad Bluetooth Android-Arduino
Arduino ! C+digo
void setup(){
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0 ){
// read the oldest byte in the
// serial buffer:
int incomingByte Serial.read()!

if (incomingByte "#") {
blin$()!
}
}
! 1niciali<amos el %uerto seria a
>F00 b%s %or es%eci.icaci+n del
chi% de ,
! Com%robamos si estC
dis%onible el %uerto serie
! Si estC dis%onible leemos b(te
a b(te
Conectividad Bluetooth Android-Arduino
Android ! C+digo

AGadir %ermisos ,luetooth al -ani.est

Saber si Android tiene so%orte %ara ,


%uses&permission android:name"android.permission.BLUETOOTH" />
%uses&permission android:name"android.permission.BLUETOOTH_ADMIN"/>
// 'nit Bluetooth
mBluetooth(dapter Bluetooth(dapter)getDefaultAdapter()!
if (mBluetooth(dapter null) {
// *evice does not support Bluetooth
return!
}
Conectividad Bluetooth Android-Arduino
Android ! C+digo

Habilitar , si estC desactivado. -ostramos


%antalla del sistema %ara habilitarlo
if (+mBluetooth(dapter)is,nabled()) {
'ntent enableBt'ntent new
'ntent(Bluetooth(dapter)ACTION_E!UE"T_ENABLE)!
start(ctivity-or#esult(enableBt'ntent. #,/0,123,4(B5,3B2)!
}
Conectividad Bluetooth Android-Arduino
Android ! C+digo

0tili<amos Receiver %ara escanear ,


(BluetoothDevice.ACTION_FOUND)
m#eceiver new Broadcast#eceiver(){
public void on#eceive(6onte7t conte7t. 'ntent intent) {
1tring action intent)get(ction()!
if (Bluetooth*evice)ACTION_#OUND)e8uals(action))
{
// 9et the Bluetooth*evice ob:ect from the 'ntent
Bluetooth*evice device
intent)get;arcelable,7tra(Bluetooth*evice)E$TA_DE%ICE)!
5og)d(2(9. device)get4ame() < =>n= <
device)get(ddress())!
}
}
}!
Conectividad Bluetooth Android-Arduino
Android ! C+digo

0tili<amos Receiver %ara detectar .in de scan


(BluetoothDevice.ACTION_DISCOVERY_FINISHED)
m#eceiver new Broadcast#eceiver(){
public void on#eceive(6onte7t conte7t. 'ntent intent) {
1tring action intent)get(ction()!
if (Bluetooth(dapter)ACTION_DI"CO%E&_#INI"HED)e8uals(action))
{
5og)d(2(9. =1can finished+=)!
// 1how all the devices discovered
}}}!
'ntent-ilter filter new 'ntent-ilter()!
filter)add(ction(Bluetooth*evice)ACTION_#OUND)!
filter)add(ction(Bluetooth(dapter)ACTION_DI"CO%E&_#INI"HED)!
register#eceiver(m#eceiver. filter)!

Registrar receiver e intents


Conectividad Bluetooth Android-Arduino
Android ! C+digo

Cone3i+n Serie& RIC)--

5nviar ( recibir datos a travJs de


)ut%utStream e 1n%utStream
//1tandard 1erial;ort1ervice '*
00'* uuid 00'*)from"tring(=0000??0?&0000&?000&@000&00@0AfBbCDfb=)!
mB1oc$et m*evice)create#fcomm1oc$et2o1ervice#ecord(uuid)!
mB1oc$et)connect()!
mmEutput1tream mB1oc$et)getEutput1tream()!
mm'nput1tream mB1oc$et)get'nput1tream()!
Conectividad Bluetooth Android-Arduino
Android ! C+digo

5nviar datos

Recibir datos
public void send (1tring data)
{
try {
mmEutput1tream)write(data)getBytes())!
} catch ('E,7ception e) {
5og)e(2(9. e)getFessage())!
}
}
int numBytes 10!
byteGH b new byteGnumBytesH!
try {
mm'nput1tream)read(b.0.numBytes)!
5og)d(2(9. =#ecv: = < 1tring)'alueOf(b))!
}
Conectividad Bluetooth Android-Arduino
Re.erencias

Documentaci+n Android ,luetooth Klin"L

Control Arduino ;ith 8our Smart%hone or


ablet Klin"L

Pro.essional Android A A%%lication


Develo%ment Klin"L

Arduino Coo"boo" Klin"L

Vous aimerez peut-être aussi