Example usage for android.bluetooth BluetoothAdapter getState

List of usage examples for android.bluetooth BluetoothAdapter getState

Introduction

In this page you can find the example usage for android.bluetooth BluetoothAdapter getState.

Prototype

@RequiresPermission(Manifest.permission.BLUETOOTH)
@AdapterState
public int getState() 

Source Link

Document

Get the current state of the local Bluetooth adapter.

Usage

From source file:Main.java

/**
 * Ensure Bluetooth is turned on.//from   w  w  w. j  a  va 2s. co  m
 *
 * @throws IllegalStateException If {@code adapter} is null or Bluetooth state is not
 *             {@link BluetoothAdapter#STATE_ON}.
 */
static void checkAdapterStateOn(BluetoothAdapter adapter) {
    if (adapter == null || adapter.getState() != BluetoothAdapter.STATE_ON) {
        throw new IllegalStateException("BT Adapter is not turned ON");
    }
}

From source file:Main.java

/**
 * Ensure Bluetooth is turned on.//from w w w . j a  v  a2  s  .  c o  m
 *
 * @throws IllegalStateException If {@code adapter} is null or Bluetooth state is not
 *             {@link android.bluetooth.BluetoothAdapter#STATE_ON}.
 */
public static void checkAdapterStateOn(BluetoothAdapter adapter) {
    if (adapter == null || adapter.getState() != BluetoothAdapter.STATE_ON) {
        throw new IllegalStateException("BT Adapter is not turned ON");
    }
}

From source file:Main.java

public static int getBluetoothState() {
    BluetoothAdapter adapter = getBluetoothAdapter();
    return adapter != null ? adapter.getState() : 0;
}

From source file:Main.java

public static boolean isBluetoothTurnedOn() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return mBluetoothAdapter != null && mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
}

From source file:Main.java

public static boolean isBluetoothEnabled(Context mContext) {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null)
        return adapter.getState() == BluetoothAdapter.STATE_ON;
    return false;
}

From source file:Main.java

public static int getBluetoothState(Context context) {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null)
        return adapter.getState();
    else {/*from  w ww. j  a v a2  s. c  om*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
            adapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
        if (adapter != null)
            return adapter.getState();
        else
            return BluetoothAdapter.STATE_OFF;
    }
}

From source file:Main.java

public static void toggleBlueTooth(Context context) {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {
        switch (adapter.getState()) {
        case BluetoothAdapter.STATE_OFF:
            adapter.enable();/* ww w . j ava  2s  .  c  om*/
            break;
        case BluetoothAdapter.STATE_ON:
            adapter.disable();
            break;
        }
    }
}

From source file:Main.java

public static boolean isBluetoothAvailable() {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (!adapter.isEnabled()) {
        if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
            adapter.enable();/*from   w  w w . ja v a 2s .  c o  m*/
        }
    }
    return adapter.getState() == BluetoothAdapter.STATE_ON;
}

From source file:Main.java

public static int getBluetoothState() throws Exception {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        throw new Exception("bluetooth device not found!");
    } else {/*w ww  .  j a v  a 2 s.com*/
        return bluetoothAdapter.getState();
    }
}

From source file:com.imagine.BaseActivity.java

int btState(BluetoothAdapter adapter) {
    return adapter.getState();
}