Turn on Bluetooth if available; - Android Bluetooth

Android examples for Bluetooth:Turn On bluetooth

Description

Turn on Bluetooth if available;

Demo Code


//package com.java2s;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;

import android.content.Intent;

import android.widget.Toast;

public class Main {
    /**/*from   w  w  w.ja va 2s .  co  m*/
     * Turn on Bluetooth if available;
     * @return
     */
    public static boolean on(Activity a) {

        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter == null) {
            Toast.makeText(a.getApplicationContext(),
                    "Bluetooth Not available", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!adapter.isEnabled()) {
            Intent enableBluetooth = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            a.startActivityForResult(enableBluetooth, 0);
        }
        return adapter.isEnabled();
    }
}

Related Tutorials