Skip to content

Commit

Permalink
Bluetooth Detector Fix (#18)
Browse files Browse the repository at this point in the history
* Bluetooth Detector Fix

* Try new travis yml
  • Loading branch information
kbabcockuf committed Jun 2, 2019
1 parent 0a317cb commit a6d2d14
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -12,7 +12,9 @@ licenses:
- 'android-sdk-license-.+'

before_install:
- yes | sdkmanager "platforms;android-27"
- mkdir "$ANDROID_HOME/licenses" || true
- echo d56f5187479451eabf01fb78af6dfcb131a6481e > "$ANDROID_HOME/licenses/android-sdk-license"
- echo 24333f8a63b6825ea9c5514f83c2829b004d1fee >> "$ANDROID_HOME/licenses/android-sdk-license"

jdk:
- oraclejdk8
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -13,7 +13,7 @@
# org.gradle.parallel=true

GROUP=com.uber.rxcentralble
VERSION_NAME=1.0.5-SNAPSHOT
VERSION_NAME=1.0.6-SNAPSHOT
POM_DESCRIPTION=RxCentralBle
POM_URL=https://github.com/uber/RxCentralBle/
POM_SCM_URL=https://github.com/uber/RxCentralBle/
Expand Down
Expand Up @@ -6,9 +6,11 @@
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.uber.rxcentralble.BluetoothDetector;
import com.uber.rxcentralble.ConnectionManager;
import com.uber.rxcentralble.GattManager;
import com.uber.rxcentralble.RxCentralLogger;
Expand All @@ -18,9 +20,11 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import timber.log.Timber;

Expand All @@ -29,6 +33,7 @@

public class MainActivity extends AppCompatActivity {

private BluetoothDetector bluetoothDetector;
private ConnectionManager connectionManager;
private GattManager gattManager;

Expand All @@ -41,6 +46,7 @@ public class MainActivity extends AppCompatActivity {
@BindView(R.id.buttonConnect)
Button connectButton;

Disposable bluetoothDetection;
Disposable connection;

@Override
Expand All @@ -50,6 +56,7 @@ protected void onCreate(Bundle savedInstanceState) {

ButterKnife.bind(this);

bluetoothDetector = ((SampleApplication) getApplication()).getBluetoothDetector();
connectionManager = ((SampleApplication) getApplication()).getConnectionManager();
gattManager = ((SampleApplication) getApplication()).getGattManager();

Expand Down Expand Up @@ -195,4 +202,25 @@ public void getMtu() {
mtu -> Timber.i("MTU: success: " + mtu),
error -> Timber.i("MTU: error: " + error.getMessage()));
}

@OnCheckedChanged(R.id.toggleButtonBleDetect)
public void bleDetect(CompoundButton button, boolean checked) {
if (checked && bluetoothDetection == null) {
Timber.i("Bluetooth Detection Active");
bluetoothDetection = new CompositeDisposable();
bluetoothDetection = bluetoothDetector
.enabled()
.subscribe(enabled -> {
if (enabled) {
Timber.i("Bluetooth Enabled");
} else {
Timber.i("Bluetooth Disabled");
}
});
} else if (bluetoothDetection != null) {
bluetoothDetection.dispose();
bluetoothDetection = null;
Timber.i("Bluetooth Detection Deactivated");
}
}
}
@@ -1,6 +1,10 @@
package com.uber.rxcentralble.sample;

import android.annotation.TargetApi;
import android.app.Application;
import android.os.Build;

import com.uber.rxcentralble.BluetoothDetector;
import com.uber.rxcentralble.ConnectionManager;
import com.uber.rxcentralble.GattManager;
import com.uber.rxcentralble.Utils;
Expand All @@ -26,17 +30,24 @@ public class SampleApplication extends Application {

private ConnectionManager connectionManager;
private GattManager gattManager;
private BluetoothDetector bluetoothDetector;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onCreate() {
super.onCreate();

bluetoothDetector = new CoreBluetoothDetector(this.getApplicationContext());
gattManager = new CoreGattManager();
connectionManager = new CoreConnectionManager(this,
new CoreBluetoothDetector(this),
new CoreGattIO.Factory());
}

public BluetoothDetector getBluetoothDetector() {
return bluetoothDetector;
}

public ConnectionManager getConnectionManager() {
return connectionManager;
}
Expand Down
15 changes: 14 additions & 1 deletion rx-central-ble-sample/src/main/res/layout/activity_main.xml
Expand Up @@ -52,7 +52,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/batteryButton" />
app:layout_constraintTop_toBottomOf="@+id/toggleButtonBleDetect" />

<Button
android:id="@+id/batteryButton"
Expand Down Expand Up @@ -84,4 +84,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nameEditText" />

<ToggleButton
android:id="@+id/toggleButtonBleDetect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:text="ToggleButton"
android:textOff="BLE Detect Off"
android:textOn="BLE Detect On"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/gapButton" />

</android.support.constraint.ConstraintLayout>
Expand Up @@ -47,7 +47,7 @@ public CoreBluetoothDetector(Context context) {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

if (RxCentralLogger.isDebug()) {
Expand Down Expand Up @@ -100,7 +100,13 @@ private void startDetection() {
}

private void stopDetection() {
context.unregisterReceiver(bluetoothStateReceiver);
try {
context.unregisterReceiver(bluetoothStateReceiver);
} catch (IllegalArgumentException e) {
if (RxCentralLogger.isError()) {
RxCentralLogger.error("stopDetection - Unregister receiver failed!");
}
}

bluetoothEnabledRelay.accept(Capability.UNSUPPORTED);
}
Expand Down

0 comments on commit a6d2d14

Please sign in to comment.