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:no.nordicsemi.android.BLE.profile.BleProfileServiceReadyActivity.java

private void setBluetoothAdapter() {
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
}

From source file:com.example.RITW.Ble.BleProfileServiceReadyActivity.java

protected boolean isBLEEnabled() {
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    final BluetoothAdapter adapter = bluetoothManager.getAdapter();
    return adapter != null && adapter.isEnabled();
}

From source file:undot.safedrivers.BLE.BluetoothLeService.java

/**
 * Initializes a reference to the local Bluetooth adapter.
 *
 * @return Return true if the initialization is successful.
 *///from w w  w  . ja v  a2s . co  m
public boolean initialize() {
    // For API level 18 and above, get a reference to BluetoothAdapter through
    // BluetoothManager.
    System.out.println("BluetoothLeService initialize" + mBluetoothManager);
    if (mBluetoothManager == null) {
        mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager == null) {
            Log.e(TAG, "Unable to initialize BluetoothManager.");
            return false;
        }
    }

    mBluetoothAdapter = mBluetoothManager.getAdapter();
    if (mBluetoothAdapter != null) {
        mBluetoothAdapter.startLeScan(mLeScanCallback);

    } else {
        Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
        return false;
    }

    return true;
}

From source file:com.android.launcher3.widget.DigitalAppWidgetProvider.java

private void refreshBtStatus(Context context, RemoteViews widget) {
    bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothApapter = bluetoothManager.getAdapter();
    if (bluetoothApapter != null) {
        int btStatus = bluetoothApapter.getState();
        if (BluetoothAdapter.STATE_ON == btStatus || BluetoothAdapter.STATE_TURNING_ON == btStatus) {
            widget.setImageViewResource(R.id.bt, R.drawable.status_bt_on);
        } else {//from   w  ww.ja  v a  2 s. com
            widget.setImageViewResource(R.id.bt, R.drawable.ic_qs_bluetooth_off_sprd);
        }
    }
}

From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java

private boolean enableBluetoothConnectivity() {
    Log.d(TAG, "EnableBluetoothConnectivity");
    if (mBluetoothManager == null) {
        mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager == null) {
            Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothManager");
            return false;
        }// w w  w . j av a  2  s  . c o  m
    }

    mBluetoothAdapter = mBluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothAdapter");
        return false;
    }

    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    if (mBluetoothLeScanner == null) {
        Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothLeScanner");
        return false;
    }

    // The first auto-connection after boot might be missed due to starting time of the updater service.
    List<BluetoothDevice> connectedDevices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
    for (BluetoothDevice device : connectedDevices) {
        if (isTargetKeyboard(device) && !isUpdateServiceInUse()) {
            Log.d(TAG, "enableBluetoothConnectivity: Found keyboard " + device.getName() + " ["
                    + device.getAddress() + "] connected");
            obtainKeyboardInfo(device.getName(), device.getAddress());
            break;
        }
    }

    return true;
}

From source file:com.umundus.service.NCallServiceOld.java

/**
 * Initializes a reference to the local Bluetooth adapter.
 *
 * @return Return true if the initialization is successful.
 *//*from www . j  a v  a2 s .co  m*/
public boolean initialize() {
    // For API level 18 and above, get a reference to BluetoothAdapter through
    // BluetoothManager.
    Log.d(TAG, "[*]Service-initialize#1");
    if (mBluetoothManager == null) {
        mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager == null) {
            Log.e(TAG, "[*]Unable to initialize BluetoothManager.");
            return false;
        }
    }

    Log.d(TAG, "[*]Service-initialize#2");
    mBluetoothAdapter = mBluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        Log.e(TAG, "[*]Unable to obtain a BluetoothAdapter.");
        return false;
    }

    return true;
}

From source file:br.liveo.ndrawer.ui.activity.MainActivity.java

License:asdf

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override/* w  w  w.j a  v  a2  s  .  c o m*/
public void onInt(Bundle savedInstanceState) {
    // User Information
    this.userName.setText("DoubleS");
    this.userEmail.setText("onlyboys@kaist.ac.kr");
    this.userPhoto.setImageResource(R.drawable.ic_rudsonlive);
    this.userBackground.setImageResource(R.drawable.ic_user_background_first);

    // Creating items navigation
    mHelpLiveo = new HelpLiveo();
    mHelpLiveo.add("", R.drawable.tracking);
    //     mHelpLiveo.add("", R.drawable.recommend);
    mHelpLiveo.add("?", R.drawable.steal);
    mHelpLiveo.add("?", R.drawable.enroll);
    mHelpLiveo.add("", R.drawable.setting);

    with(this).startingPosition(startingPosition) //Starting position in the list
            .addAllHelpItem(mHelpLiveo.getHelp())

            .colorItemSelected(R.color.nliveo_blue_colorPrimary)
            .colorNameSubHeader(R.color.nliveo_blue_colorPrimary)

            .setOnClickUser(onClickPhoto).setOnPrepareOptionsMenu(onPrepare).build();

    int position = this.getCurrentPosition();
    this.setElevationToolBar(position != 2 ? 15 : 0);

    //-- shobeat_add 20/Oct/15
    // BLE
    BluetoothManager LEManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = LEManager.getAdapter();

    // BLE Callback method
    /* mLeScanCallback = new BluetoothAdapter.LeScanCallback(){
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord){
        //String msg = "NAME=" + device.getName() + "ADDRESS=" + device.getAddress() + "\nRSSI=" + rssi;
        //Log.d("BLE", msg);
        // TODO: local? arraylist   ?
        for(int i = 0; i < mDeviceList.size(); i++){
            if(device.getAddress().equals(mDeviceList.get(i).getBdAddr()))
            {
                mDeviceList.get(i).setRssi(rssi);
                mDeviceList.get(i).setTxPower((int)scanRecord[29]);
                return;
            }
        }
        // TODO:  ?  ? ?  rssi ??
        BeaconDevice newDevice = new BeaconDevice(device.getAddress(), device.getName(), rssi, (int)scanRecord[29]);
        mDeviceList.add(newDevice);
    }
     };
            
     startTimerTask();*/
    //-- shobeat_end
}

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

/**
 * Initializes a reference to the local Bluetooth adapter.
 *
 * @return Return true if the initialization is successful.
 *///ww  w . j  a  v  a 2  s . co  m
@SuppressLint("NewApi")
public IoTResultCodes initialize() {

    // 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();
        return IoTResultCodes.BLE_NOT_SUPPORTED;
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    if (mBluetoothManager == null) {
        mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager == null) {
            Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Unable to initialize BluetoothManager.");
            return IoTResultCodes.BLUETOOTH_NOT_SUPPORTED;
        }
    }
    mBluetoothAdapter = mBluetoothManager.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();
        Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
        //finish();
        return IoTResultCodes.BLUETOOTH_NOT_SUPPORTED;
    }
    // Ensures Bluetooth is enabled on the device.  If Bluetooth is not currently enabled,
    // fire an intent to display a dialog asking the user to grant permission to enable it.
    if (!mBluetoothAdapter.isEnabled()) {
        return IoTResultCodes.BLUETOOTH_NOT_ENABLED;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mLeScanLollipopCallback = new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                //super.onScanResult(callbackType, result);

                try {
                    BluetoothDevice device = result.getDevice();
                    byte[] scanRecord = result.getScanRecord().getBytes();
                    final HashMap<Integer, AdRecord> adRecords = AdRecord.parseScanRecord(scanRecord);

                    /**
                     * UUID  ? .
                     */
                    ArrayList<String> scannedUuids = DataParser.getUuids(adRecords);
                    if (scannedUuids == null || scannedUuids.size() == 0) {
                        Log.e(TAG, ">>> xx device name " + device.getAddress() + " has no uuid advertisement");
                        return;
                    }

                    IoTDevice iotDevice = new IoTDevice();
                    iotDevice.setDeviceId(device.getAddress());
                    iotDevice.setDeviceName(device.getName());
                    iotDevice.setDeviceType(IoTDevice.DEVICE_TYPE_STRING_BT);

                    iotDevice.setUuids(scannedUuids);
                    iotDevice.setUuidLen(DataParser.getUuidLength(adRecords));

                    iotDevice.setAdRecordHashMap(adRecords);
                    mTempScanedList.put(iotDevice.getDeviceId(), iotDevice);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onBatchScanResults(List<ScanResult> results) {
                //super.onBatchScanResults(results);
                Log.d(TAG, "mScanCallback.. onBatchScanResults");
            }

            @Override
            public void onScanFailed(int errorCode) {
                //super.onScanFailed(errorCode);
                Log.d(TAG, "mScanCallback.. onScanFailed");
            }
        };
    } else {
        mLeScanKitkatCallback = new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                Log.d(TAG, ">> mLeScanKitkatCallback..");

                try {
                    final HashMap<Integer, AdRecord> adRecords = AdRecord.parseScanRecord(scanRecord);
                    /**
                     * UUID  ? .
                     */
                    ArrayList<String> scannedUuids = DataParser.getUuids(adRecords);
                    if (scannedUuids == null || scannedUuids.size() == 0) {
                        Log.e(TAG, ">>> xx device name " + device.getAddress() + " has no uuid advertisement");
                        return;
                    }

                    IoTDevice iotDevice = new IoTDevice();
                    iotDevice.setDeviceId(device.getAddress());
                    iotDevice.setDeviceName(device.getName());
                    iotDevice.setDeviceType(IoTDevice.DEVICE_TYPE_STRING_BT);

                    iotDevice.setUuids(scannedUuids);
                    iotDevice.setUuidLen(DataParser.getUuidLength(adRecords));

                    iotDevice.setAdRecordHashMap(adRecords);
                    mTempScanedList.put(iotDevice.getDeviceId(), iotDevice);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }

    return IoTResultCodes.SUCCESS;
}

From source file:com.nbplus.iotapp.service.IoTService.java

/**
 * ??  ? ,  enable  ? ? ?? .//from w  w w.j av a  2s .c o m
 */
public IoTResultCodes checkBluetoothEnabled() {
    // 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)) {
        return IoTResultCodes.BLE_NOT_SUPPORTED;
    }

    // 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);
    final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (bluetoothAdapter == null) {
        return IoTResultCodes.BLUETOOTH_NOT_SUPPORTED;
    }

    // Ensures Bluetooth is enabled on the device.  If Bluetooth is not currently enabled,
    // fire an intent to display a dialog asking the user to grant permission to enable it.
    if (!bluetoothAdapter.isEnabled()) {
        return IoTResultCodes.BLUETOOTH_NOT_ENABLED;
    }

    mLastConnectionStatus = true;
    return IoTResultCodes.SUCCESS;
}

From source file:com.paywith.ibeacon.service.IBeaconService.java

@TargetApi(18)
private BluetoothAdapter getBluetoothAdapter() {
    if (android.os.Build.VERSION.SDK_INT < 18) {
        Log.w(TAG, "Not supported prior to API 18.");
        return null;
    }/* w w  w . j a v a 2s.c  o m*/
    if (bluetoothAdapter == null) {
        // Initializes Bluetooth adapter.
        final BluetoothManager bluetoothManager = (BluetoothManager) this.getApplicationContext()
                .getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
    }
    return bluetoothAdapter;
}