Bluetooth Accessing Android for mca .pptx

TheRockyFF 8 views 18 slides Aug 10, 2024
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

notes


Slide Content

Bluetooth accessing MCA Sem 1

The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.

Using the Bluetooth APIs, an Android application can perform the following Scan for other Bluetooth devices Query the local Bluetooth adapter for paired Bluetooth devices Establish RFCOMM channels Connect to other devices through service discovery Transfer data to and from other devices Manage multiple connections

Classic Bluetooth is the right choice for more battery-intensive operations, which include streaming and communicating between Android devices. For Bluetooth devices with low power requirements, Android 4.3 (API level 18) introduces API support for Bluetooth Low Energy. 

In order for Bluetooth-enabled devices to transmit data between each other, they must first form a channel of communication using a  pairing  process. One device, a  discoverable device , makes itself available for incoming connection requests. Another device finds the discoverable device using a  service discovery  process. After the discoverable device accepts the pairing request, the two devices complete a  bonding  process where they exchange security keys. The devices cache these keys for later use. After the pairing and bonding processes are complete, the two devices exchange information. When the session is complete, the device that initiated the pairing request releases the channel that had linked it to the discoverable device. The two devices remain bonded, however, so they can reconnect automatically during a future session as long as they're in range of each other and neither device has removed the bond.

Bluetooth permissions In order to use Bluetooth features in your application, you must declare two permissions. The first of these is BLUETOOTH . You need this permission to perform any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data. The other permission that you must declare is ACCESS_FINE_LOCATION . Your app needs this permission because a Bluetooth scan can be used to gather information about the location of the user. This information may come from the user's own devices, as well as Bluetooth beacons in use at locations such as shops and transit facilities. Services running on Android 10 and higher cannot discover Bluetooth devices unless they have the ACCESS_BACKGROUND_LOCATION permission. For more information on this requirement, see Access location in the background.

If you want your app to initiate device discovery or manipulate Bluetooth settings, you must declare the BLUETOOTH_ADMIN permission in addition to the BLUETOOTH permission. Most applications need this permission solely for the ability to discover local Bluetooth devices. The other abilities granted by this permission should not be used, unless the application is a "power manager" that modifies Bluetooth settings upon user request.

Android Bluetooth API The android.bluetooth package provides a lot of interfaces classes to work with bluetooth such as: BluetoothAdapter BluetoothDevice BluetoothSocket BluetoothServerSocket BluetoothClass BluetoothProfile BluetoothProfile.ServiceListener BluetoothHeadset BluetoothA2dp BluetoothHealth BluetoothHealthCallback BluetoothHealthAppConfiguration

BluetoothAdapter class By the help of BluetoothAdapter class, we can perform fundamental tasks such as initiate device discovery, query a list of paired (bonded) devices, create a BluetoothServerSocket instance to listen for connection requests etc. Constants of BluetoothAdapter class BluetoothAdapter class provides many constants. Some of them are as follows: String ACTION_REQUEST_ENABLE String ACTION_REQUEST_DISCOVERABLE String ACTION_DISCOVERY_STARTED String ACTION_DISCOVERY_FINISHED

Methods of BluetoothAdapter class static synchronized BluetoothAdapter getDefaultAdapter ()  returns the instance of BluetoothAdapter . boolean enable()  enables the bluetooth adapter if it is disabled. boolean isEnabled ()  returns true if the bluetooth adapter is enabled. boolean disable()  disables the bluetooth adapter if it is enabled. String getName ()  returns the name of the bluetooth adapter. boolean setName (String name)  changes the bluetooth name. int getState ()  returns the current state of the local bluetooth adapter. Set< BluetoothDevice > getBondedDevices ()  returns a set of paired (bonded) BluetoothDevice objects. boolean startDiscovery ()  starts the discovery process.

Android Bluetooth Example: enable, disable and make discovrable bluetooth programmatically activity_main.xml < RelativeLayout   xmlns:androclass = "http://schemas.android.com/ apk /res/android"         xmlns:tools = "http://schemas.android.com/tools"         android:layout_width = " match_parent "         android:layout_height = " match_parent "         tools:context = ". MainActivity "   >            < TextView   android:text = ""          android:id = "@+id/out"           android:layout_width = " wrap_content "           android:layout_height = " wrap_content " >         </ TextView >   

<Button             android:id = "@+id/button1"             android:layout_width = " wrap_content "             android:layout_height = " wrap_content "             android:layout_alignParentLeft = "true"             android:layout_alignParentTop = "true"             android:layout_marginLeft = "30dp"             android:layout_marginTop = "49dp"             android:text = "TURN_ON"   />            

Provide Permission You need to provide following permissions in AndroidManifest.xml file. <uses-permission   android:name = " android.permission.BLUETOOTH "   />    <uses-permission   android:name = " android.permission.BLUETOOTH_ADMIN "   />    ion android:name =" android.permission.BLUETOOTH_PRIVILEGED " tools:ignore =" ProtectedPermissions " /> <uses-permission android:name =" android.permission.BLUETOOTH " /> <uses-permission android:name =" android.permission.BLUETOOTH_ADMIN "/> <uses-permission android:name =" android.permission.BLUETOOTH_ADVERTISE " /> <uses-permission android:name =" android.permission.BLUETOOTH_CONNECT " /> <uses-permission android:name =" android.permission.BLUETOOTH_SCAN " />

Activity class package com.example.bluetoothapp ; import androidx.appcompat.app.AppCompatActivity ; import androidx.core.app.ActivityCompat ; import android.annotation. SuppressLint ; import android.bluetooth.BluetoothAdapter ; import android.content.Intent ; import android.content.pm.PackageManager ; import android.os.Bundle ; import android.view.View ; import android.widget.Button ; import android.widget.TextView ; import android.widget.Toast ; public class MainActivity extends AppCompatActivity { private static final int REQUEST_ENABLE_BT = ; private static final int REQUEST_DISCOVERABLE_BT = ; }

@Override protected void onCreate ( Bundle savedInstanceState ) { super .onCreate ( savedInstanceState ); setContentView ( R . layout . activity_main ); final TextView out = ( TextView ) findViewById ( R . id . out ); final Button button1 = ( Button ) findViewById ( R . id . buttonON ); BluetoothAdapter mBluetoothAdapter = BluetoothAdapter . getDefaultAdapter (); if ( mBluetoothAdapter == null ) { out .append ( "device not supported" ); } button1 .setOnClickListener( new View . OnClickListener () { public void onClick ( View v) { try { if ( mBluetoothAdapter .isEnabled ()) { mBluetoothAdapter .disable (); String s1 = mBluetoothAdapter .getAddress (); String blename = mBluetoothAdapter .getName (); out .setText ( "Bluetooth Disabled" + s1 + " " + blename ); } else { mBluetoothAdapter .enable (); out .setText ( "Bluetooth Enabled" ); } } catch ( SecurityException se) { out .setText ( "exception occured " ); } } }); }

<? xml version ="1.0" encoding ="utf-8" ?> < manifest xmlns: android ="http://schemas.android.com/ apk /res/android" xmlns: tools ="http://schemas.android.com/tools" > < uses-permission android :name =" android.permission.BLUETOOTH_PRIVILEGED " tools :ignore =" ProtectedPermissions " /> < uses-permission android :name =" android.permission.BLUETOOTH " /> < uses-permission android :name =" android.permission.BLUETOOTH_ADMIN " /> < uses-permission android :name =" android.permission.BLUETOOTH_ADVERTISE " /> < uses-permission android :name =" android.permission.BLUETOOTH_CONNECT " /> < uses-permission android :name =" android.permission.BLUETOOTH_SCAN " /> < application android :allowBackup ="true" android :dataExtractionRules ="@xml/ data_extraction_rules " android :fullBackupContent ="@xml/ backup_rules " android :icon ="@mipmap/ ic_launcher " android :label ="@string/ app_name " android :roundIcon ="@mipmap/ ic_launcher_round " android :supportsRtl ="true" android :theme ="@style/ Theme.BluetoothApp " tools :targetApi ="31" > < activity android :name =". MainActivity " android :exported ="true" > < intent-filter > < action android :name =" android.intent.action.MAIN " /> < category android :name =" android.intent.category.LAUNCHER " /> </ intent-filter > </ activity > </ application > </ manifest >

<? xml version ="1.0" encoding ="utf-8" ?> < android.widget.AbsoluteLayout xmlns: android ="http://schemas.android.com/ apk /res/android" xmlns: app ="http://schemas.android.com/ apk /res-auto" xmlns: tools ="http://schemas.android.com/tools" android :layout_width =" match_parent " android :layout_height =" match_parent " tools :context =". MainActivity " > < Button android :id ="@+id/ buttonON " android :layout_width =" wrap_content " android :layout_height =" wrap_content " android :layout_alignParentLeft ="true" android :layout_alignParentTop ="true" android :layout_marginLeft ="100dp" android :layout_marginTop ="49dp" android :layout_x ="147dp" android :layout_y ="268dp" android :text ="TURN_ON" /> < TextView android :id ="@+id/out" android :layout_width =" wrap_content " android :layout_height =" wrap_content " android :layout_x ="130dp" android :layout_y ="400dp" android :textSize ="30dp" android :text =" StatusofBlueTooth " /> </ android.widget.AbsoluteLayout >
Tags