Example usage for android.bluetooth BluetoothManager getAdapter

List of usage examples for android.bluetooth BluetoothManager getAdapter

Introduction

In this page you can find the example usage for android.bluetooth BluetoothManager getAdapter.

Prototype

public BluetoothAdapter getAdapter() 

Source Link

Document

Get the default BLUETOOTH Adapter for this device.

Usage

From source file:ble.AndroidBle.java

public AndroidBle(BleService service) {
    mService = service;//from ww  w.j ava 2s  .c om
    // btQuery = BTQuery.getInstance();
    if (!mService.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        mService.bleNotSupported();
        return;
    }

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

    mBtAdapter = bluetoothManager.getAdapter();
    if (mBtAdapter == null) {
        mService.bleNoBtAdapter();
    }
    mBluetoothGatts = new HashMap<String, BluetoothGatt>();
}

From source file:com.mikroe.hexiwear_android.DeviceScanActivity.java

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

    mHandler = new Handler();
    mDeviceAddresses = new ArrayList<String>();
    mScanTitle = (TextView) findViewById(R.id.scanTitle);
    mDeviceAddresses.add(KWARP_ADDRESS);
    mDeviceAddresses.add(KWARP_ADDRESS_TWO);

    // 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();//  w  w w. ja  v  a  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;
    }

    // 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()) {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }

    //checkNotificationEnabled();

    LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));
    ComponentName name = startService(new Intent(DeviceScanActivity.this, NotificationService.class));

    registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());

    scanLeDevice(true);

    Intent gattServiceIntent = new Intent(DeviceScanActivity.this, BluetoothLeService.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}

From source file:com.karma.konnect.BeaconConfigFragment.java

private void initializeBluetooth() {
    // Initializes a Bluetooth adapter. For API version 18 and above,
    // get a reference to BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getActivity()
            .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: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);
    }// w w  w . j  a  v a2 s  .  com

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

From source file:fr.bmartel.android.notti.service.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 w  w.  j a v  a 2 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
                    && !scanningList.containsKey(device.getAddress())) {

                scanningList.put(device.getAddress(), device);

                try {
                    JSONObject object = new JSONObject();
                    object.put("address", device.getAddress());
                    object.put("deviceName", device.getName());

                    ArrayList<String> deviceInfo = new ArrayList<>();
                    deviceInfo.add(object.toString());

                    broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCOVERED, deviceInfo);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

/**
 * Create a new BLE driver for Android./*from www. j a  v a 2 s. co  m*/
 *
 * @param vContext Vanadium context.
 */
public Driver(VContext vContext) {
    mContext = V.getAndroidContext(vContext);
    if (mContext == null) {
        throw new IllegalStateException("AndroidContext not available");
    }
    mServices = new HashMap<>();

    BluetoothManager manager = ((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE));
    mBluetoothAdapter = manager.getAdapter();
    if (mBluetoothAdapter == null) {
        Log.w(TAG, "BluetoothAdapter not available");
        return;
    }

    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(mContext,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.w(TAG, "ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION not granted, "
                + "Bluetooth discovery will not be happening");
        return;
    }

    mContext.registerReceiver(new BluetoothAdapterStatusReceiver(),
            new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    if (!mBluetoothAdapter.isEnabled()) {
        // Prompt user to turn on Bluetooth.
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        enableBtIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(enableBtIntent);
        return;
    }
    resume();
}

From source file:pl.mrwojtek.sensrec.app.HeartRateDialog.java

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

    handler = new Handler(getContext().getMainLooper());
    adapter = new DevicesAdapter();

    SensorsRecorder recorder = RecordingService.getRecorder(getActivity());
    SortedMap<Integer, BleRecorder> bleRecorders = recorder.getBleRecorders();
    for (Map.Entry<Integer, BleRecorder> r : bleRecorders.entrySet()) {
        adapter.add(r.getValue());/*from  w  w  w  .ja  v a2 s .c  om*/
    }

    final BluetoothManager bluetoothManager = (BluetoothManager) getContext()
            .getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    if (!bluetoothAdapter.isEnabled()) {
        Log.w(TAG, "Bluetooth adapter not enabled");
        requestEnableBluetooth();
    } else {
        scanLeDevice(true);
    }
}

From source file:com.megster.cordova.ble.central.BLECentralPlugin.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();
    }//  www.j ava 2s .c  o m

    boolean validAction = true;

    if (action.equals(SCAN)) {

        UUID[] serviceUUIDs = parseServiceUUIDList(args.getJSONArray(0));
        int scanSeconds = args.getInt(1);
        findLowEnergyDevices(callbackContext, serviceUUIDs, scanSeconds);

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

        UUID[] serviceUUIDs = parseServiceUUIDList(args.getJSONArray(0));
        findLowEnergyDevices(callbackContext, serviceUUIDs, -1);

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

        bluetoothAdapter.stopLeScan(this);
        callbackContext.success();

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

        listKnownDevices(callbackContext);

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

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

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

        String macAddress = args.getString(0);
        disconnect(callbackContext, macAddress);

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

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        read(callbackContext, macAddress, serviceUUID, characteristicUUID);

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

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        byte[] data = args.getArrayBuffer(3);
        int type = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT;
        write(callbackContext, macAddress, serviceUUID, characteristicUUID, data, type);

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

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        byte[] data = args.getArrayBuffer(3);
        int type = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE;
        write(callbackContext, macAddress, serviceUUID, characteristicUUID, data, type);

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

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        registerNotifyCallback(callbackContext, macAddress, serviceUUID, characteristicUUID);

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

        String macAddress = args.getString(0);
        UUID serviceUUID = uuidFromString(args.getString(1));
        UUID characteristicUUID = uuidFromString(args.getString(2));
        removeNotifyCallback(callbackContext, macAddress, serviceUUID, characteristicUUID);

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

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

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

        String macAddress = args.getString(0);

        if (peripherals.containsKey(macAddress) && peripherals.get(macAddress).isConnected()) {
            callbackContext.success();
        } else {
            callbackContext.error("Not connected.");
        }

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

        Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
        cordova.getActivity().startActivity(intent);
        callbackContext.success();

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

        enableBluetoothCallback = callbackContext;
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH);

    } else {

        validAction = false;

    }

    return validAction;
}