Example usage for android.content Context BLUETOOTH_SERVICE

List of usage examples for android.content Context BLUETOOTH_SERVICE

Introduction

In this page you can find the example usage for android.content Context BLUETOOTH_SERVICE.

Prototype

String BLUETOOTH_SERVICE

To view the source code for android.content Context BLUETOOTH_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.bluetooth.BluetoothManager for using Bluetooth.

Usage

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  . ja v a 2 s. c  o m*/
        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

/**
 * Check if bluetooth function enabled//w  w  w .  ja  v a  2  s.c o  m
 *
 * @param context the context
 * @return true if bluetooth enabled
 */
public static boolean isBluetoothEnabled(@NonNull Context context) {
    BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

    if (bluetoothManager == null) {
        return false;
    }

    final BluetoothAdapter bluetoothAdapter;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        bluetoothAdapter = bluetoothManager.getAdapter();
    } else {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    if (bluetoothAdapter == null) {
        return false;
    }

    return bluetoothAdapter.isEnabled();
}

From source file:Main.java

/**
 * Enable bluetooth and get the adapter object
 * @return A BluetoothAdapter instance/*from w  ww  .j av  a  2s.c  om*/
 */
public static BluetoothAdapter getLeAdapter(Activity _this) {
    // Initializes Bluetooth adapter.
    final BluetoothManager bluetoothLeManager = (BluetoothManager) _this
            .getSystemService(Context.BLUETOOTH_SERVICE);
    final BluetoothAdapter bluetoothLeAdapter = bluetoothLeManager.getAdapter();

    // Ensures Bluetooth is available on the device and it is enabled. If not,
    // displays a dialog requesting user permission to mEnableCharacteristic Bluetooth.
    // TODO / FIXME currently this bugs if the user doesn't have BT enabled,
    // TODO / FIXME since it just returns null and doesn't check the result later.
    if (bluetoothLeAdapter == null || !bluetoothLeAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        _this.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        return null;
    } else {
        return bluetoothLeAdapter;
    }
}

From source file:Main.java

public static Pair<BluetoothManager, BluetoothAdapter> checkBT(final Activity activity) {
    // Use this check to determine whether BLE is supported on the device. Then
    // you can selectively disable BLE-related features.
    if (!btleSupport(activity)) {
        return null;
    }/*from  w  ww .  j av  a 2  s . c  om*/
    final BluetoothManager bluetoothManager = (BluetoothManager) activity
            .getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

    //On some devices this does not work
    try {
        bluetoothAdapter.getBluetoothLeScanner();
    } catch (NoSuchMethodError e) {
        return null;
    }

    // Ensures Bluetooth is available on the device and it is enabled. If not,
    // displays a dialog requesting user permission to enable Bluetooth.
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(enableBtIntent, 1);
        bluetoothAdapter = bluetoothManager.getAdapter();
    }
    return new Pair<BluetoothManager, BluetoothAdapter>(bluetoothManager, bluetoothAdapter);
}

From source file:Main.java

/**
 * Check if Bluetooth LE Peripheral mode supported on the running environment.
 *
 * @param context the context//from w  w  w .j  a v  a 2 s  . c o  m
 * @return true if supported
 */
@SuppressLint("NewApi")
public static boolean isBlePeripheralSupported(@NonNull Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return false;
    }

    final BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context
            .getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();

    if (bluetoothAdapter == null) {
        return false;
    }

    return bluetoothAdapter.isMultipleAdvertisementSupported();
}

From source file:Main.java

/**
 * Check if Bluetooth LE device supported on the running environment.
 *
 * @param context the context/*from w  w w .  j  av a 2s .  co m*/
 * @return true if supported
 */
public static boolean isBleSupported(@NonNull Context context) {
    try {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) == false) {
            return false;
        }

        BluetoothManager bluetoothManager = (BluetoothManager) context
                .getSystemService(Context.BLUETOOTH_SERVICE);

        final BluetoothAdapter bluetoothAdapter;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            bluetoothAdapter = bluetoothManager.getAdapter();
        } else {
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        }

        if (bluetoothAdapter != null) {
            return true;
        }
    } catch (Throwable t) {
        // ignore exception
    }
    return false;
}

From source file:jdev.cordova.bluetooth.ble.BLEHelper.java

private void init() {
    activity = cordova.getActivity();/*from   w  w  w  . ja  v  a  2  s .  co  m*/
    context = activity.getApplicationContext();
    bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    gson = new Gson();
}

From source file:com.android.bleserver.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle(R.string.activity_main_title);

    if (savedInstanceState == null) {

        mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();

        // Is Bluetooth supported on this device?
        if (mBluetoothAdapter != null) {

            // Is Bluetooth turned on?
            if (mBluetoothAdapter.isEnabled()) {

                // Are Bluetooth Advertisements supported on this device?
                if (mBluetoothAdapter.isMultipleAdvertisementSupported()) {

                    // Everything is supported and enabled, load the fragments.
                    setupFragments();/*  ww w .  j a v  a  2s.co m*/

                } else {

                    // Bluetooth Advertisements are not supported.
                    showErrorText(R.string.bt_ads_not_supported);
                }
            } else {

                // Prompt user to turn on Bluetooth (logic continues in onActivityResult()).
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, Constants.REQUEST_ENABLE_BT);
            }
        } else {

            // Bluetooth is not supported.
            showErrorText(R.string.bt_not_supported);
        }
    }
}

From source file:sample.hawk.com.mybasicappcomponents.hardware.bt_advertisements.BLE_AdvertisementsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bt_advertisement_activity);
    setTitle(R.string.btadv_activity_title);

    if (savedInstanceState == null) {

        mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();

        // Is Bluetooth supported on this device?
        if (mBluetoothAdapter != null) {

            // Is Bluetooth turned on?
            if (mBluetoothAdapter.isEnabled()) {

                // Are Bluetooth Advertisements supported on this device?
                if (mBluetoothAdapter.isMultipleAdvertisementSupported()) {

                    // Everything is supported and enabled, load the fragments.
                    setupFragments();/*from   w  ww.  j ava  2 s .c  om*/

                } else {

                    // Bluetooth Advertisements are not supported.
                    showErrorText(R.string.bt_ads_not_supported);
                }
            } else {

                // Prompt user to turn on Bluetooth (logic continues in onActivityResult()).
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, BLE_Constants.REQUEST_ENABLE_BT);
            }
        } else {

            // Bluetooth is not supported.
            showErrorText(R.string.bt_not_supported);
        }
    }
}

From source file:com.jaguarlandrover.auto.remote.vehicleentry.BeaconRanger.java

public BeaconRanger(Context ctx) {
    final BluetoothManager bluetoothManager = (BluetoothManager) ctx
            .getSystemService(Context.BLUETOOTH_SERVICE);
    adapter = bluetoothManager.getAdapter();

    prefs = PreferenceManager.getDefaultSharedPreferences(ctx);

    numberMesurments = Integer.parseInt(prefs.getString("pref_ranging_sample_buffer", "10"));
    ranges = EvictingQueue.create(numberMesurments);
    weightedRanges = EvictingQueue.create(numberMesurments);
    calc = new CurveFittedDistanceCalculator(0.42093, 6.9476, 0.54992);
}