Skip to content

Instantly share code, notes, and snippets.

@EdsonAlcala
Created October 19, 2017 16:22
Show Gist options
  • Select an option

  • Save EdsonAlcala/659a5aba65823e300e8b6620d7ed4a62 to your computer and use it in GitHub Desktop.

Select an option

Save EdsonAlcala/659a5aba65823e300e8b6620d7ed4a62 to your computer and use it in GitHub Desktop.
package com.example.edson.terminatorapp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.ScanSettings;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.UUID;
import com.polidea.rxandroidble.RxBleClient;
import com.polidea.rxandroidble.RxBleConnection;
import com.polidea.rxandroidble.RxBleDevice;
import com.polidea.rxandroidble.internal.RxBleLog;
import org.w3c.dom.Text;
import rx.Subscription;
public class MainActivity extends AppCompatActivity {
RxBleClient rxBleClient;
Boolean IsConnected = false;
String macAddress;
String name;
Button helloButton;
Button scanButton;
TextView macAddressText;
Subscription scanSubscription;
//String uuidCharacteristicValue = "0xA001";
String uuidCharacteristicValue = "0000a001-0000-1000-8000-0080";
//String uuidServiceValue = "0xA000";
final UUID characteristicUuid = java.util.UUID.fromString(uuidCharacteristicValue);
//final UUID serviceUuid = java.util.UUID.fromString(uuidServiceValue);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rxBleClient = RxBleClient.create(this);
helloButton = (Button) findViewById(R.id.helloButton);
scanButton = (Button) findViewById(R.id.scanButton);
macAddressText = (TextView)findViewById(R.id.macAddressText);
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Log.e("MainActivity", "Scanning");
scanSubscription = rxBleClient.scanBleDevices().subscribe(
rxBleScanResult -> {
// Process scan result here.
name = rxBleScanResult.getBleDevice().getName();
Log.e("MainActivity","Name :"+ name);
if(name.equals("Terminator")) { //TODO REFACTOR
Log.e("MainActivity","Found terminator!");
macAddress = rxBleScanResult.getBleDevice().getMacAddress();
macAddressText.setText("Connected to: " + macAddress);
scanSubscription.unsubscribe();
}
},
throwable -> {
Log.e("MainActivity","ERROR :"+ throwable.getMessage());
}
);
} catch (Exception e) {
e.printStackTrace();
}
}
});
helloButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
try{
Log.e("MainActivity", "Connecting");
RxBleDevice device = rxBleClient.getBleDevice(macAddress);
RxBleClient.setLogLevel(RxBleLog.DEBUG);
Subscription subscription = device.establishConnection(getApplicationContext(), false)
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristicUuid))
.doOnNext(notificationObservable -> {
// Notification has been set up
Log.e("MainActivity", "Bytes: " + notificationObservable);
})
.flatMap(notificationObservable -> notificationObservable) // <-- Notification has been set up, now observe value changes.
.subscribe(
bytes -> {
// Given characteristic has been changes, here is the value.
Log.e("MainActivity", "Bytes: " + bytes);
},
throwable -> {
// Handle an error here.
}
);
// //.flatMap(RxBleConnection::discoverServices)
// .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid))
// .subscribe(characteristicValue -> {
// Log.e("MainActivity", "CharacteristicValue: " + characteristicValue);
// },
// throwable -> {
// Log.e("MainActivity", "Any error:" + throwable.getMessage());
// });
// .subscribe(
// rxBleConnection -> {
// // All GATT operations are done through the rxBleConnection.
// Log.e("MainActivity", "GATT Operations: " + rxBleConnection.discoverServices());
// },
// throwable -> {
// // Handle an error here.
// Log.e("MainActivity", "Any error");
// }
// );
} catch (Exception e){
e.printStackTrace();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment