connect.tech- android development for arduino 101

36
Android Development For Arduino 101 By: Bryan Jones Richardson Software Engineer stable|kernel [email protected] stablekernel.com

Upload: stablekernel

Post on 22-Jan-2018

288 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Connect.Tech- Android Development For Arduino 101

Android Development For Arduino 101By: Bryan Jones Richardson Software Engineer stable|[email protected] stablekernel.com

Page 2: Connect.Tech- Android Development For Arduino 101

We’re stable|kernel.

stable|kernel is an Atlanta-based mobile development company to craft smartly-designed mobile applications that connect brands directly with their users.

Page 3: Connect.Tech- Android Development For Arduino 101

The Internet of Things?

@stablekernel

Page 4: Connect.Tech- Android Development For Arduino 101

@stablekernel

The internet of things (IoT) is the network of physical devices, embedded with electronics, software, sensors, actuators, and network connectivity that enable them to collect and exchange data.

• Transportation (Street Lights)• Healthcare• Agriculture• Durables & Wearable• Object Oriented World

Page 5: Connect.Tech- Android Development For Arduino 101

What Is Arduino?

@stablekernel

Page 6: Connect.Tech- Android Development For Arduino 101

@stablekernel

Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's intended for anyone making interactive projects.

(Basically, a small computer that you can connect to anything).• Uno• Mega• Yun• Minimal Cost Barriers• Raspberry Pi, Edison, etc.

Page 7: Connect.Tech- Android Development For Arduino 101
Page 8: Connect.Tech- Android Development For Arduino 101
Page 9: Connect.Tech- Android Development For Arduino 101

Let’s Make Music

@stablekernel

Page 10: Connect.Tech- Android Development For Arduino 101

Arduino Communication Strategies

@stablekernel

•USB•Bluetooth•Network

Page 11: Connect.Tech- Android Development For Arduino 101

Android & Arduino via USB

@stablekernel

Page 12: Connect.Tech- Android Development For Arduino 101

@stablekernel

Android USB Classes• UsbManager• UsbDevice• UsbDeviceConnection• UsbInterface• UsbEndpoint• UsbConstants

Page 13: Connect.Tech- Android Development For Arduino 101

@stablekernel

Detect Android USB Connectionprivate final BroadcastReceiver usbConnectionAttachedReceiver = new BroadcastReceiver() { @Override public void onReceive(@NonNull Context context, @NonNull Intent intent) { activeUsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); connectUsb(); }};

private final BroadcastReceiver usbConnectionDetachedReceiver = new BroadcastReceiver() { @Override public void onReceive(@NonNull Context context, @NonNull Intent intent) { UsbDevice detachedDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (detachedDevice == activeUsbDevice) { disconnectUsb(); } }};

Page 14: Connect.Tech- Android Development For Arduino 101

@stablekernel

Determine if Arduino is Connected for (int i = 0; i < activeUsbDevice.getInterfaceCount(); i++) { UsbInterface usbInterface = activeUsbDevice.getInterface(i);

if (usbInterface.getEndpointCount() == 2) { for (int j = 0; j < endpointCount; j++) { UsbEndpoint endpoint = usbInterface.getEndpoint(j); if (endpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) { continue;

} if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) { endpointOut = usbInterface.getEndpoint(j); } else if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) { endpointIn = usbInterface.getEndpoint(j); } } } } }

Page 15: Connect.Tech- Android Development For Arduino 101

@stablekernel

Establish Communication w/ Arduinoprivate void setupUSBComm() { UsbManager manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE); Boolean permitToRead = manager.hasPermission(arduinoDevice);

if (permitToRead) { connection = manager.openDevice(arduinoDevice); if (connection != null) { connection.claimInterface(activeUSBInterface, true);

//requestType, SET_CONTROL_LINE_STATE, value, index, buffer, length, timeout connection.controlTransfer(RQSID_SET_REQUEST_TYPE, RQSID_SET_CONTROL_LINE_STATE, 0, 0, null, 0, 0); connection.controlTransfer(RQSID_SET_REQUEST_TYPE, RQSID_SET_LINE_CODING, 0, 0, encodingSetting, 7, 0); } } else { manager.requestPermission(arduinoDevice, permissionIntent); }}

Page 16: Connect.Tech- Android Development For Arduino 101

@stablekernel

Send Android Data to Arduino

@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) { playNote(position);}

public void playNote(int note_value) { byte[] bytesOut = new byte[]{ (byte) note_value};

if(arduinoDevice != null) { usbArduinoConnection.bulkTransfer(endpointOut, bytesOut, 1, 0); }}

Page 17: Connect.Tech- Android Development For Arduino 101

@stablekernel

Process Android Data on Arduinoint speakerPin = 9;int tones[] = { 440, 494, 523, 587, 659, 698, 784};

void setup() { pinMode(speakerPin, OUTPUT); Serial.begin(9600);}

void loop() { if (Serial.available()) { int incomingByte = Serial.read();

if (incomingByte > -1) { playNote(incomingByte); } }}

void playNote(int note) { tone(speakerPin, tones[note], 200);}

Page 18: Connect.Tech- Android Development For Arduino 101

@stablekernel

Hardware Setup

Ground Pin 9

Piezo BreadBoardUno USB

Page 19: Connect.Tech- Android Development For Arduino 101

Android & Arduino via Bluetooth

@stablekernel

Page 20: Connect.Tech- Android Development For Arduino 101

@stablekernel

Android Bluetooth Classes

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

private BluetoothAdapter bluetoothAdapter;private BluetoothSocket bluetoothSocket;private Set<BluetoothDevice> pairedBluetoothDevices;

static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

Page 21: Connect.Tech- Android Development For Arduino 101

@stablekernel

Connect To BlueTooth Device

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();BluetoothDevice btDevice = bluetoothAdapter.getRemoteDevice(bluetoothAddress);btSocket = btDevice.createInsecureRfcommSocketToServiceRecord(myUUID);BluetoothAdapter.getDefaultAdapter().cancelDiscovery();btSocket.connect();

Page 22: Connect.Tech- Android Development For Arduino 101

@stablekernel

Send Android Data to Arduino

byte[] bytesOut = new byte[]{ note_value };

try { btSocket.getOutputStream().write(bytesOut);} catch (IOException e) { // handle exception}

Page 23: Connect.Tech- Android Development For Arduino 101

@stablekernel

Debugging on Arduino

Page 24: Connect.Tech- Android Development For Arduino 101

@stablekernel

Debugging on Arduino

digitalWrite(ledPin, LOW);

digitalWrite(ledPin, HIGH);

serialPrint(…);

Page 25: Connect.Tech- Android Development For Arduino 101

@stablekernel

Hardware Setup

BluetoothTransmitter

LED

Page 26: Connect.Tech- Android Development For Arduino 101

Android & Arduino via Network

@stablekernel

Page 27: Connect.Tech- Android Development For Arduino 101

@stablekernel

Configure Yun For Connectivity

Page 28: Connect.Tech- Android Development For Arduino 101

@stablekernel

Configure Yun For Connectivity

Page 29: Connect.Tech- Android Development For Arduino 101

@stablekernel

Configure Yun For Connectivity

Page 30: Connect.Tech- Android Development For Arduino 101

@stablekernel

Implement Arduino Code

#include <Bridge.h>#include <YunServer.h>#include <YunClient.h>

YunServer server;...

Page 31: Connect.Tech- Android Development For Arduino 101

@stablekernel

Implement Arduino Code

void setup() { ...

Bridge.begin(); server.listenOnLocalhost(); server.begin();}

Page 32: Connect.Tech- Android Development For Arduino 101

@stablekernel

Implement Arduino Code

void loop() { YunClient client = server.accept();

if (client) { process(client); client.stop(); }

delay(50);}

void process(YunClient client) { String incomingNote = client.readStringUntil('/'); playNote(incomingNote.toInt());}

Page 33: Connect.Tech- Android Development For Arduino 101

@stablekernel

Send Android Data to Arduino Yun @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState);

String[] musicalNotes = new String[] { "A","B","C","D","E","F","G"};

ArrayAdapter<String> musicalNotesAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, musicalNotes); notesListView.setOnItemClickListener(this); notesListView.setAdapter(musicalNotesAdapter); }

@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String note = String.valueOf(position);

HttpClient client = new DefaultHttpClient(); String URL = “http://10.0.0.19/arduino/“ + note; HttpGet request = new HttpGet(URL); ResponseHandler<String> responseHandler = new BasicResponseHandler();

client.execute(httpget, responseHandler); }

Page 34: Connect.Tech- Android Development For Arduino 101

@stablekernel

Hardware Setup

Page 35: Connect.Tech- Android Development For Arduino 101

Live Demo

@stablekernel

Page 36: Connect.Tech- Android Development For Arduino 101

Questions?

Business Inquiries:Sarah WoodwardDirector of Business [email protected]

Bryan Jones RichardsonSoftware [email protected]