15/07/2015 Android Bluetooth Switch on/off programmatically
http://www.mysamplecode.com/2012/11/android-bluetooth-switch-onoff.html 1/3
MySampleCode Mashery API ManagementComplete and secure API management from Mashery, An Intel Company
Android Bluetooth Switch on/off programmatically
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.
Android Manifest
123456789101112131415161718192021
<?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>
Application Layout - activity_main.xml
12345678910111213141516171819
<?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>
Search
Search this Website ...
Check out the Blog Archive
Recommend this on Google
Android Listview Example usingCursorAdapter and SQLite database
Dynamically generate HTML table usingJavaScript - document.createElement()method
Android ListView Checkbox Example -OnItemClickListener() andOnClickListener()
Android switch button example
Android SQLITE query selection example
jQuery AJAX request and responseexample - Java Servlets, MySQL and JSON
Android EditText text change listenerexample
Popular Posts ...
iOS UISearchBar with UITableViewexampleUISearchBar class provides a text field for enteringtext, a search button, a bookmark button, and acancel button. With…
Android JSON stream data parsingexample using GsonIf the request to the Web service is a simple JSONobject, then you can convert the HTTP response…
Sencha Touch auto login exampleIf you are creating a native application using SenchaTouch then most likely your users may expect theapp to…
Java SFTP Apache commons filedownload, upload and delete exampleSecure File Transfer Protocol or SFTP uses the SSHsecure shell protocol to provides file access, filetransfer, and file…
iOS programmatically create a customUITableViewCellIn our previous tutorial we learned how to create acustom UITableViewCell using a xib file. Mypersonal preference is…
Recent Posts ...
Terms and Conditions
Privacy Policy
Site Information ...
?
?
15/07/2015 Android Bluetooth Switch on/off programmatically
http://www.mysamplecode.com/2012/11/android-bluetooth-switch-onoff.html 2/3
Application Activity - MainActivity.java
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
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) ... } }}
Comments Policy
?
15/07/2015 Android Bluetooth Switch on/off programmatically
http://www.mysamplecode.com/2012/11/android-bluetooth-switch-onoff.html 3/3
Newer Post Older PostHome
Subscribe to: Post Comments ( Atom )
Tutorial Tags: Android , BlueTooth
Did you find this article helpful? Share it!
Enter your comment...
Comment as: Google Account
Publish Preview
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
Copyright © 2011-2013 mysamplecode.com, All rights reserved. Powered by Blogger.
Top Related