set Bluetooth - Android Phone

Android examples for Phone:Bluetooth

Description

set Bluetooth

Demo Code


//package com.java2s;

import android.bluetooth.BluetoothAdapter;

public class Main {

    public static void setBluetooth(boolean enable) throws Exception {

        if (isBluetoothOpen() != enable) {

            if (enable) {
                BluetoothAdapter.getDefaultAdapter().enable();
            } else {
                BluetoothAdapter.getDefaultAdapter().disable();
            }/*from   w w w.  ja  v  a 2  s. co  m*/
        }
    }

    public static boolean isBluetoothOpen() throws Exception {
        int bluetoothStateCode = getBluetoothState();
        return bluetoothStateCode == BluetoothAdapter.STATE_ON
                || bluetoothStateCode == BluetoothAdapter.STATE_TURNING_ON ? true
                : false;
    }

    public static int getBluetoothState() throws Exception {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter
                .getDefaultAdapter();
        if (bluetoothAdapter == null) {
            throw new Exception("bluetooth device not found!");
        } else {
            return bluetoothAdapter.getState();
        }
    }
}

Related Tutorials