Sunteți pe pagina 1din 3

15/07/2015

Android Bluetooth Switch on/off programmatically

MySampleCode

Mashery API Management


Complete and secure API management from Mashery, An Intel Company

Search this Website ...

Android Bluetooth Switch on/off programmatically

Search

Bluetooth is a wireless technology standard for exchanging data over short distances, low-bandwidth peer-to-peer
communications, between mobile devices creating personal area networks (PANs) with high levels of security. The Bluetooth
device in Android is controlled via the BluetoothAdapter class. For the adatper to work you application manifest needs to include
the BLUETOOTH and the BLUETOOTH_ADMIN permission.
If you are going to use Bluetooth Scanner in your mobile application to scan barcodes then it will stop you from using the soft
keyboard unless a keyboard wedge is provided by the scanner company. So the only way to use the keyboard is to switch off the
bluetooth as the mobile device thinks that the scanner is as a bluetooth keyboard. The ablity to quickly switch off bluetooth and
switch it back on is needed if you want to both scan barcodes as well as use the keyboard in your app.

Check out the Blog Archive


Recommend this on Google

Popular Posts ...


Android Listview Example using
CursorAdapter and SQLite database
Dynamically generate HTML table using
JavaScript - document.createElement()
method
Android ListView Checkbox Example OnItemClickListener() and
OnClickListener()
Android switch button example
Android SQLITE query selection example
jQuery AJAX request and response
example - Java Servlets, MySQL and JSON
Android EditText text change listener
example

Android Manifest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

<?xml version="1.0" encoding="UTF-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.as400samplecode" android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />


<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Recent Posts ...


iOS UISearchBar with UITableView
example

UISearchBar class provides a text field for entering


text, a search button, a bookmark button, and a
cancel button. With

Android JSON stream data parsing


example using Gson

If the request to the Web service is a simple JSON


object, then you can convert the HTTP response

Sencha Touch auto login example

Application Layout - activity_main.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

?
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="5dp">

<TextView android:id="@+id/status" android:layout_width="fill_parent"


android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="10dp"
android:text="BlueTooth is currently switched OFF"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<Button android:id="@+id/changeStatus" style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:layout_below="@+id/status"
android:layout_marginTop="10dp" android:text="Switch ON Bluetooth"
android:textStyle="bold" />
</RelativeLayout>

http://www.mysamplecode.com/2012/11/android-bluetooth-switch-onoff.html

If you are creating a native application using Sencha


Touch then most likely your users may expect the
app to

Java SFTP Apache commons file


download, upload and delete example

Secure File Transfer Protocol or SFTP uses the SSH


secure shell protocol to provides file access, file
transfer, and file

iOS programmatically create a custom


UITableViewCell

In our previous tutorial we learned how to create a


custom UITableViewCell using a xib file. My
personal preference is

Site Information ...


Terms and Conditions
Privacy Policy

1/3

15/07/2015

Android Bluetooth Switch on/off programmatically


Comments Policy

Application Activity - MainActivity.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

package com.as400samplecode;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private BluetoothAdapter mBluetoothAdapter;
private Button changeStatus;
private TextView status;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//reference to the button
changeStatus = (Button) findViewById(R.id.changeStatus);
changeStatus.setOnClickListener(this);
//reference to the text view
status = (TextView) findViewById(R.id.status);
//reference to the bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//check if adatpter is available, please note if you running
//this application in emulator currently there is no support for bluetooth
if(mBluetoothAdapter == null){
status.setText("BlueTooth adapter not found");
changeStatus.setText("BlueTooth Disabled");
changeStatus.setEnabled(false);
}
//check the status and set the button text accordingly
else {
if (mBluetoothAdapter.isEnabled()) {
status.setText("BlueTooth is currently switched ON");
changeStatus.setText("Switch OFF Bluetooth");
}else{
status.setText("BlueTooth is currently switched OFF");
changeStatus.setText("Switch ON Bluetooth");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.changeStatus:
//disable the bluetooth adapter
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
status.setText("BlueTooth is currently switched OFF");
changeStatus.setText("Switch ON Bluetooth");
}
//enable the bluetooth adapter
else{
mBluetoothAdapter.enable();
status.setText("BlueTooth is currently switched ON");
changeStatus.setText("Switch OFF Bluetooth");
}
break;
// More buttons go here (if any) ...
}
}
}

http://www.mysamplecode.com/2012/11/android-bluetooth-switch-onoff.html

2/3

15/07/2015

Android Bluetooth Switch on/off programmatically

Tutorial Tags: Android , BlueTooth


Did you find this article helpful? Shar e it!

4 comments :
Jay Tirouvengadam VinothKumar October 24, 2013 at 3:06 AM
I have a 7" ICS 4.0.4 android tablet pc with rooted can i block or remove permanently the wifi, bluetooth, hdmi, otg, sd card and
all communication's.
Please guide me...

Reply

Anonymous July 22, 2014 at 6:15 AM


It is an example of very good and for me, a good base for another important issue. Thank you and congratulations

Reply

Anonymous January 17, 2015 at 11:56 PM


Thank you very much for the guidence

Reply

trongvodang April 11, 2015 at 6:33 AM


See whether the subsidizing organization makes awards to people. Painstakingly consider this step in light of the fact that some
stipend producers provide for associations that serve people instead of specifically financing people.

Reply

Enter your comment...

Comment as:

Publish

Google Account

Preview

Newer Post

Home

Older Post

Subscribe to: Post Comments ( Atom )

Copyright 2011-2013 mysamplecode.com, All rights reserved. Powered by Blogger.

http://www.mysamplecode.com/2012/11/android-bluetooth-switch-onoff.html

3/3