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:com.example.RITW.Ble.BleProfileService.java

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS))
        throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key");

    final Uri logUri = intent.getParcelableExtra(EXTRA_LOG_URI);
    //      mLogSession = Logger.openSession(getApplicationContext(), logUri);
    mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);

    //      Logger.i(mLogSession, "Service started");

    // notify user about changing the state to CONNECTING
    final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
    broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING);
    LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast);

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    final BluetoothAdapter adapter = bluetoothManager.getAdapter();
    final BluetoothDevice device = adapter.getRemoteDevice(mDeviceAddress);
    mDeviceName = device.getName();/*from  www .  j  a  v a 2s . co m*/
    onServiceStarted();

    //      Logger.v(mLogSession, "Connecting...");
    mBleManager.connect(BleProfileService.this, device);
    return START_REDELIVER_INTENT;
}

From source file:no.android.proxime.profile.BleProfileService.java

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS))
        throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key");

    final Uri logUri = intent.getParcelableExtra(EXTRA_LOG_URI);
    mLogSession = Logger.openSession(getApplicationContext(), logUri);
    mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);

    Logger.i(mLogSession, "Service started");

    // notify user about changing the state to CONNECTING
    final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
    broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING);
    LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast);

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    final BluetoothAdapter adapter = bluetoothManager.getAdapter();
    final BluetoothDevice device = adapter.getRemoteDevice(mDeviceAddress);
    mDeviceName = device.getName();// w w  w  .j  a  va 2 s  .co m
    onServiceStarted();

    Logger.v(mLogSession, "Connecting...");
    mBleManager.connect(BleProfileService.this, device);
    return START_REDELIVER_INTENT;
}

From source file:org.bart452.runningshoesensor.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestPermission();//  ww  w. j ava2 s . c  o m
    context = getApplicationContext();

    mHeaderImageView = (ImageView) findViewById(R.id.appBarIv);
    Picasso.with(context).load(R.drawable.sunset_road_landscape).into(mHeaderImageView);

    // Toolbar
    mToolbar = (Toolbar) findViewById(R.id.toolBar);
    setSupportActionBar(mToolbar);
    mCollapsingTb = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
    mCollapsingTb.setTitle("Shoe sensor");
    mCollapsingTb.setExpandedTitleGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);

    // Bluetooth
    final BluetoothManager mBleMan = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBleAdapter = mBleMan.getAdapter();
    mBleScanner = mBleAdapter.getBluetoothLeScanner();
    mCharList = new ArrayList<>();
    mBleSem = new Semaphore(1);
    mBleThread = new BleThread(mBleSem);
    mBleThread.start();

    mBleReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
                if (mBleAdapter.getState() == BluetoothAdapter.STATE_ON) {
                    Log.d(LOG_TAG, "BT ON");
                    mScanFab.setEnabled(true);
                    if (mSnackBar != null) {
                        if (mSnackBar.isShown())
                            mSnackBar.dismiss();
                    }
                } else {
                    mScanFab.setEnabled(false);
                    bleEnable();
                }
            }
        }
    };

    // Text
    mDevNameTv = (TextView) findViewById(R.id.devNameTv);
    mDevNameTv.setText(getString(R.string.device_name) + " No device found");
    mRssiTv = (TextView) findViewById(R.id.rssiTv);
    mAddrTv = (TextView) findViewById(R.id.devAddrTv);

    // Buttons and switches
    mScanFab = (FloatingActionButton) findViewById(R.id.scanFab);
    mScanFab.setOnClickListener(this);
    mConnSwitch = (Switch) findViewById(R.id.connSwitch);
    mConnSwitch.setOnClickListener(this);

    //Graph
    mDataGraph = (GraphView) findViewById(R.id.dataGraph);
    mDataXSeries = new LineGraphSeries<>();
    mDataYSeries = new LineGraphSeries<>();
    mDataYSeries.setBackgroundColor(Color.RED);
    mDataGraph.addSeries(mDataXSeries);
    mDataGraph.addSeries(mDataYSeries);
    mDataGraph.getViewport().setXAxisBoundsManual(true);
    mDataGraph.getViewport().setMinX(0);
    mDataGraph.getViewport().setMaxX(20);

    // Animation
    mRotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    mRotateAnim.setDuration(SCAN_PERIOD);
    mRotateAnim.setInterpolator(new LinearInterpolator());
    bleEnable();
}

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/* w  w  w  .j a  v a2  s  .c  o  m*/
        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.diy.blelib.profile.BleProfileService.java

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS))
        throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key");

    mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
    Log.i(TAG, "Service started");
    //?????/* w ww .j a v a2 s. co  m*/
    if (!mBlutoothAdapter.isEnabled())
        mBlutoothAdapter.enable();
    // notify user about changing the state to CONNECTING
    final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
    broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING);
    LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast);

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    final BluetoothAdapter adapter = bluetoothManager.getAdapter();
    final BluetoothDevice device = adapter.getRemoteDevice(mDeviceAddress);
    mDeviceName = device.getName();
    onServiceStarted();

    Log.v(TAG, "Connecting...");
    mBleManager.connect(BleProfileService.this, device);
    return START_REDELIVER_INTENT;
}

From source file:top.isense.demo.testsensor.DeviceScanActivity.java

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

    initial_naviview(this);
    //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 ww .  jav  a2s.  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;
    }

}

From source file:com.robotic.goldenridge.blecontroller.DeviceScanActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getSupportActionBar().setTitle(R.string.title_devices);
    // 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 (Utility.isLE()) {
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
            finish();//from  ww  w. j  a  va2s  . 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.bt_support_err, Toast.LENGTH_SHORT).show();
        finish();
        return;
    } else {
        //les = mBluetoothAdapter.getBluetoothLeScanner();
    }
}

From source file:com.amti.vela.bluetoothlegatt.bluetooth.DeviceScanActivity.java

void btInit() {
    // 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();

    //with the swipe refresh, we need to manually set refresh to true through a runnable on app start or it doesn't work
    swipeContainer.post(new Runnable() {
        @Override/*from   w  ww .  j a va  2 s. co  m*/
        public void run() {
            swipeContainer.setRefreshing(true);
        }
    });
}

From source file:com.bluewatcher.activity.BlueWatcherActivity.java

private boolean isBluetoothEnabled() {
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

    if (bluetoothAdapter == null) {
        Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_LONG).show();
        finish();/*from w  w w.j  av a 2s  . co  m*/
    }

    boolean enabled = true;
    if (!bluetoothAdapter.isEnabled()) {
        enabled = false;
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, ENABLE_BLUETOOTH);
    }
    return enabled;
}

From source file:com.ibm.pi.beacon.PIBeaconSensor.java

private void initBluetoothAdapter() throws Exception {
    if (mBluetoothAdapter == null) {
        final BluetoothManager bluetoothManager = (BluetoothManager) mContext
                .getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        if (mBluetoothAdapter == null)
            throw new Exception("Failed to get bluetooth adapter");
    }//  w w w  . j av  a 2 s  .c o  m
}