Java Code Examples for android.bluetooth.bluetoothgatt#connect()
The following examples show how to use
android.bluetooth.bluetoothgatt#connect() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GattConnection.java From bitgatt with Mozilla Public License 2.0 | 5 votes |
private boolean ifGattHasBeenInstantiatedConnect(FitbitBluetoothDevice device) { if (isConnected()) { Timber.w("[%s] You can't connect while connected, or connecting, please know what you are doing here.", device); return true; } setState(GattState.CONNECTING); // keep in mind that this could go on for 90 seconds on some devices if (mockMode) { mockConnect(); return true; } /* we can re-use the listener here because all of our gatt instances are using the * same listener which calls back everyone, but will only notify the transaction instances * on the connection that are relevant using the device as a discriminator. */ BluetoothGatt localGatt = gatt; boolean success = false; if (localGatt != null) { try { success = localGatt.connect(); } catch (NullPointerException e) { Timber.e(e); setState(GattState.FAILURE_CONNECTING_WITH_SYSTEM_CRASH); return false; } } if (!success) { setState(GattState.FAILURE_CONNECTING); } return success; }
Example 2
Source File: DfuBaseService.java From microbit with Apache License 2.0 | 5 votes |
private void gattConnect(final BluetoothGatt gatt) { try { if (gatt.connect()) { synchronized (mLock) { while (mConnectionState != STATE_CONNECTED_AND_READY && mError == 0) { mLock.wait(30 * 1000); //Wait only 30 seconds. TODO Check this again } } } } catch (final InterruptedException e) { e.printStackTrace(); loge("Sleeping interrupted", e); } }
Example 3
Source File: MyBleService.java From science-journal with Apache License 2.0 | 5 votes |
public boolean connect(String address) { if (btAdapter == null) { // Not sure how we could get here, but it happens (b/36738130), so flag an error // instead of crashing. return false; } BluetoothDevice device = btAdapter.getRemoteDevice(address); // Explicitly check if Ble is enabled, otherwise it attempts a connection // that never timesout even though it should. if (device == null || !isBleEnabled()) { return false; } BluetoothGatt bluetoothGatt = addressToGattClient.get(address); int connectionState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT); if (bluetoothGatt != null && connectionState != BluetoothProfile.STATE_CONNECTED) { return bluetoothGatt.connect(); } if (bluetoothGatt != null && connectionState == BluetoothProfile.STATE_CONNECTED) { sendGattBroadcast(address, BleEvents.GATT_CONNECT, null); return true; } if (bluetoothGatt != null) { bluetoothGatt.close(); } bluetoothGatt = device.connectGatt( this, false, // autoConnect = false gattCallbacks); addressToGattClient.put(address, bluetoothGatt); return true; }
Example 4
Source File: BluetoothUtilImpl.java From android-ponewheel with MIT License | 4 votes |
public void connectToGatt(BluetoothGatt gatt) { Timber.d( "connectToGatt:" + gatt.getDevice().getName()); gatt.connect(); }
Example 5
Source File: BluetoothUtilImpl.java From android-ponewheel with MIT License | 4 votes |
private void onOWStateChangedToDisconnected(BluetoothGatt gatt, Context context) { //TODO: we really should have a BluetoothService we kill and restart Timber.i("We got disconnected from our Device: " + gatt.getDevice().getAddress()); if (Looper.myLooper() == null) { Looper.prepare(); } Toast.makeText(mainActivity, "We got disconnected from our device: " + gatt.getDevice().getAddress(), Toast.LENGTH_SHORT).show(); mainActivity.deviceConnectedTimer(false); mOWDevice.isConnected.set(false); App.INSTANCE.releaseWakeLock(); mScanResults.clear(); if (App.INSTANCE.getSharedPreferences().shouldAutoReconnect()) { mRetryCount++; Timber.i("mRetryCount=" + mRetryCount); try { if (mRetryCount == 20) { Timber.i("Reached too many retries, stopping search"); gatt.close(); stopScanning(); disconnect(); //mainActivity.invalidateOptionsMenu(); } else { Timber.i("Waiting for 5 seconds until trying to connect to OW again."); TimeUnit.SECONDS.sleep(5); Timber.i("Trying to connect to OW at " + mOWDevice.deviceMacAddress.get()); //BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mOWDevice.deviceMacAddress.get()); //connectToDevice(device); gatt.connect(); } } catch (InterruptedException e) { Timber.d("Connection to OW got interrupted:" + e.getMessage()); } } else { gatt.close(); mainActivity.invalidateOptionsMenu(); } }