On Launch
on BlueTooth Enable
on BlueTooth Disable
Create new Android Project
Project Name: System Settings
//tested from 2.3.3 to current android sdk
Build Target: Android 2.3.3 //or greater than that
Application Name: SystemSettings
Package Name: com.shaikhhamadali.blogspot.systemsettings
Create layout file: activity_Blue_tooth
1. create layout:
<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"
>
<TextView
android:id="@+id/TVBlueTooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:text="Blue Tooth: Disable"
android:textColor="#1BD6E0"
android:textSize="40sp" />
<ToggleButton
android:id="@+id/tBBlueTooth"
android:layout_width="300dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:textSize="30sp"
android:textOff="Enable"
android:textOn="Disable" />
</RelativeLayout>
- Add Permission in Manifest:
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
package com.shaikhhamadali.blogspot.systemsettings;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;
public class BlueTooth_Toggle extends Activity{
// constants
static final String STATUS_ON = "Blue Tooth: Enable";
static final String STATUS_OFF = "Blue Tooth: Disable";
static final String TURN_ON = "Enable";
static final String TURN_OFF = "Disable";
// controls
TextView TVBlueTooth;
ToggleButton tBBlueTooth;
//variable
BluetoothAdapter mBluetoothAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue_tooth);
// load controls
TVBlueTooth=(TextView)findViewById(R.id.TVBlueTooth);
tBBlueTooth=(ToggleButton)findViewById(R.id.tBBlueTooth);
// set click event for button
tBBlueTooth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// check current state first
boolean state = isBlueToothEnable();
// toggle the state
if(state)toggleBlueTooth(false);
else toggleBlueTooth(true);
// update UI to new state
updateUI(!state);
}
});
}
public void updateUI(boolean state) {
//set text according to state
if(!state) {
TVBlueTooth.setText(STATUS_OFF);
tBBlueTooth.setText(TURN_ON);
} else {
TVBlueTooth.setText(STATUS_ON);
tBBlueTooth.setText(TURN_OFF);
}
}
public boolean isBlueToothEnable() {
//get status of blue tooth
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
return mBluetoothAdapter.isEnabled();
}
public void toggleBlueTooth(boolean stat){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (stat) {
mBluetoothAdapter.enable();
}
else{
mBluetoothAdapter.disable();
}
}
}
3. note that:
- you can use this on button onclick,on action_down,on the fly etc.
- you can toggle more setting options refer my previous posts toggle Airplane Mode,toggle Mobile Data..
- about BlueTooth Adapter.
- Some information about how to toggle BlueTooth (Enable /Disable).
- Know how to update UI after toggle.
- know what is BlueTooth Adapter.
5. About the post:
- The code seems to explain itself due to comments, if you have any question you can ask too!
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code here
Cheers,
you can download the source code here
Hamad Ali Shaikh