You are on page 1of 8

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 android.bluetooth 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,main.xml file will like
this:
3ile: activity,main.xml
5. 6-elative#ayout xmlns:androclass78http:99schemas.android.com9apk9res9android8
%. xmlns:tools78http:99schemas.android.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=anifest.xml file.
5. 6usesFpermission android:name78android.permission.B#0.T**T$8 9>
%. 6usesFpermission android:name78android.permission.B#0.T**T$,A=I+8 9>
The full code of Android=anifest.xml file is given below.
3ile: Android=anifest.xml
5. 6Gxml version785.E8 encoding78utfFA8G>
%. 6manifest xmlns:androclass78http:99schemas.android.com9apk9res9android8
:. package78com.example.bluetooth8
;. android:version"ode7858
<. android:version+ame785.E8 >
?.
@. 6usesFsdk
A. android:min!dk1ersion78A8
D. android:target!dk1ersion785?8 9>
5E.
55. 6usesFpermission android:name78android.permission.B#0.T**T$8 9>
5%. 6usesFpermission android:name78android.permission.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:name78com.example.bluetooth.=ainActivity8
%5. android:label78Bstring9app,name8 >
%%. 6intentFfilter>
%:. 6action android:name78android.intent.action.=AI+8 9>
%;.
%<. 6category android:name78android.intent.category.#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 com.example.bluetoothI
%. import android.os.BundleI
:. import android.app.ActivityI
;. import android.view.=enuI
<. import android.app.ActivityI
?. import android.bluetooth.BluetoothAdapterI
@. import android.content."ontextI
A. import android.content.IntentI
D. import android.os.BundleI
5E. import android.util.#ogI
55. import android.view.1iewI
5%. import android.widget.ButtonI
5:. import android.widget.Text1iewI
5;. import android.widget.ToastI
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. super.on"reate(savedInstance!tate)I
%%. set"ontent1iew(-.layout.activity,main)I
%:. final Text1iew out7(Text1iew)find1iewById(-.id.out)I
%;. final Button button5 7 (Button) find1iewById(-.id.button5)I
%<. final Button button% 7 (Button) find1iewById(-.id.button%)I
%?. final Button button: 7 (Button) find1iewById(-.id.button:)I
%@. final BluetoothAdapter mBluetoothAdapter 7 BluetoothAdapter.getefaultAdapter()I
%A. if (mBluetoothAdapter 77 null) J
%D. out.append(8device not supported8)I
:E. K
:5. button5.set*n"lick#istener(new 1iew.*n"lick#istener() J
:%. public void on"lick(1iew v) J
::. if (LmBluetoothAdapter.is.nabled()) 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 (LmBluetoothAdapter.isiscovering()) J
;:. 99out.append(8=AMI+N 2*0- .1I". I!"*1.-AB#.8)I
;;. Toast.makeText(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
<?. mBluetoothAdapter.disable()I
<@. 99out.append(8T0-+,*33 B#0.T**T$8)I
<A. Toast.makeText(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(-.menu.activity,main& menu)I
?A. return trueI
?D. K
@E.
@5. K
download this android example

You might also like