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:com.a3did.partner.recosample.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_reco);
    mLayout = findViewById(R.id.mainLayout);

    View includedLayout = (View) findViewById(R.id.reco_settings_values);
    TextView mRecoOnlyText = (TextView) includedLayout.findViewById(R.id.recoRecoonlySetting);
    if (SCAN_RECO_ONLY) {
        mRecoOnlyText.setText(R.string.settings_result_true);
    } else {//from w  w w .  j a va2s  . com
        mRecoOnlyText.setText(R.string.settings_result_false);
    }

    TextView mDiscontinuousText = (TextView) includedLayout.findViewById(R.id.recoDiscontinouosSetting);
    if (DISCONTINUOUS_SCAN) {
        mDiscontinuousText.setText(R.string.settings_result_true);
    } else {
        mDiscontinuousText.setText(R.string.settings_result_false);
    }

    TextView mBackgroundRangingTimeoutText = (TextView) includedLayout
            .findViewById(R.id.recoBackgroundtimeoutSetting);
    if (ENABLE_BACKGROUND_RANGING_TIMEOUT) {
        mBackgroundRangingTimeoutText.setText(R.string.settings_result_true);
    } else {
        mBackgroundRangingTimeoutText.setText(R.string.settings_result_false);
    }

    //If a user device turns off bluetooth, request to turn it on.
    //?  ?? .
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = mBluetoothManager.getAdapter();

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
    }

    /**
     * In order to use RECO SDK for Android API 23 (Marshmallow) or higher,
     * the location permission (ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION) is required.
     * Please refer to the following permission guide and sample code provided by Google.
     *
     * ? API 23 ()?? , ?? RECO SDK  
     *   (ACCESS_COARSE_LOCATION ? ACCESS_FINE_LOCATION)?  .
     *  ? , ?  ?  ?.
     *
     * http://www.google.com/design/spec/patterns/permissions.html
     * https://github.com/googlesamples/android-RuntimePermissions
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.i("MainActivity",
                    "The location permission (ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION) is not granted.");
            this.requestLocationPermission();
        } else {
            Log.i("MainActivity",
                    "The location permission (ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION) is already granted.");
        }
    }
}

From source file:com.orange.beaconme_sdk.control.BeaconMonitor.java

/**
 * Check if Bluetooth is enabled. If it is enabled then start scanner, ask to enable Bluetooth otherwise.
 *//*from   w w  w  .j  ava  2  s  . co m*/
private void startScan() {
    BluetoothAdapter btAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE))
            .getAdapter();
    if (btAdapter == null || !btAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        enableBtIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(enableBtIntent);
    } else {
        context.sendBroadcast(new Intent(BLEDeviceScanner.START_SCAN_SERVICE_ACTION));
    }
}

From source file:com.github.akinaru.bleanalyzer.bluetooth.BluetoothCustomManager.java

@SuppressLint("NewApi")
public void init(Context context) {

    // Initializes Bluetooth adapter.
    final BluetoothManager bluetoothManager = (BluetoothManager) context
            .getSystemService(Context.BLUETOOTH_SERVICE);

    mBluetoothAdapter = bluetoothManager.getAdapter();

    //init message handler
    mHandler = null;/*from   w  ww .jav  a2 s.  c o  m*/
    mHandler = new Handler();

    scanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, final byte[] scanRecord) {

            if (device.getAddress() != null && device.getName() != null) {

                if (device.getName().equals("RFdroid")) {
                    dispatchRFdroid(device, rssi, scanRecord);
                } else {
                    dispatchBtDevices(device, rssi, scanRecord);
                }
            }
        }
    };
}

From source file:dev.coatl.co.urband_basic.presenter.activities.StartSearchDevice.java

private void setResources() {
    handler = new Handler();
    listDevices = (ListView) findViewById(R.id.urband_device_list);
    listDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override/*from  www .  j  av  a2  s. c  om*/
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final BluetoothDevice device = ((BluetoothUrbandDeviceItem) parent.getItemAtPosition(position))
                    .getDevice();
            if (device == null) {
                return;
            }
            intent = new Intent(getApplicationContext(), ActivityHomePanel.class);
            intent.putExtra(EXTRAS_DEVICE_NAME, device.getName());
            intent.putExtra(EXTRAS_DEVICE_ADDRESS, device.getAddress());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (scanning) {
                bluetoothAdapter.stopLeScan(scanDevices);
                scanning = false;
            }
            startActivity(intent);
        }
    });
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, "Este dispositivo no soporta Bluetooth", Toast.LENGTH_SHORT).show();
        finish();
    }
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
}

From source file:com.marekscholtz.bluetify.utilities.BluetoothChatService.java

private synchronized void connected(BluetoothSocket socket) {
    if (mAcceptThread != null) {
        mAcceptThread.cancel();/*from  w  ww  .j  a  v  a  2s .  c o m*/
        mAcceptThread = null;
    }
    if (mConnectThread != null) {
        mConnectThread.cancel();
        mConnectThread = null;
    }
    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }
    mConnectedThread = new ConnectedThread(socket);
    mConnectedThread.start();
    BluetoothAdapter bluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE))
            .getAdapter();

    if (mSharedPreferences == null) {
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    }

    byte[] usernameBytes = ("&u" + mSharedPreferences.getString("username", bluetoothAdapter.getName()) + "&p")
            .getBytes();
    if (mSharedPreferences.getString("profile_photo", null) == null) {
        write(usernameBytes);
    } else {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            outputStream.write(usernameBytes);
            outputStream
                    .write(Base64.decode(mSharedPreferences.getString("profile_photo", null), Base64.DEFAULT));
        } catch (IOException e) {
            e.printStackTrace();
        }
        write(outputStream.toByteArray());
    }
}

From source file:au.com.smarttrace.beacons.BluetoothService.java

/** Init bluetooth adapter */
private void initBluetooth() {
    if (btManager == null)
        btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    btAdapter = btManager.getAdapter();//from   ww  w.  j a v a 2s .c om

    ScanSettings.Builder builder = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED)
            .setReportDelay(0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        builder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
    btSettings = builder.build();

    btFilters = new ArrayList<>();
}

From source file:org.physical_web.physicalweb.FatBeaconBroadcastService.java

@Override
public void onCreate() {
    super.onCreate();
    mBluetoothLeAdvertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    mNotificationManager = NotificationManagerCompat.from(this);
    mBluetoothManager = (BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE);
}

From source file:it.prof.iotsemplicedemo.DeviceScanActivity.java

private void InizializeBluetooth() {

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }//from w  ww  . j  ava2s  .c  om

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

From source file:com.agustinprats.myhrv.MainActivity.java

/** Returns true if bluetooth is enabled. False otherwise. */
public boolean isBluetoothEnabled() {

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

    return bluetoothManager.getAdapter().isEnabled();
}

From source file:com.github.google.beaconfig.ScanningActivity.java

private void setupScanner() {
    Log.d(TAG, "Setting up scanner...");
    BluetoothManager manager = (BluetoothManager) getApplicationContext()
            .getSystemService(Context.BLUETOOTH_SERVICE);
    btAdapter = manager.getAdapter();//from ww w.  j  av  a  2s  .c  o m

    requestBluetoothOn();
}