CSS216 MOBILE PROGRAMMING Android, Chapter 13 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (

Slides:



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

1 Working with the Bluetooth radio Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
CIMCO Integration Software Products
Programming with Android: System Services Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
WiFi in Android.
ASP.NET Best Practices Dawit Wubshet Park University.
CCNA – Network Fundamentals
1 Semester 2 Module 4 Learning about Other Devices Yuda college of business James Chen
Wireless Networking with Android Uichin Lee KAIST KSE.
Introduction to push technology © 2009 Research In Motion Limited.
WAWC’05 Enhancing Mobile Peer-To-Peer Environment with Neighborhood Information Arto Hämäläinen -
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
A Study of Mobile IP Kunal Ganguly Wichita State University CS843 – Distributed Computing.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment Chapter 8: Implementing and Managing Printers.
WXES2106 Network Technology Semester /2005 Chapter 8 Intermediate TCP CCNA2: Module 10.
(part 3).  Switches, also known as switching hubs, have become an increasingly important part of our networking today, because when working with hubs,
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.
 An electrical device that sends or receives radio or television signals through electromagnetic waves.
Bluetooth. Bluetooth is an open, wireless protocol for exchanging data between devices over a short distance. –managed by the Bluetooth Special Interest.
1 Chapter Overview Creating Sites and Subnets Configuring Intersite Replication Troubleshooting Active Directory Replication.
Wireless Networking 102.
Week #10 Objectives: Remote Access and Mobile Computing Configure Mobile Computer and Device Settings Configure Remote Desktop and Remote Assistance for.
CS378 - Mobile Computing Connecting Devices. How to pass data between devices? – Chat – Games – Driving Options: – Use the cloud and a service such as.
2011 Embedded Systems Software Training Center BluRapport SDK.
Hacking the Bluetooth Pairing Authentication Process Graduate Operating System Mini Project Siyuan Jiang and Haipeng Cai.
Intro to Android Programming George Nychis Srinivasan Seshan.
Name Resolution Domain Name System.
Mobile Application Development Selected Topics – CPIT 490
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Bluetooth Kirthika Parmeswaran Telcordia Technologies OOPSLA’2000.
Objectives Configure routing in Windows Server 2008 Configure Routing and Remote Access Services in Windows Server 2008 Network Address Translation 1.
Chapter 7 Low-Level Protocols
© 2009 Research In Motion Limited Advanced Java Application Development for the BlackBerry Smartphone Trainer name Date.
CMPT 471 Networking II Address Resolution IPv4 ARP RARP 1© Janice Regan, 2012.
1. Insert the Resource CD into your CD-ROM drive, click Start and choose Run. In the field that appears, enter F:\XXX\Setup.exe (if “F” is the letter of.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
10/15/ Short-Range Radio Frequency Networking B. Ramamuthy.
Hands-On Microsoft Windows Server Introduction to Remote Access Routing and Remote Access Services (RRAS) –Enable routing and remote access through.
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.
BLUETOOTH Created By: Ajay Bahoriya. Agenda Introduction to Bluetooth Bluetooth Basics Mode of operation Technology Security Advantages Integrating BT.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Computer Networking From LANs to WANs: Hardware, Software, and Security Chapter 13 FTP and Telnet.
® IBM Software Group © 2007 IBM Corporation Best Practices for Session Management
Bluetooth Techniques Chapter 15. Overview of Bluetooth Initially developed by Swedish mobile phone maker in 1994 to let laptop computers make calls over.
1 © 2005 Cisco Systems, Inc. All rights reserved. 111 © 2004, Cisco Systems, Inc. All rights reserved. CNIT 221 Security 2 ver.2 Module 8 City College.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
FCM Workflow using GCM.
JPMA PROJECT PRESENTATION (ITU) 1 INTRODUCTION BLUETOOTH CHAT DESIGN JAVA ON MOBILE DEVICE Presenters Benjamin Boateng Abass Omer Venkateshwar Rao Namilakonda.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
11 WORKING WITH ACTIVE DIRECTORY SITES Chapter 3.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Instructor Materials Chapter 6 Building a Home Network
Bluetooth.
Wireless Technologies
Always Connected introduces Tigo SMART 3.0 This guide is interactive!
NETWORK PROGRAMMING CNET 441
Chapter 3: Windows7 Part 4.
Wireless Communication between Android Application and Sensors
BLUETOOTH Personal Area Networking [ PAN ] over Bluetooth
Always Connected introduces Tigo SMART 3.1 This guide is interactive!
Allocating IP Addressing by Using Dynamic Host Configuration Protocol
Short-Range Radio Frequency Networking
Short-Range Radio Frequency Networking
Network Discovery and Service Discovery
Mobile Programming Dr. Mohsin Ali Memon.
What’s New In WatchGuard Wi-Fi Cloud v8.6
Presentation transcript:

CSS216 MOBILE PROGRAMMING Android, Chapter 13 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov ( )

Outline - Bluetooth, Networks, and Wi-Fi Managing Bluetooth devices Discovering remote Bluetooth devices Managing discovery mode Communicating over Bluetooth Monitoring Internet connectivity Obeying user preferences for background data transfer Monitoring Wi-Fi and network details Configuring networks and Wi-Fi configurations Scanning for Wi-Fi access points

Introduction In this chapter you’ll continue to explore Android’s low-level communications APIs by examining the Bluetooth, network, and Wi-Fi packages. Android offers APIs to manage and monitor your Bluetooth device settings, to control discoverability, to discover nearby Bluetooth devices, and to use Bluetooth as a proximity- based peer-to-peer transport layer for your applications. A full network and Wi-Fi package is also available. Using these APIs you can scan for hotspots, create and modify Wi- Fi configuration settings, monitor your Internet connectivity, and control and monitor Internet settings and preferences.

Using Bluetooth In this section you’ll learn how to interact with the local Bluetooth device and communicate with remote devices on nearby phones. Using Bluetooth you can search for, and connect to, other devices within range. By initiating a communications link using Bluetooth Sockets you can then transmit and receive streams of data between devices from within your applications.

Bluetooth is a communications protocol designed for short- range, low-bandwidth peer-to-peer communications. As of Android 2.1, only encrypted communication is supported, meaning you can only form connections between paired devices. In Android, Bluetooth devices and connections are handled by the following classes: ➤ BluetoothAdapter The Bluetooth Adapter represents the local Bluetooth device—that is, the Android device on which your application is running. ➤ BluetoothDevice Each remote device with which you wish to communicate is represented as a BluetoothDevice. ➤ BluetoothSocket Call createRfcommSocketToServiceRecord on a remote Bluetooth Device object to create a Bluetooth Socket that will let you make a connection request to the remote device, and then initiate communications. ➤ BluetoothServerSocket By creating a Bluetooth Server Socket (using the listenUsingRfcommWithServiceRecord method) on your local Bluetooth Adapter, you can listen for incoming connection requests from Bluetooth Sockets on remote devices.

Accessing the Local Bluetooth Device Adapter The local Bluetooth device is controlled via the BluetoothAdapter class. To access the default Bluetooth adapter on the host device call getDefaultAdapter. It is possible that some Android devices will feature multiple Bluetooth adapters, though it is currently only possible to access the default device. BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); To read any of the local Bluetooth Adapter properties, initiate discovery, or find bonded devices you will need to include the BLUETOOTH manifest permission. In order to modify any of the local device properties the BLUETOOTH_ADMIN uses-permission is also required.

Managing Bluetooth Properties and State The Bluetooth Adapter offers methods for reading and setting properties of the local Bluetooth hardware. The Bluetooth Adapter properties can be read and changed only if the Bluetooth adapter is currently turned on (that is, if its device state is enabled). If the device is off, these methods will return null. If the Bluetooth Adapter is turned on, and you have included the BLUETOOTH permission in your manifest, you can access the Bluetooth Adapter’s friendly name (an arbitrary string that users can set and then use to identify a particular device) and hardware address.

Reading Bluetooth Adapter properties BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); String toastText; if (bluetooth.isEnabled()) { String address = bluetooth.getAddress(); String name = bluetooth.getName(); toastText = name + " : " + address; }else toastText = "Bluetooth is not enabled"; Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();

States If you also have the BLUETOOTH_ADMIN permission you can change the friendly name of the Bluetooth Adapter using the setName method:bluetooth.setName("Blackfang"); To find a more detailed description of the current Bluetooth Adapter state, use the getState method, which will return one of the following BluetoothAdapter constants: ➤ STATE_TURNING_ON ➤ STATE_ON ➤ STATE_TURNING_OFF ➤ STATE_OFF

Enable Bluetooth By default the Bluetooth adapter will be turned off. In order to conserve battery life and optimize security, most users will keep Bluetooth disabled unless it’s in use. To enable the Bluetooth Adapter you can start a system sub- Activity using the ACTION_REQUEST_ENABLE Bluetooth Adapter static constant as a startActivityForResult action string: String enableBT = BluetoothAdapter.ACTION_REQUEST_ENABLE; startActivityForResult(new Intent(enableBT), 0);

It is also possible to turn the Bluetooth Adapter on and off directly, using the enable and disable methods, if you include the BLUETOOTH_ADMIN permission in your manifest. Note that this should be done only when absolutely necessary and that the user should always be notified if you are manually changing the Bluetooth Adapter status on the user’s behalf. In most cases you should use the Intent mechanism described earlier.

Being Discoverable and Remote Device Discovery The process of two devices finding each other in order to connect is called discovery. Before you can establish a Bluetooth Socket for communications, the local Bluetooth Adapter must bond with the remote device. Before two devices can bond and connect, they first need to discover each other.

Managing Device Discoverability The Bluetooth Adapter’s discoverability is indicated by its scan mode. You can find the adapter’s scan mode by calling getScanMode on the BluetoothAdapter object. It will return one of the following BluetoothAdapter constants:. ➤ SCAN_MODE_CONNECTABLE_DISCOVERABLE Inquiry scan and page scan are both enabled, meaning that the device is discoverable from any Bluetooth device performing a discovery scan. ➤ SCAN_MODE_CONNECTABLE Page Scan is enabled but inquiry scan is not. This means that devices that have previously connected and bonded to the local device can find it during discovery, but new devices can’t. ➤ SCAN_MODE_NONE Discoverability is turned off. No remote devices can find the local adapter during discovery.

Discoverability For privacy reasons, Android devices will default to having discoverability disabled. To turn on discovery you had to obtain explicit permission from the user; you do this by starting a new Activity using the ACTION_REQUEST_DISCOVERABLE action: String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE; startActivityForResult(new Intent(aDiscoverable), DISCOVERY_REQUEST); By default discoverability will be enabled for two minutes. You can modify this setting by adding an EXTRA_DISCOVERABLE_DURATION extra to the launch Intent, specifying the number of seconds you want discoverability to last.

Bluetooth Communications The Bluetooth communications APIs are wrappers around RFCOMM, the Bluetooth radio frequency communications protocol. RFCOMM supports RS232 serial communication over the Logical Link Control and Adaptation Protocol (L2CAP) layer. In practice, this alphabet soup provides a mechanism for opening communication sockets between two paired Bluetooth devices.

Connect You can establish an RFCOMM communication channel for bidirectional communications using the following classes. ➤ BluetoothServerSocket Used to establish a listening socket for initiating a link between devices. To establish a handshake, one device acts as a server to listen for, and accept, incoming connection requests. ➤ BluetoothSocket Used in creating a new client socket to connect to a listening Bluetooth Server Socket, and returned by the Server Socket once a connection is established. Once the connection is made, Bluetooth Sockets are used on both the server and client sides to transfer data streams.

Listen for connections final BluetoothServerSocket btserver = bluetooth.listenUsingRfcommWithServiceRecord(name, uuid); Thread acceptThread = new Thread(new Runnable() { public void run() { try { // Block until client connection established. BluetoothSocket serverSocket = btserver.accept(); // TODO Transfer data using the server socket } catch (IOException e) { Log.d("BLUETOOTH", e.getMessage()); }}});acceptThread.start();

Connecting to a remote Bluetooth server Try{ BluetoothDevice device = bluetooth.getRemoteDevice("00:23:76:35:2F:AA"); BluetoothSocket clientSocket = device.createRfcommSocketToServiceRecord(uuid); clientSocket.connect(); // TODO Transfer data using the Bluetooth Socket } catch (IOException e) { Log.d("BLUETOOTH", e.getMessage()); }

Managing network connectivity The incredible growth of Internet services and the ubiquity of mobile devices have made mobile Internet access an increasingly prevalent feature on mobile phones. With the speed, reliability, and cost of Internet connectivity dependent on the network technology being used (Wi-Fi, GPRS, 3G), letting your applications know and manage these connections can help to ensure they run efficiently and responsively. Android broadcasts Intents that describe changes in network connectivity and offers APIs that provide control over network settings and connections. Just as importantly, users can specify their connectivity preferences — particularly in the case of allowing background data transfers.

Introducing the Connectivity Manager The ConnectivityManager represents the Network Connectivity Service. It’s used to monitor the state of network connections, configure failover settings, and control the network radios. To access the Connectivity Manager, use getSystemService, passing in Context.CONNECTIVITY_SERVICE as the service name.

Accessing the Connectivity Manager String service = Context.CONNECTIVITY_SERVICE; ConnectivityManager connectivity = (ConnectivityManager)getSystemService(service); To use the Connectivity Manager, your application needs read and write network state access permissions. Add each to your manifest, as shown here:

Reading User Preferences for Background Data Transfer One of the most important pieces of information available via the Connectivity Manager is the user’s preference for background data transfers. Users can select to enable or disable background data transfers through the Settings ➪ Accounts & sync settings ➪ Background data setting

While your applications are not forced to obey the user’s preference for background data transfers, not doing so is likely to earn vocal criticism from users who installed your application and were rewarded with a significant mobile data bill.

Finding and Configuring Network Preferences and Controlling Hardware Radios The Connectivity Manager can also be used to control network hardware and configure failover preferences. Android will attempt to connect to the preferred network whenever an authorized application requests an Internet connection. You can find the current, and set the preferred, network using the getNetworkPreference and setNetworkPreference methods, respectively, as shown in the following code snippet: int networkPreference = connectivity.getNetworkPreference(); connectivity.setNetworkPreference(NetworkPreference.PREFE R_WIFI);

Preferable If the preferred connection is unavailable, or connectivity on this network is lost, Android will automatically attempt to connect to the secondary network. You can control the availability of the network types using the setRadio method. This method lets you set the state of the radio associated with a particular network (Wi-Fi, mobile, etc.). For example, in the following code snippet the Wi-Fi radio is turned off and the mobile radio is turned on: connectivity.setRadio(NetworkType.WIFI, false); connectivity.setRadio(NetworkType.MOBILE, true);

Monitoring Network Connectivity One of the most useful functions of the Connectivity Manager is to notify applications of changes in network connectivity. To monitor network connectivity create your own Broadcast Receiver implementation that listens for ConnectivityManager.CONNECTIVITY_ACTION broadcast Intents. Such Intents include several extras that provide additional details on the change to the connectivity state. You can access each extra using one of the static constants available from the ConnectivityManager class: ➤ EXTRA_IS_FAILOVER ➤ EXTRA_NO_CONNECTIVITY ➤ EXTRA_REASON ➤ EXTRA_NETWORK_INFO ➤ EXTRA_OTHER_NETWORK_INFO ➤ EXTRA_EXTRA_INFO

Managing your Wi-Fi The WifiManager represents the Android Wi-Fi Connectivity Service. It can be used to configure Wi-Fi network connections, manage the current Wi-Fi connection, scan for access points, and monitor changes in Wi-Fi connectivity. As with the ConnectivityManager, you access the Wi- FiManager using the getSystemService method,passing in the Context.WIFI_SERVICE constant

Accessing the Wi-Fi Manager String service = Context.WIFI_SERVICE; WifiManager wifi = (WifiManager)getSystemService(service); To use the Wi-Fi Manager your application must have uses- permissions for accessing and changing the Wi-Fi state included in its manifest.

Enable Wi-Fi You can use the Wi-Fi Manager to enable or disable your Wi-Fi hardware using the setWifiEnabled method, or request the current Wi-Fi state using the getWifiState or isWifiEnabled methods. if (!wifi.isWifiEnabled()) if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) wifi.setWifiEnabled(true);

Monitoring Wi-Fi Connectivity The Wi-Fi Manager broadcasts Intents whenever the connectivity status of the Wi-Fi network changes, using an action from one of the following constants defined in the WifiManager class: ➤ WIFI_STATE_CHANGED_ACTION Indicates that the Wi-Fi hardware status has changed, moving between enabling, enabled, disabling, disabled, and unknown. ➤ SUPPLICANT_CONNECTION_CHANGE_ACTION This Intent is broadcast whenever the connection state with the active supplicant (access point) changes. It is fired when a new connection is established or an existing connection is lost. ➤ NETWORK_STATE_CHANGED_ACTION Fired whenever the Wi-Fi connectivity state changes. This Intent includes two extras—the first EXTRA_NETWORK_INFO includes a NetworkInfo object that details the current network state. ➤ RSSI_CHANGED_ACTION You can monitor the current signal strength of the connected Wi-Fi network by listening for the RSSI_CHANGED_ACTION Intent.

Querying the active network connection WifiInfo info = wifi.getConnectionInfo(); if (info.getBSSID() != null) { int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5); int speed = info.getLinkSpeed(); String units = WifiInfo.LINK_SPEED_UNITS; String ssid = info.getSSID(); String cSummary = String.format("Connected to %s at %s%s. Strength %s/5“, ssid, speed, units, strength); }

Managing Wi-Fi Configurations You can use the Wi-FiManager to manage the configured network settings and control which networks to connect to. Once connected, you can interrogate the active network connection to get additional details of its configuration and settings. Get a list of the current network configurations using getConfiguredNetworks. The list of WifiConfiguration objects returned includes the network ID, SSID, and other details for each configuration.

Activating a network connection // Get a list of available configurations List configurations = wifi.getConfiguredNetworks(); // Get the network ID for the first one. if (configurations.size() > 0) { int netID = configurations.get(0).networkId; // Enable that network. boolean disableAllOthers = true; wifi.enableNetwork(netID, disableAllOtherstrue); }

Creating Wi-Fi Network Configurations To connect to a Wi-Fi network you need to create and register a configuration. Normally, your users would do this using the native Wi-Fi configuration settings, but there’s no reason you can’t expose the same functionality within your own applications. Network configurations are stored as WifiConfiguration objects. The following is a non-exhaustive list of some of the public fields available for each Wi-Fi configuration: ➤ BSSID The BSSID for an access point ➤ SSID The SSID for a particular network ➤ networkId A unique identifier used to identify this network configuration on the current device ➤ priority The network configuration’s priority to use when ordering the list of potential access points to connect to ➤ status The current status of this network connection, which will be one of the following: WifiConfiguration.Status.ENABLED, WifiConfiguration.Status.DISABLED, or WifiConfiguration.Status.CURRENT

The configuration object also contains the supported authentication techniques, as well as the keys used previously to authenticate with this access point. The addNetwork method lets you specify a new configuration to add to the current list; similarly, updateNetwork lets you update a network configuration by passing in a WifiConfiguration that’s sparsely populated with a network ID and the values you want to change. You can also use removeNetwork, passing in a network ID, to remove a configuration. To persist any changes made to the network configurations, you must call saveConfiguration.

THE END Thank you