Skip to content

Commit

Permalink
G5: debug version for new collection method (part 7b)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 29, 2016
1 parent 0dd5d0e commit b194acf
Showing 1 changed file with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
//Log.d(TAG, "SDK: " + Build.VERSION.SDK_INT);
//stopScan();
if (!CollectionServiceStarter.isBTG5(xdrip.getAppContext())) {
Log.e(TAG,"Shutting down as no longer using G5 data source");
service_running = false;
keep_running = false;
stopSelf();
Expand Down Expand Up @@ -347,6 +348,7 @@ public IBinder onBind(Intent intent) {
}

public void setupBluetooth() {

getTransmitterDetails();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
//First time using the app or bluetooth was turned off?
Expand Down Expand Up @@ -377,6 +379,10 @@ public void run() {
filters.add(new ScanFilter.Builder().setDeviceName("Dexcom" + transmitterIdLastTwo).build());
}

// unbond here to avoid clashes when we are mid-connection
if (alwaysUnbond()) {
forgetDevice();
}
cycleScan(0);
}
}
Expand Down Expand Up @@ -480,7 +486,7 @@ public void run() {

private synchronized void scanLogic() {
if (!keep_running) return;
if (JoH.ratelimit("G5-scanlogic",2)) {
if (JoH.ratelimit("G5-scanlogic", 2)) {
try {
mLEScanner.stopScan(mScanCallback);
isScanning = false;
Expand Down Expand Up @@ -622,7 +628,7 @@ public void run() {
}
}

void forgetDevice() {
private synchronized void forgetDevice() {
Log.d(TAG,"forgetDevice() start");
Transmitter defaultTransmitter = new Transmitter(prefs.getString("dex_txid", "ABCDEF"));
mBluetoothAdapter = mBluetoothManager.getAdapter();
Expand All @@ -631,9 +637,9 @@ void forgetDevice() {
for (BluetoothDevice device : pairedDevices) {
if (device.getName() != null) {

String transmitterIdLastTwo = Extensions.lastTwoCharactersOfString(defaultTransmitter.transmitterId);
String deviceNameLastTwo = Extensions.lastTwoCharactersOfString(device.getName());
Log.e(TAG, "removeBond");
final String transmitterIdLastTwo = Extensions.lastTwoCharactersOfString(defaultTransmitter.transmitterId);
final String deviceNameLastTwo = Extensions.lastTwoCharactersOfString(device.getName());
Log.e(TAG, "removeBond: "+transmitterIdLastTwo+" vs "+deviceNameLastTwo);
if (transmitterIdLastTwo.equals(deviceNameLastTwo)) {
try {
Method m = device.getClass().getMethod("removeBond", (Class[]) null);
Expand Down Expand Up @@ -787,30 +793,31 @@ private synchronized void connectToDevice(BluetoothDevice device) {
mGatt = null;
}
Log.i(TAG, "Request Connect");
final BluetoothDevice mDevice = device;
if (enforceMainThread()) {
Handler iHandler = new Handler(Looper.getMainLooper());
final BluetoothDevice mDevice = device;
iHandler.post(new Runnable() {
@Override
public void run() {
// TODO move this to method
Log.i(TAG, "mGatt Null, connecting...");
Log.i(TAG, "connectToDevice On Main Thread? " + isOnMainThread());
mGatt = mDevice.connectGatt(getApplicationContext(), false, gattCallback);

connectGatt(mDevice);
}
});
} else {
Log.i(TAG, "mGatt Null, connecting...");
Log.i(TAG, "connectToDevice On Main Thread? " + isOnMainThread());
mGatt = device.connectGatt(getApplicationContext(), false, gattCallback);
connectGatt(mDevice);
}

} else {
Log.e(TAG, "connectToDevice baulking due to rate-limit");
}
}

private synchronized void connectGatt(BluetoothDevice mDevice)
{
Log.i(TAG, "mGatt Null, connecting...");
Log.i(TAG, "connectToDevice On Main Thread? " + isOnMainThread());
mGatt = mDevice.connectGatt(getApplicationContext(), false, gattCallback);
}


// Sends the disconnect tx message to our bt device.
private synchronized void doDisconnectMessage(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Expand Down Expand Up @@ -1172,10 +1179,11 @@ private synchronized void processOnCharacteristicRead (BluetoothGatt gatt,
authStatus = new AuthStatusRxMessage(buffer);

// TODO KS check here
//if (authStatus.authenticated == 1 && authStatus.bonded == 1 && !isBondedOrBonding) {
// device.createBond();
// isBondedOrBonding = true;
//}
if (authStatus.authenticated == 1 && authStatus.bonded == 1 && !isBondedOrBonding) {
Log.e(TAG,"Special bonding test case!");
device.createBond();
isBondedOrBonding = true;
}

if (authStatus.authenticated == 1 && authStatus.bonded == 1 && isBondedOrBonding) {
// TODO check bonding logic here and above
Expand Down

0 comments on commit b194acf

Please sign in to comment.