0% found this document useful (0 votes)
64 views8 pages

Android Bluetooth Programming Guide

This document provides information about Bluetooth functionality in Android. It discusses: 1. The Android Bluetooth API allows apps to scan for Bluetooth devices, connect to and transfer data with other devices, and manage multiple connections. Key classes include BluetoothAdapter, BluetoothDevice, and BluetoothSocket. 2. The BluetoothAdapter class enables fundamental tasks like device discovery, querying paired devices, and creating server sockets. Common methods include getDefaultAdapter(), enable(), disable(), and startDiscovery(). 3. An Android example shows how to enable, disable, and make the device discoverable programmatically using the BluetoothAdapter class and intents. Permissions must be declared and buttons can control various Bluetooth states.

Uploaded by

pakadam7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views8 pages

Android Bluetooth Programming Guide

This document provides information about Bluetooth functionality in Android. It discusses: 1. The Android Bluetooth API allows apps to scan for Bluetooth devices, connect to and transfer data with other devices, and manage multiple connections. Key classes include BluetoothAdapter, BluetoothDevice, and BluetoothSocket. 2. The BluetoothAdapter class enables fundamental tasks like device discovery, querying paired devices, and creating server sockets. Common methods include getDefaultAdapter(), enable(), disable(), and startDiscovery(). 3. An Android example shows how to enable, disable, and make the device discoverable programmatically using the BluetoothAdapter class and intents. Permissions must be declared and buttons can control various Bluetooth states.

Uploaded by

pakadam7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
  • BluetoothAdapter Class
  • Android Bluetooth Tutorial
  • Android Bluetooth API
  • Constants and Methods of BluetoothAdapter
  • XML Configuration
  • Provide Permission
  • Activity Class

Android Bluetooth Tutorial

Bluetooth is a way to exchange data with other devices wirelessly. Android provides Bluetooth
API to perform several tasks such as:
scan bluetooth devices
connect and transfer data from and to other devices
manage multiple connections etc.
Android Bluetooth API
The [Link] package provides a lot of interfaces classes to work with bluetooth such
as:
BluetoothAdapter
Bluetoothevice
Bluetooth!ocket
Bluetooth!erver!ocket
Bluetooth"lass
BluetoothProfile
BluetoothProfile.!ervice#istener
Bluetooth$eadset
BluetoothA%dp
Bluetooth$ealth
Bluetooth$ealth"allback
Bluetooth$ealthApp"onfiguration
BluetoothAdapter class
By the help of BluetoothAdapter class& we can perform fundamental tasks such as initiate device
discovery& 'uery a list of paired (bonded) devices& create a Bluetooth!erver!ocket instance to
listen for connection re'uests etc.
Constants of BluetoothAdapter class
BluetoothAdapter class provides many constants. !ome of them are as follows:
!tring A"TI*+,-./0.!T,.+AB#.
!tring A"TI*+,-./0.!T,I!"*1.-AB#.
!tring A"TI*+,I!"*1.-2,!TA-T.
!tring A"TI*+,I!"*1.-2,3I+I!$.
Methods of BluetoothAdapter class
"ommonly used methods of BluetoothAdapter class are as follows:
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.
tring get!a"e() returns the name of the bluetooth adapter.
boolean set!a"e(tring na"e) changes the bluetooth name.
int gettate() returns the current state of the local bluetooth adapter.
et#BluetoothDe$ice% getBondedDe$ices() returns a set of paired (bonded)
Bluetoothevice ob4ects.
boolean startDisco$ery() starts the discovery process.
Android Bluetooth E&a"ple' enable( disable and "a)e disco$rable bluetooth
progra""atically
2ou need to write few lines of code only& to enable or disable the bluetooth.
acti$ity*"ain+&"l
rag one textview and three buttons from the pallete& now the activity,[Link] file will like
this:
3ile: activity,[Link]
5. 6-elative#ayout xmlns:androclass78http:[Link].com9apk9res9android8
%. xmlns:tools78http:[Link].com9tools8
:. android:layout,width78match,parent8
;. android:layout,height78match,parent8
<. tools:context78.=ainActivity8 >
?.
@. 6Text1iew android:text788
A. android:id78BCid9out8
D. android:layout,width78wrap,content8
5E. android:layout,height78wrap,content8>
55. 69Text1iew>
5%. 6Button
5:. android:id78BCid9button58
5;. android:layout,width78wrap,content8
5<. android:layout,height78wrap,content8
5?. android:layout,alignParent#eft78true8
5@. android:layout,alignParentTop78true8
5A. android:layout,margin#eft78:Edp8
5D. android:layout,marginTop78;Ddp8
%E. android:text78T0-+,*+8 9>
%5.
%%. 6Button
%:. android:id78BCid9button%8
%;. android:layout,width78wrap,content8
%<. android:layout,height78wrap,content8
%?. android:layout,align#eft78BCid9button58
%@. android:layout,below78BCid9button58
%A. android:layout,marginTop78%@dp8
%D. android:text78I!"*1.-AB#.8 9>
:E.
:5. 6Button
:%. android:id78BCid9button:8
::. android:layout,width78wrap,content8
:;. android:layout,height78wrap,content8
:<. android:layout,align#eft78BCid9button%8
:?. android:layout,below78BCid9button%8
:@. android:layout,marginTop78%Adp8
:A. android:text78T0-+,*338 9>
:D.
;E. 69-elative#ayout>
Pro$ide Per"ission
2ou need to provide following permissions in Android=[Link] file.
5. 6usesFpermission android:[Link].B#0.T**T$8 9>
%. 6usesFpermission android:[Link].B#0.T**T$,A=I+8 9>
The full code of Android=[Link] file is given below.
3ile: Android=[Link]
5. 6Gxml version785.E8 encoding78utfFA8G>
%. 6manifest xmlns:androclass78http:[Link].com9apk9res9android8
:. [Link].bluetooth8
;. android:version"ode7858
<. android:version+ame785.E8 >
?.
@. 6usesFsdk
A. android:min!dk1ersion78A8
D. android:target!dk1ersion785?8 9>
5E.
55. 6usesFpermission android:[Link].B#0.T**T$8 9>
5%. 6usesFpermission android:[Link].B#0.T**T$,A=I+8 9>
5:.
5;. 6application
5<. android:allowBackup78true8
5?. android:icon78Bdrawable9ic,launcher8
5@. android:label78Bstring9app,name8
5A. android:theme78Bstyle9AppTheme8 >
5D. 6activity
%E. android:[Link].=ainActivity8
%5. android:label78Bstring9app,name8 >
%%. 6intentFfilter>
%:. 6action android:[Link].=AI+8 9>
%;.
%<. 6category android:[Link].#A0+"$.-8 9>
%?. 69intentFfilter>
%@. 69activity>
%A. 69application>
%D.
:E. 69manifest>
Acti$ity class
#etHs write the code to enable& disable and make bluetooth discoverable.
3ile: =ainActivity.4ava
5. package [Link]
%. import [Link]
:. import [Link]
;. import [Link].=enuI
<. import [Link]
?. import [Link]
@. import [Link]."ontextI
A. import [Link]
D. import [Link]
5E. import [Link].#ogI
55. import [Link].1iewI
5%. import [Link]
5:. import [Link].Text1iewI
5;. import [Link]
5<.
5?. public class =ainActivity extends Activity J
5@. private static final int -./0.!T,.+AB#.,BT 7 EI
5A. private static final int -./0.!T,I!"*1.-AB#.,BT 7 EI
5D. B*verride
%E. protected void on"reate(Bundle savedInstance!tate) J
%5. [Link]"reate(savedInstance!tate)I
%%. set"ontent1iew(-.[Link],main)I
%:. final Text1iew out7(Text1iew)find1iewById(-.[Link])I
%;. final Button button5 7 (Button) find1iewById(-.id.button5)I
%<. final Button button% 7 (Button) find1iewById(-.[Link]%)I
%?. final Button button: 7 (Button) find1iewById(-.[Link]:)I
%@. final BluetoothAdapter mBluetoothAdapter 7 [Link]()I
%A. if (mBluetoothAdapter 77 null) J
%D. [Link](8device not supported8)I
:E. K
:5. [Link]*n"lick#istener(new 1iew.*n"lick#istener() J
:%. public void on"lick(1iew v) J
::. if ([Link]()) J
:;. Intent enableBtIntent 7 new Intent(BluetoothAdapter.A"TI*+,-./0.!T,.
+AB#.)I
:<. startActivity3or-esult(enableBtIntent& -./0.!T,.+AB#.,BT)I
:?. K
:@. K
:A. K)I
:D. button%.set*n"lick#istener(new 1iew.*n"lick#istener() J
;E. B*verride
;5. public void on"lick(1iew argE) J
;%. if ([Link]()) J
;:. [Link](8=AMI+N 2*0- .1I". I!"*1.-AB#.8)I
;;. [Link](getApplication"ontext()& 8=AMI+N 2*0- .1I". I!"
*1.-AB#.8&
;<. Toast.#.+NT$,#*+N)I
;?.
;@. Intent enableBtIntent 7 new Intent(BluetoothAdapter.A"TI*+,-./0.!T,I
!"*1.-AB#.)I
;A. startActivity3or-esult(enableBtIntent& -./0.!T,I!"*1.-AB#.,BT)I
;D.
<E. K
<5. K
<%. K)I
<:. button:.set*n"lick#istener(new 1iew.*n"lick#istener() J
<;. B*verride
<<. public void on"lick(1iew argE) J
<?. [Link]()I
<@. [Link](8T0-+,*33 B#0.T**T$8)I
<A. [Link](getApplication"ontext()& 8T0-+I+N,*33 B#0.T**T$8& Toa
st.#.+NT$,#*+N)I
<D.
?E. K
?5. K)I
?%. K
?:.
?;. B*verride
?<. public boolean on"reate*ptions=enu(=enu menu) J
??. 99 Inflate the menuI this adds items to the action bar if it is present.
?@. get=enuInflater().inflate(-.[Link],main& menu)I
?A. return trueI
?D. K
@E.
@5. K
download this android example

Android Bluetooth Tutorial
Bluetooth is a way to exchange data with other devices wirelessly. Android provides Bluetooth 
API
Constants of BluetoothAdapter class
BluetoothAdapter class provides many constants. Some of them are as follows:

String ACT
2.     xmlns:tools="http://schemas.android.com/tools"  
3.     android:layout_width="match_parent"  
4.     android:layout_he
29.         android:text="DISCOVERABLE" />  
30.   
31.     <Button  
32.         android:id="@+id/button3"  
33.         and
9.         android:targetSdkVersion="16" />  
10.   
11.     <uses-permission android:name="android.permission.BLUETOOTH" />
2. import android.os.Bundle;  
3. import android.app.Activity;  
4. import android.view.Menu;  
5. import android.app.Activit
29.        out.append("device not supported");  
30.     }  
31.     button1.setOnClickListener(new View.OnClickListener() {
54.         @Override  
55.         public void onClick(View arg0) {     
56.             mBluetoothAdapter.disable();  
57.

You might also like