![Turn-On BlueTooth using intent of BluetoothAdapter.ACTION_REQUEST_ENABLE Turn-On BlueTooth using intent of BluetoothAdapter.ACTION_REQUEST_ENABLE](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1LkZQXW2Vh3UEbTB9mQSUCfvlDnFB3_R1AmmoL3iXXq01y5MgsRyMsZ_05PuFR6MsILPNVSWbPNUdrvEVv8BICBQqYlq89bqGGa4958wjsRg99FeKaulmeC7UvI0d1rlkTHWlqKZwhxlm/s400/AndroidBluetooth_02.png)
Modify AndroidBluetooth.java
package com.exercise.AndroidBluetooth;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidBluetooth extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
/** Called when the activity is first created. */
TextView stateBluetooth;
BluetoothAdapter bluetoothAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stateBluetooth = (TextView)findViewById(R.id.bluetoothstate);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
CheckBlueToothState();
}
private void CheckBlueToothState(){
if (bluetoothAdapter == null){
stateBluetooth.setText("Bluetooth NOT support");
}else{
if (bluetoothAdapter.isEnabled()){
if(bluetoothAdapter.isDiscovering()){
stateBluetooth.setText("Bluetooth is currently in device discovery process.");
}else{
stateBluetooth.setText("Bluetooth is Enabled.");
}
}else{
stateBluetooth.setText("Bluetooth is NOT Enabled!");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQUEST_ENABLE_BT){
CheckBlueToothState();
}
}
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8p8OYSJ34x9VZ9cj4IzAOVtRlBOzcLrPRJWQ4iuiJI1eCKjrLYOEL2p2RYTNBidfvJuDOpoiZjXICYCF5ifVUBp09IMGbHsHFSOko1_Xdf4gYrG9MbcBSZDywv5SWAQhCav5OT9OGPE5T/s200/icons_download.gif)
next;
- Get the list of paired Bluetooth devices
1 comment:
On my device, it shows "Bluetooth has stopped"
Post a Comment