下面列出了android.bluetooth.BluetoothGatt#connect ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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);
}
}
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;
}
public void connectToGatt(BluetoothGatt gatt) {
Timber.d( "connectToGatt:" + gatt.getDevice().getName());
gatt.connect();
}
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();
}
}