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:cloudbank.ble.activity.DeviceScanActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan);

    //getSupportActionBar().setElevation(3);
    //getSupportActionBar().setTitle(R.string.app_name);

    // Sets the Toolbar to act as the ActionBar for this Activity window.
    // Make sure the toolbar exists in the activity and is not null

    // Use this check to determine whether BLE is supported on the device.  Then you can
    // selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();//from w  w  w.  ja  v  a  2s  . co  m
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragHolder, new ScanListFragment());
    ft.commit();
}

From source file:com.orange.beaconme_sdk.ble.control.BLEDeviceScanner.java

@Override
public void onCreate() {
    Log.d(TAG, "onCreate");
    super.onCreate();
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mAdapter = bluetoothManager.getAdapter();

    if (mAdapter != null && mAdapter.isEnabled()) {
        startScanning();//from   ww w .ja va 2 s  .  co  m
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(receiver, intentFilter);
    } else {
        stopSelf();
    }
}

From source file:com.megster.cordova.rfduino.RFduinoPlugin.java

@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {

    LOG.d(TAG, "action = " + action);

    if (bluetoothAdapter == null) {
        Activity activity = cordova.getActivity();
        BluetoothManager bluetoothManager = (BluetoothManager) activity
                .getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
    }//w ww  . j a  v a2s.  c om

    boolean validAction = true;

    if (action.equals(DISCOVER)) {

        int scanSeconds = args.getInt(0);
        findLowEnergyDevices(callbackContext, scanSeconds);

    } else if (action.equals(LIST)) {

        listKnownDevices(callbackContext);

    } else if (action.equals(CONNECT)) {

        String uuid = args.getString(0);
        connect(callbackContext, uuid);

    } else if (action.equals(ON_DATA)) {

        registerOnDataCallback(callbackContext);

    } else if (action.equals(DISCONNECT)) {

        disconnect(callbackContext);

    } else if (action.equals(WRITE)) {

        byte[] data = args.getArrayBuffer(0);
        write(callbackContext, data);

    } else if (action.equals(IS_ENABLED)) {

        if (bluetoothAdapter.isEnabled()) {
            callbackContext.success();
        } else {
            callbackContext.error("Bluetooth is disabled.");
        }

    } else if (action.equals(IS_CONNECTED)) {

        if (activePeripheral != null && activePeripheral.isConnected()) {
            callbackContext.success();
        } else {
            callbackContext.error("Not connected.");
        }

    } else {

        validAction = false;

    }

    return validAction;
}

From source file:com.djit.mixfader.sample.BaseActivity.java

/**
 * Handles permissions and features needed by the app
 *///from  ww w  .j  a va 2 s. co m
@Override
protected void onResume() {
    super.onResume();

    // Asks for Bluetooth activation is needed
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    // Asks for permissions if needed
    final int checkCoarseLocation = ContextCompat.checkSelfPermission(this,
            "android.permission.ACCESS_COARSE_LOCATION");
    final int checkFineLocation = ContextCompat.checkSelfPermission(this,
            "android.permission.ACCESS_FINE_LOCATION");
    if (checkCoarseLocation == PackageManager.PERMISSION_DENIED
            && checkFineLocation == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                MY_PERMISSIONS_REQUEST_COARSE_LOCATION);
    } else {
        // Ask for location service if needed
        if (needToEnableLocation(this)) {
            final Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(viewIntent);
        }
    }
}

From source file:com.hardwarebreakout.bleboosterdemo.DeviceScanActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    // Use this check to determine whether BLE is supported on the device.  Then you can
    // selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();//from ww  w  . ja  va  2 s  .  com
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
}

From source file:org.jraf.android.bikey.app.heartrate.bluetooth.BleScanListFragment.java

private boolean ensureBluetoothEnabled() {
    BluetoothManager bluetoothManager = (BluetoothManager) getActivity()
            .getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, REQUEST_ENABLE_BT);
        return false;
    }/*  w  w  w.  j  a va2 s  .com*/
    return true;
}

From source file:com.artur.softwareproject.BluetoothConnectionList.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_bluetooth_connection);

    ListView bluetoothList;//from   w w  w.  jav a  2s. c o  m
    bluetoothList = (ListView) findViewById(R.id.bluetoothList);

    ListAdapter = new BluetoothConnectionListAdapter(this, bluetoothAddress, bluetoothName, bDevices);

    bluetoothList.setAdapter(ListAdapter);

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

    bluetoothAdapter = bluetoothManager.getAdapter();

    mHandler = new Handler();

    File topLevelDir, jsonDir, objDir;

    //Creating the directory structure for the app.
    topLevelDir = new File(Environment.getExternalStorageDirectory() + "/ViSensor");

    jsonDir = new File(Environment.getExternalStorageDirectory() + "/ViSensor/Json");

    objDir = new File(Environment.getExternalStorageDirectory() + "/ViSensor/Obj");

    if (!topLevelDir.exists()) {
        if (!topLevelDir.mkdir()) {
            Log.d(TAG, "Creating top level directory failed.");
        }

        if (!jsonDir.exists()) {
            if (!jsonDir.mkdir()) {
                Log.d(TAG, "Creating json directory failed.");
            }
        }

        if (!objDir.exists()) {
            if (!objDir.mkdir()) {
                Log.d(TAG, "Creating obj directory failed.");
            }
        }
    } else if (!jsonDir.exists()) {
        if (!jsonDir.mkdir()) {
            Log.d(TAG, "Creating json directory failed.");
        }

        if (!objDir.exists()) {
            if (!objDir.mkdir()) {
                Log.d(TAG, "Creating obj directory failed.");
            }
        }
    } else if (!objDir.exists()) {
        if (!objDir.mkdir()) {
            Log.d(TAG, "Creating obj directory failed.");
        }
    }

    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        final int REQUEST_ENABLE_BT = 1;

        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    int permissionCheckLocation = ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION);

    int permissionCheckWrite = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permissionCheckLocation != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 0);
    }

    if (permissionCheckWrite != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }

    scanLeDevice(true);
}

From source file:ch.ethz.inf.vs.a1.fabischn.ble.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    // TODO when bluetooth is off, concurrently the onCreate keeps running.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_LONG).show();
        finish();//w  w w  .  jav a2 s . com
        return;
    }

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

    // TODO onActivityResult, check if Cancelled or not, then continue or leave
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        // REQUEST_ENABLE_BT will be returned as requestCode from onActivityResult
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    // TODO change this if onActivity Result is used
    // Continues scanning if enabled, else toast and finish
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    // TODO lookup the correct idiom for this, look at onRequestPermissionsResult
    // We need this for Android 6.0+
    if (checkLocationPermission()) {

    } else {
        ActivityCompat.requestPermissions(this,
                new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, 1);
    }

    if (mBluetoothAdapter != null) {
        mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    }
    ScanFilter.Builder filterBuilder = new ScanFilter.Builder();
    mScanFilter = filterBuilder.setDeviceName("Smart Humigadget").build();
    ScanSettings.Builder settingsBuilder = new ScanSettings.Builder();
    mScanSettings = settingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            //                .setMatchMode(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
            //                .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
            .build();
    mScanFilters = new ArrayList<ScanFilter>();
    mScanFilters.add(mScanFilter);

}

From source file:com.github.w666.ezonwatch.DeviceScanActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    // Use this check to determine whether BLE is supported on the device.  Then you can
    // selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();//from   w  w  w . java 2 s .  c o  m
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    if (!(Preferences.readDeviceAddress(getApplicationContext()) == null)
            && !(Preferences.readDeviceName(getApplicationContext()) == null)) {
        final Intent intent = new Intent(this, DeviceControlActivity.class);
        startActivity(intent);
    }
}

From source file:com.google.android.apps.forscience.ble.DeviceDiscoverer.java

protected DeviceDiscoverer(Context context) {
    mContext = context.getApplicationContext();
    BluetoothManager manager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = manager.getAdapter();
    mDevices = new ArrayMap<>();
}