Bluetooth. Wireless networking technology Limited broadcast range (< 10 meters) Example uses include Connecting headsets to phones, keyboard/mouse to.

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
1 Android Introduction Hello Threads. 2 Goal Create an application that uses a background thread as a UDP server to receive messages from the UDP client.
1 Applets Programming Enabling Application Delivery Via the Web.
1 Working with the Bluetooth radio Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
Services. Application component No user interface Two main uses Performing background processing Supporting remote method execution.
SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Inter-Process Communication: Network Programming using TCP Java Sockets Dr. Rajkumar Buyya Cloud Computing and Distributed Systems (CLOUDS) Laboratory.
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
WiFi in Android.
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
專題進度報告 組員:吳家齊、江弘喻.
SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.
1 CMSC628: Introduction to Mobile Computing Nilanjan Banerjee Mobile Systems Programming (Acknowledgment to Deepa Shinde and Cindy Atheron University of.
Cosc 5/4730 Android Bluetooth. Bluetooth Basics First off Bluetooth is NOT – Infrared, which requires line-of-sight – a/b/g/n. It was designed as.
Bluetooth. Bluetooth is an open, wireless protocol for exchanging data between devices over a short distance. –managed by the Bluetooth Special Interest.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Io package as Java’s basic I/O system continue’d.
CS378 - Mobile Computing Connecting Devices. How to pass data between devices? – Chat – Games – Driving Options: – Use the cloud and a service such as.
By: Joel Rodriguez.  International student from Mexico  Delicias, Chihuahua Mexico  Spanish  Sports and Music.
CS378 - Mobile Computing Web - WebView and Web Services.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
CSS216 MOBILE PROGRAMMING Android, Chapter 13 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android.
Java: Chapter 1 Computer Systems Computer Programming II.
Broadcast intents.
LOGO E-Radio code presentation Prepared By Abdullah Mustafa Abu Nada Hussian Abd-Jaleel Shaalan Nael Wael Skaik Software group.
Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1 Mobile Software Development Framework: Android 2/28/2011 Y. Richard Yang.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Data Streams David Meredith Source Chapter 19 of – Shiffman, D. (2008). Learning Processing. Morgan Kaufmann, Burlington, MA. ISBN:
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
@2010 Mihail L. Sichitiu1 Android Introduction Hello Threads.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Presenter: Tyler Newsome
Small talk with the UI thread
Echo Networking COMP
Bluetooth.
Broadcast receivers.
Notifications and Services
Reactive Android Development
Developing Android Services
Wireless Communication between Android Application and Sensors
null, true, and false are also reserved.
Activities and Intents
Android Developer Fundamentals V2 Lesson 5
Service Services.
Objects First with Java
Mobile Programming Dr. Mohsin Ali Memon.
Presentation transcript:

Bluetooth

Wireless networking technology Limited broadcast range (< 10 meters) Example uses include Connecting headsets to phones, keyboard/mouse to desktop computer, medical devices to controllers, etc.

Some key classes: BluetoothAdapter - The local Bluetooth radio BluetoothDevice - A remote Bluetooth device BluetoothServerSocket - An open server socket that listens for incoming requests BluetoothSocket - Communication endpoint

android.permission.BLUETOOTH Allow connections & data transfer android.permission.BLUETOOTH_ADMIN Turn on Bluetooth radio & discover local devices

Set up Bluetooth Find devices already paired or available in the local area Connect devices via a BluetoothSocket Transfer data

Get BluetoothAdapter instance with BluetoothAdapter.getDefaultAdapter(); Returns null if Bluetooth is not available Check whether Bluethooth is enabled with BluetoothAdapter.isEnabled() Returns true if Bluetooth is on; false otherwise

Can use built-in Intent to enable Bluetooth BluetoothAdapter.ACTION_REQUEST_ENABLE Presents users a dialog asking whether they give permission for Bluetooth to be enabled

public class DataTransferActivity extends Activity { … public void onCreate(Bundle savedInstanceState) { … mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // No Bluetooth support finish(); } …

if (!mBluetoothAdapter.isEnabled()) { Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult( enableBluetoothIntent,REQUEST_ENABLE_BT); } …

Paired devices are devices that have already consented to interact with the local device BluetoothAdapter.getBondedDevices(); Can discover new devices with BluetoothAdapter.startDiscovery(); Remote devices must be discoverable Must go through pairing process Can stop discovery process with BluetoothAdapter.cancelDiscovery()

private void selectServer() { Set pairedDevices = mBluetoothAdapter.getBondedDevices(); ArrayList pairedDeviceStrings = new ArrayList (); if (pairedDevices.size() > 0 ) { for (BluetoothDevice device : pairedDevices) { pairedDeviceStrings.add(device.getName() + "\n + device.getAddress()); } Intent showDevicesIntent = new Intent(this, ShowDevices.class); showDevicesIntent.putStringArrayListExtra("devices", pairedDeviceStrings); startActivityForResult(showDevicesIntent, SELECT_SERVER); }

public class ShowDevices extends ListActivity { … protected void onCreate(Bundle savedInstanceState) { … // search for more devices mBluetoothAdapter.startDiscovery(); … // user selects one device BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(/* mac addr String */); Intent data = new Intent(); data.putExtra(BluetoothDevice.EXTRA_DEVICE, device); setResult(RESULT_OK, data); finish(); …

Intent broadcast on device discovery BluetoothDevice.ACTION_FOUND Device info contained in BluetoothDevice instance intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE)

final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // update display } };

Client & server need the same UUID 128-bit Universally Unique Identifier Many freely-available generators exist Client creates

Server creates BluetoothServerSocket with BluetoothAdapter.listenUsingRfcommWithServiceRecord(name, uuid) Listen for incoming connection with BluetoothServerSocket.accept() Returns a BluetoothSocket on connection

class AcceptThread extends Thread { public AcceptThread(Handler handler) { … try { mServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("Bluetooth Demo", DataTransferActivity.APP_UUID); } catch (IOException e) {} } …

public void run() { while (true) { try { mBluetoothSocket = mServerSocket.accept(); manageConnectedSocket(); mServerSocket.close(); break; } catch (IOException e1) { … } } …

Client creates a BluetoothSocket with BluetoothDevice.createRfcommSocketToServiceRecord(uuid) Make connection on BluetoothSocket with BluetoothSocket.connect()

public class ConnectThread extends Thread { … public ConnectThread(String deviceID, Handler handler) { mDevice = mBluetoothAdapter.getRemoteDevice(deviceID); try { mBluetoothSocket = mDevice.createRfcommSocketToServiceRecord(DataTransferActivity.APP_UUID); } catch (IOException e) { … } } …

public void run() { mBluetoothAdapter.cancelDiscovery(); try { mBluetoothSocket.connect(); manageConnectedSocket(); } catch (IOException connectException) { … } }

private void manageConnectedSocket() { ConnectionThread conn = new ConnectionThread(mBluetoothSocket, mHandler); mHandler.obtainMessage( DataTransferActivity.SOCKET_CONNECTED, conn).sendToTarget(); conn.start(); }

public class ConnectionThread extends Thread { … ConnectionThread(BluetoothSocket socket, Handler handler){ super(); mBluetoothSocket = socket; mHandler = handler; try { mInStream = mBluetoothSocket.getInputStream(); mOutStream = mBluetoothSocket.getOutputStream(); } catch (IOException e) { … } } …

public void run() { byte[] buffer = new byte[1024]; int bytes; while (true) { try { bytes = mInStream.read(buffer); String data = new String(buffer, 0, bytes); mHandler.obtainMessage( DataTransferActivity.DATA_RECEIVED,data).sendToTarget(); } catch (IOException e) { break; } } …

public void write(byte[] bytes) { try { mOutStream.write(bytes); } catch (IOException e) { … } }

// Handler in DataTransferActivity public Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SOCKET_CONNECTED: { mBluetoothConnection = (ConnectionThread) msg.obj; if (!mServerMode) mBluetoothConnection.write("this is a message".getBytes()); break; } case DATA_RECEIVED: { data = (String) msg.obj; tv.setText(data); if (mServerMode) mBluetoothConnection.write(data.getBytes()); } …