Está en la página 1de 14

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. This document is distributed under the
Creative Commons Attribution-ShareAlike 2.5 licence, available
in http://creativecommons.org/licenses/by-sa/2.5/

Conectividad Bluetooth Android-Arduino

Objetivos

Comunicacin BT desde Arduino

Comunicacin BT desde Android

Modificar estado de sensores digitales

Leer estado de sensores digitales

Uso del BT con comunicacin serie

Conectividad Bluetooth Android-Arduino

Material (I)

Arduino Nano
Soporte conexin
serie (RX,TX)

LED

Sensor Puerta Abierta

Conectividad Bluetooth Android-Arduino

Smartphone Android

Soporte Bluetooth

Android > 2.3

Material (II)

Mdulo Bluetooth

JY-MCU

VCC, GND, RX y TX

http://www.amazon.com/JY-MCU-Arduino-Bluetooth-Wireless-Serial/dp/B009DZQ4MG

Conectividad Bluetooth Android-Arduino

Arduino

Esquemtico de la placa arduino:

Pin Arduino

Conectividad Bluetooth Android-Arduino

RX0

TXD (BT)

TX1

RXD (BT)

D8

LED

+5V

VCC (BT)

GND

GND (BT)

Arduino - Cdigo
- Inicializamos el puerto seria a
9600 bps por especificacin del
chip de BT

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 == 'R') {
blink();
}
}

Conectividad Bluetooth Android-Arduino

- Comprobamos si est
disponible el puerto serie
- Si est disponible leemos byte
a byte

Android - Cdigo

Aadir permisos Bluetooth al Manifest

<uses-permission android:name="android.permission.BLUETOOTH" />


<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Saber si Android tiene soporte para BT


// Init Bluetooth
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
return;
}

Conectividad Bluetooth Android-Arduino

Android - Cdigo

Habilitar BT si est desactivado. Mostramos


pantalla del sistema para habilitarlo
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

Conectividad Bluetooth Android-Arduino

Android - Cdigo

Utilizamos Receiver para escanear BT


(BluetoothDevice.ACTION_FOUND)
mReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

};

Log.d(TAG, device.getName() + "\n" +


device.getAddress());

Conectividad Bluetooth Android-Arduino

Android - Cdigo

Utilizamos Receiver para detectar fin de scan


(BluetoothDevice.ACTION_DISCOVERY_FINISHED)
mReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
Log.d(TAG, "Scan finished!");
// Show all the devices discovered
}}};

Registrar receiver e intents


IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);

Conectividad Bluetooth Android-Arduino

Android - Cdigo

Conexin Serie: RFCOMM


Enviar y recibir datos a travs de
OutputStream e InputStream

//Standard SerialPortService ID
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
mBSocket = mDevice.createRfcommSocketToServiceRecord(uuid);
mBSocket.connect();
mmOutputStream = mBSocket.getOutputStream();
mmInputStream = mBSocket.getInputStream();

Conectividad Bluetooth Android-Arduino

Android - Cdigo

Enviar datos
public void send (String data)
{
try {
mmOutputStream.write(data.getBytes());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}

Recibir datos
int numBytes = 10;
byte[] b = new byte[numBytes];
try {
mmInputStream.read(b,0,numBytes);
Log.d(TAG, "Recv: " + String.valueOf(b));
}

Conectividad Bluetooth Android-Arduino

Referencias

Documentacin Android Bluetooth [link]


Control Arduino with Your Smartphone or
Tablet [link]
Professional Android 4 Application
Development [link]
Arduino Cookbook [link]

Conectividad Bluetooth Android-Arduino

También podría gustarte