We must not use UUID_CHAR_PAIR anymore. This prevents connecting
without being bonded. Connecting when bonded still works.

As without bonding, ControlCenter would not display the device anymore,
we have to re-install the "remember last connected device" in the
preferences thing.
master
cpfeiffer 2016-07-14 23:53:25 +02:00
parent ebda3e1535
commit 7b26986ab0
2 changed files with 34 additions and 14 deletions

View File

@ -1,6 +1,5 @@
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
@ -44,8 +43,12 @@ public class MiBandPairingActivity extends GBActivity {
if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
LOG.debug("pairing activity: device changed: " + device);
if (macAddress.equals(device.getAddress()) && device.isInitialized()) {
pairingFinished(true);
if (macAddress.equals(device.getAddress())) {
if (device.isInitialized()) {
pairingFinished(true, macAddress);
} else if (device.isConnecting() || device.isInitializing()) {
LOG.info("still connecting/initializing device...");
}
}
}
}
@ -62,26 +65,32 @@ public class MiBandPairingActivity extends GBActivity {
if (bondState == BluetoothDevice.BOND_BONDED) {
LOG.info("Bonded with " + device.getAddress());
bondingMacAddress = null;
Looper mainLooper = Looper.getMainLooper();
new Handler(mainLooper).postDelayed(new Runnable() {
@Override
public void run() {
performPair();
}
}, DELAY_AFTER_BONDING);
attemptToConnect();
} else if (bondState == BluetoothDevice.BOND_BONDING) {
LOG.info("Bonding in progress with " + device.getAddress());
} else if (bondState == BluetoothDevice.BOND_NONE) {
LOG.info("Not bonded with " + device.getAddress() + ", aborting bonding.");
pairingFinished(false);
LOG.info("Not bonded with " + device.getAddress() + ", attempting to connect anyway.");
bondingMacAddress = null;
attemptToConnect();
} else {
LOG.warn("Unknown bond state for device " + device.getAddress() + ": " + bondState);
pairingFinished(false, bondingMacAddress);
}
}
}
}
};
private void attemptToConnect() {
Looper mainLooper = Looper.getMainLooper();
new Handler(mainLooper).postDelayed(new Runnable() {
@Override
public void run() {
performPair();
}
}, DELAY_AFTER_BONDING);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -166,7 +175,7 @@ public class MiBandPairingActivity extends GBActivity {
}
}
private void pairingFinished(boolean pairedSuccessfully) {
private void pairingFinished(boolean pairedSuccessfully, String macAddress) {
LOG.debug("pairingFinished: " + pairedSuccessfully);
if (!isPairing) {
// already gone?
@ -178,6 +187,14 @@ public class MiBandPairingActivity extends GBActivity {
unregisterReceiver(mBondingReceiver);
if (pairedSuccessfully) {
// remember the device since we do not necessarily pair... temporary -- we probably need
// to query the db for available devices in ControlCenter. But only remember un-bonded
// devices, as bonded devices are displayed anyway.
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
if (device != null && device.getBondState() == BluetoothDevice.BOND_NONE) {
Prefs prefs = GBApplication.getPrefs();
prefs.getPreferences().edit().putString(MiBandConst.PREF_MIBAND_ADDRESS, macAddress).apply();
}
Intent intent = new Intent(this, ControlCenter.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
@ -210,6 +227,7 @@ public class MiBandPairingActivity extends GBActivity {
}
private void performPair() {
GBApplication.deviceService().disconnect(); // just to make sure...
GBApplication.deviceService().connect(macAddress, true);
}
}

View File

@ -109,7 +109,9 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
enableNotifications(builder, true)
.setLowLatency(builder)
.readDate(builder) // without reading the data, we get sporadic connection problems, especially directly after turning on BT
.pair(builder)
// this is apparently not needed anymore, and actually causes problems when bonding is not used/does not work
// so we simply not use the UUID_PAIR characteristic.
// .pair(builder)
.requestDeviceInfo(builder)
.sendUserInfo(builder)
.checkAuthenticationNeeded(builder, getDevice())