void ConnectWithAddress(final String address) { new BLEAction("ConnectWithAddress") { @Override public Void action() { try { //turn off the scanner if it is not already off if (mBluetoothLeAdvertisementScanner != null) { mBluetoothLeAdvertisementScanner.stopScan(mDeviceScanCallback); isScanning = false; } //if device list is not empty, try something if (!mLeDevices.isEmpty()) { for (BluetoothDevice bluetoothDevice : mLeDevices) { if (bluetoothDevice.getAddress().equals(address)) { forceDisconnect(); mBluetoothGatt = bluetoothDevice.connectGatt(activity, autoReconnect, mGattCallback); if (mBluetoothGatt != null) { //TODO(Will): Delete the puts to this map gattMap.put(bluetoothDevice.getAddress(), mBluetoothGatt); scheduleConnectionTimeoutMessage(); Log.i(LOG_TAG, "ConnectWithAddress successful."); return null; } else { outer.ConnectionFailed("Connect failed to return a BluetoothGatt object"); Log.e(LOG_TAG, "ConnectWithAddress failed."); } } } outer.ConnectionFailed("No device found with address " + address); } else { //check if the given address is valid if(BluetoothAdapter.checkBluetoothAddress(address)){ //populate a BluetoothDevice with the given address BluetoothAdapter btAdapter1 = obtainBluetoothAdapter(); BluetoothDevice bluetoothDevice1 = btAdapter1.getRemoteDevice(address); //connection routine forceDisconnect(); mBluetoothGatt = bluetoothDevice1.connectGatt(activity, autoReconnect, mGattCallback); if (mBluetoothGatt != null) { //TODO(Will): Delete the puts to this map gattMap.put(bluetoothDevice1.getAddress(), mBluetoothGatt); scheduleConnectionTimeoutMessage(); Log.i(LOG_TAG, "ConnectWithAddress successful."); return null; } else { outer.ConnectionFailed("Connect failed to return a BluetoothGatt object"); Log.e(LOG_TAG, "ConnectWithAddress failed."); } }else{ outer.ConnectionFailed("Invalid Connection Address"); Log.e(LOG_TAG, "ConnectWithAddress failed."); } } } catch (IndexOutOfBoundsException e) { outer.ConnectionFailed(e.getMessage()); signalError("ConnectWithAddress", ERROR_INDEX_OUT_OF_BOUNDS, "ConnectWithAddress", "DeviceList"); } return null; } }.run(); }