Pebble: Add Pebble specific pairing activity mainly useful for Pebble 2 since it properly pairs the Pebble after connecting with BLE

Fixes #433
This commit is contained in:
Andreas Shimokawa 2016-11-19 22:13:00 +01:00
parent ddfab1cdae
commit 029cc02a29
19 changed files with 249 additions and 36 deletions

View File

@ -256,6 +256,9 @@
<activity
android:name=".devices.miband.MiBandPairingActivity"
android:label="@string/title_activity_mi_band_pairing" />
<activity
android:name=".devices.pebble.PebblePairingActivity"
android:label="@string/title_activity_pebble_pairing" />
<activity
android:name=".activities.charts.ChartsActivity"
android:label="@string/title_activity_charts"

View File

@ -160,7 +160,7 @@ public class MiBandPairingActivity extends GBActivity {
private void startPairing() {
isPairing = true;
message.setText(getString(R.string.miband_pairing, macAddress));
message.setText(getString(R.string.pairing, macAddress));
IntentFilter filter = new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
@ -209,20 +209,20 @@ public class MiBandPairingActivity extends GBActivity {
protected void performBluetoothPair(BluetoothDevice device) {
int bondState = device.getBondState();
if (bondState == BluetoothDevice.BOND_BONDED) {
GB.toast(getString(R.string.miband_pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO);
GB.toast(getString(R.string.pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO);
performPair();
return;
}
bondingMacAddress = device.getAddress();
if (bondState == BluetoothDevice.BOND_BONDING) {
GB.toast(this, getString(R.string.miband_pairing_in_progress, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.INFO);
GB.toast(this, getString(R.string.pairing_in_progress, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.INFO);
return;
}
GB.toast(this, getString(R.string.miband_pairing_creating_bond_with, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.INFO);
GB.toast(this, getString(R.string.pairing_creating_bond_with, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.INFO);
if (!device.createBond()) {
GB.toast(this, getString(R.string.miband_pairing_unable_to_pair_with, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.ERROR);
GB.toast(this, getString(R.string.pairing_unable_to_pair_with, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.ERROR);
}
}

View File

@ -42,7 +42,7 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator {
@Override
public Class<? extends Activity> getPairingActivity() {
return null;
return PebblePairingActivity.class;
}
public Class<? extends Activity> getPrimaryActivity() {

View File

@ -0,0 +1,177 @@
package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.TextView;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter;
import nodomain.freeyourgadget.gadgetbridge.activities.DiscoveryActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.GBActivity;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class PebblePairingActivity extends GBActivity {
private static final Logger LOG = LoggerFactory.getLogger(PebblePairingActivity.class);
private TextView message;
private boolean isPairing;
private boolean isLEPebble;
private String macAddress;
private final BroadcastReceiver mPairingReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
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())) {
if (device.isInitialized()) {
pairingFinished(true);
} else if (device.isConnecting() || device.isInitializing()) {
LOG.info("still connecting/initializing device...");
}
}
}
}
};
private final BroadcastReceiver mBondingReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LOG.info("Bond state changed: " + device + ", state: " + device.getBondState() + ", expected address: " + macAddress);
if (macAddress != null && macAddress.equals(device.getAddress())) {
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
if (bondState == BluetoothDevice.BOND_BONDED) {
LOG.info("Bonded with " + device.getAddress());
if (!isLEPebble) {
performConnect(device);
}
} 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() + ", attempting to connect anyway.");
} else {
LOG.warn("Unknown bond state for device " + device.getAddress() + ": " + bondState);
pairingFinished(false);
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pebble_pairing);
message = (TextView) findViewById(R.id.pebble_pair_message);
Intent intent = getIntent();
macAddress = intent.getStringExtra(DeviceCoordinator.EXTRA_DEVICE_MAC_ADDRESS);
if (macAddress == null) {
Toast.makeText(this, getString(R.string.message_cannot_pair_no_mac), Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, DiscoveryActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
return;
}
startPairing();
}
@Override
protected void onDestroy() {
try {
// just to be sure, remove the receivers -- might actually be already unregistered
LocalBroadcastManager.getInstance(this).unregisterReceiver(mPairingReceiver);
unregisterReceiver(mBondingReceiver);
} catch (IllegalArgumentException ex) {
// already unregistered, ignore
}
if (isPairing) {
stopPairing();
}
super.onDestroy();
}
private void startPairing() {
isPairing = true;
message.setText(getString(R.string.pairing, macAddress));
IntentFilter filter = new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mBondingReceiver, filter);
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
if (device != null) {
performPair(device);
} else {
GB.toast(this, "No such Bluetooth Device: " + macAddress, Toast.LENGTH_LONG, GB.ERROR);
}
}
private void pairingFinished(boolean pairedSuccessfully) {
LOG.debug("pairingFinished: " + pairedSuccessfully);
if (!isPairing) {
// already gone?
return;
}
isPairing = false;
LocalBroadcastManager.getInstance(this).unregisterReceiver(mPairingReceiver);
unregisterReceiver(mBondingReceiver);
if (pairedSuccessfully) {
Intent intent = new Intent(this, ControlCenter.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
finish();
}
private void stopPairing() {
// TODO
isPairing = false;
}
protected void performPair(BluetoothDevice device) {
int bondState = device.getBondState();
if (bondState == BluetoothDevice.BOND_BONDED) {
GB.toast(getString(R.string.pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO);
return;
}
if (bondState == BluetoothDevice.BOND_BONDING) {
GB.toast(this, getString(R.string.pairing_in_progress, device.getName(), macAddress), Toast.LENGTH_LONG, GB.INFO);
return;
}
GB.toast(this, getString(R.string.pairing_creating_bond_with, device.getName(), macAddress), Toast.LENGTH_LONG, GB.INFO);
GBApplication.deviceService().disconnect(); // just to make sure...
if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
isLEPebble = true;
performConnect(device);
} else {
isLEPebble = false;
device.createBond();
}
}
private void performConnect(BluetoothDevice device) {
GBDevice gbDevice = new GBDevice(device.getAddress(), device.getName(), DeviceType.PEBBLE);
GBApplication.deviceService().connect(gbDevice);
}
}

View File

@ -6,7 +6,7 @@
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPairingActivity">
<TextView android:text="@string/miband_pairing" android:layout_width="wrap_content"
<TextView android:text="@string/pairing" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/miband_pair_message" />

View File

@ -0,0 +1,29 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebblePairingActivity">
<TextView android:text="@string/pairing" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pebble_pair_message" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_marginTop="25dp"
android:layout_below="@+id/pebble_pair_message"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pebble_le_pairing_hint"
android:id="@+id/pebble_le_pair_hint"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

View File

@ -126,7 +126,7 @@
<string name="title_activity_android_pairing">Gerät paaren</string>
<string name="android_pairing_hint">Verwende den Android Bluetooth Paaren-Dialog um Dein Gerät zu paaren.</string>
<string name="title_activity_mi_band_pairing">Paare Dein Mi Band</string>
<string name="miband_pairing">Paarung mit %s…</string>
<string name="pairing">Paarung mit %s…</string>
<string name="message_cannot_pair_no_mac">Kein MAC Adresse bekommen, kann nicht paaren.</string>
<string name="preferences_category_device_specific_settings">Gerätespezifische Einstellungen</string>
<string name="preferences_miband_settings">Mi Band Einstellungen</string>

View File

@ -126,11 +126,11 @@
<string name="title_activity_android_pairing">Emparejar dispositivo</string>
<string name="android_pairing_hint">Usar el menú de emparejar Bluetooth de Android para emparejar el dispositivo.</string>
<string name="title_activity_mi_band_pairing">Emparejar tu MiBand</string>
<string name="miband_pairing">Emparejando con %s...</string>
<string name="miband_pairing_creating_bond_with">Creando emparejamiento con %1$s (%2$s)</string>
<string name="miband_pairing_unable_to_pair_with">No se ha podido emparejar con %1$s (%2$s)</string>
<string name="miband_pairing_in_progress">Emparejamiento en progreso: %1$s (%2$s)</string>
<string name="miband_pairing_already_bonded">Emparejado con %1$s (%2$s), conectando…</string>
<string name="pairing">Emparejando con %s...</string>
<string name="pairing_creating_bond_with">Creando emparejamiento con %1$s (%2$s)</string>
<string name="pairing_unable_to_pair_with">No se ha podido emparejar con %1$s (%2$s)</string>
<string name="pairing_in_progress">Emparejamiento en progreso: %1$s (%2$s)</string>
<string name="pairing_already_bonded">Emparejado con %1$s (%2$s), conectando…</string>
<string name="message_cannot_pair_no_mac">Ninguna dirección MAC aprobada, no se puede emparejar.</string>
<string name="preferences_category_device_specific_settings">Ajustes específicos del dispositivo</string>
<string name="preferences_miband_settings">Ajustes de MiBand</string>

View File

@ -123,7 +123,7 @@
<string name="title_activity_android_pairing">Coupler l\'appareil</string>
<string name="android_pairing_hint">Utiliser le couplement Bluetooth d\'Android pour coupler l\'appareil</string>
<string name="title_activity_mi_band_pairing">Coupler votre Mi Band</string>
<string name="miband_pairing">Coupler avec %s...</string>
<string name="pairing">Coupler avec %s...</string>
<string name="message_cannot_pair_no_mac">Aucune adresse mac fournie, ne peut être couplé</string>
<string name="preferences_category_device_specific_settings">Paramètres spécifiques à l\'appareil </string>
<string name="preferences_miband_settings">Paramètres Mi Band</string>

View File

@ -123,7 +123,7 @@
<string name="title_activity_android_pairing">Eszköz párosítása</string>
<string name="android_pairing_hint">Használja az Android Bluetooth párosítás ablakot eszköz párosításához.</string>
<string name="title_activity_mi_band_pairing">Párosítsd a Mi Band-ed</string>
<string name="miband_pairing">Párosítás: %s…</string>
<string name="pairing">Párosítás: %s…</string>
<string name="message_cannot_pair_no_mac">Nincs találat a MAC címre, nem lehet párosítani.</string>
<string name="preferences_category_device_specific_settings">Eszközspecifikus beállítások</string>
<string name="preferences_miband_settings">MI Band beállítások</string>

View File

@ -126,7 +126,7 @@
<string name="title_activity_android_pairing">Accoppia dispositivo</string>
<string name="android_pairing_hint">Utilizza la funzione del sistema operativo per accoppiare il dispositivo.</string>
<string name="title_activity_mi_band_pairing">Accoppia la Mi Band</string>
<string name="miband_pairing">Accoppiamento con %s…</string>
<string name="pairing">Accoppiamento con %s…</string>
<string name="message_cannot_pair_no_mac">Indirizzo MAC mancante, impossibile completare l\'accoppiamento.</string>
<string name="preferences_category_device_specific_settings">Impostazioni specifiche dispositivo</string>
<string name="preferences_miband_settings">Impostazioni Mi Band</string>

View File

@ -129,11 +129,11 @@
<string name="title_activity_android_pairing">デバイスのペアリング</string>
<string name="android_pairing_hint">デバイスをペアリングするには、AndroidのBluetoothペアリングのダイアログを使用します。</string>
<string name="title_activity_mi_band_pairing">お使いのMi Bandをペアリング</string>
<string name="miband_pairing">%sとペアリング…</string>
<string name="miband_pairing_creating_bond_with">%1$s (%2$s) と結合を作成中</string>
<string name="miband_pairing_unable_to_pair_with">%1$s (%2$s) とペアにすることができません</string>
<string name="miband_pairing_in_progress">結合の進行中: %1$s (%2$s)</string>
<string name="miband_pairing_already_bonded">既に %1$s (%2$s) と結合しています。接続中…</string>
<string name="pairing">%sとペアリング…</string>
<string name="pairing_creating_bond_with">%1$s (%2$s) と結合を作成中</string>
<string name="pairing_unable_to_pair_with">%1$s (%2$s) とペアにすることができません</string>
<string name="pairing_in_progress">結合の進行中: %1$s (%2$s)</string>
<string name="pairing_already_bonded">既に %1$s (%2$s) と結合しています。接続中…</string>
<string name="message_cannot_pair_no_mac">MACアドレスが渡されませんでした。ペアリングできません。</string>
<string name="preferences_category_device_specific_settings">デバイス固有の設定</string>
<string name="preferences_miband_settings">Mi Bandの設定</string>

View File

@ -96,7 +96,7 @@
<string name="title_activity_android_pairing">기기 페어링</string>
<string name="android_pairing_hint">기기를 페어링하려면 안드로이드 블루투스 페어링 설정을 사용하세요</string>
<string name="title_activity_mi_band_pairing">Mi Band 페어링</string>
<string name="miband_pairing">%s에 페어링…</string>
<string name="pairing">%s에 페어링…</string>
<string name="message_cannot_pair_no_mac">MAC 주소가 통과되지 않았습니다. 페어링 할 수 없습니다.</string>
<string name="preferences_category_device_specific_settings">기기 특정 설정</string>
<string name="preferences_miband_settings">Mi Band 설정</string>

View File

@ -83,7 +83,7 @@
<string name="title_activity_android_pairing">sparuj urządzenie</string>
<string name="android_pairing_hint">Użyj parowania bluetooth na androidzie by sparować urządzenie.</string>
<string name="title_activity_mi_band_pairing">Sparuj swoje Mi Band</string>
<string name="miband_pairing">Parowanie z %s…</string>
<string name="pairing">Parowanie z %s…</string>
<string name="message_cannot_pair_no_mac">Żaden mac nie przeszedł, nie można sparować.</string>
<string name="preferences_category_device_specific_settings">Ustawienia danego urządzenia</string>
<string name="preferences_miband_settings">Ustawienia Mi Band</string>

View File

@ -99,7 +99,7 @@
<string name="title_activity_android_pairing">Сопряжение устройств</string>
<string name="android_pairing_hint">Для сопряжения устройств используйте диалог Android.</string>
<string name="title_activity_mi_band_pairing">Сопряжение вашего Mi Band</string>
<string name="miband_pairing">Сопряжение с %s…</string>
<string name="pairing">Сопряжение с %s…</string>
<string name="message_cannot_pair_no_mac">MAC-адрес не был передан, сопряжение не удалось.</string>
<string name="preferences_category_device_specific_settings">Настройки устройства</string>
<string name="preferences_miband_settings">Настройки Mi Band</string>

View File

@ -80,7 +80,7 @@
<string name="title_activity_android_pairing">Cihaz ile eşleştir</string>
<string name="android_pairing_hint">Android Bluetooth Eşleme penceresini kullanarak cihaz ile eşleştiriniz.</string>
<string name="title_activity_mi_band_pairing">Mi Band ile eşleştir</string>
<string name="miband_pairing">%s ile eşleşiyor…</string>
<string name="pairing">%s ile eşleşiyor…</string>
<string name="message_cannot_pair_no_mac">MAC adresi gelmedi, eşleşemiyor.</string>
<string name="preferences_category_device_specific_settings">Cihaz için özel ayarlar</string>
<string name="preferences_miband_settings">Mi Band Ayarları</string>

View File

@ -105,7 +105,7 @@
<string name="title_activity_android_pairing">Створення пари з пристроєм</string>
<string name="android_pairing_hint">Для створення пари із пристроєм використовуйте діалог Android.</string>
<string name="title_activity_mi_band_pairing">Створення пари із вашим Mi—Band</string>
<string name="miband_pairing">Створення пари із %s…</string>
<string name="pairing">Створення пари із %s…</string>
<string name="message_cannot_pair_no_mac">MAC-адресу не було передано, не вдалося створити пару.</string>
<string name="preferences_category_device_specific_settings">Параметри пристрою</string>
<string name="preferences_miband_settings">Параметри Mi—Band</string>

View File

@ -67,7 +67,7 @@
<string name="device_with_rssi">%1$s (%2$s)</string>
<string name="title_activity_android_pairing">Ghép đôi thiết bị</string>
<string name="title_activity_mi_band_pairing">Ghép đôi Mi Band</string>
<string name="miband_pairing">Đang ghép đôi với %s…</string>
<string name="pairing">Đang ghép đôi với %s…</string>
<string name="message_cannot_pair_no_mac">không thể kiểm tra địa chỉ mac, không thể ghép đôi.</string>
<string name="preferences_category_device_specific_settings">Cài đặt cụ thể cho thiết bị</string>
<string name="preferences_miband_settings">Cài đặt Mi Band</string>

View File

@ -155,11 +155,11 @@
<string name="title_activity_android_pairing">Pair Device</string>
<string name="android_pairing_hint">Use the Android Bluetooth Pairing dialog to pair the device.</string>
<string name="title_activity_mi_band_pairing">Pair your Mi Band</string>
<string name="miband_pairing">Pairing with %s…</string>
<string name="miband_pairing_creating_bond_with">"Creating bond with %1$s (%2$s)"</string>
<string name="miband_pairing_unable_to_pair_with">"Unable to pair with %1$s (%2$s)"</string>
<string name="miband_pairing_in_progress">Bonding in progress: %1$s (%2$s)</string>
<string name="miband_pairing_already_bonded">"Already bonded with %1$s (%2$s), connecting…"</string>
<string name="pairing">Pairing with %s…</string>
<string name="pairing_creating_bond_with">"Creating bond with %1$s (%2$s)"</string>
<string name="pairing_unable_to_pair_with">"Unable to pair with %1$s (%2$s)"</string>
<string name="pairing_in_progress">Bonding in progress: %1$s (%2$s)</string>
<string name="pairing_already_bonded">"Already bonded with %1$s (%2$s), connecting…"</string>
<string name="message_cannot_pair_no_mac">No mac address passed, cannot pair.</string>
<string name="preferences_category_device_specific_settings">Device Specific Settings</string>
<string name="preferences_miband_settings">Mi Band Settings</string>
@ -278,6 +278,10 @@
<string name="miband_prefs_reserve_alarm_calendar">Alarms to reserve for upcoming events</string>
<string name="miband_prefs_hr_sleep_detection">Use Heartrate Sensor to improve sleep detection</string>
<string name="miband_prefs_device_time_offset_hours">Device time offset in hours (for detecting sleep of shift workers)</string>
<string name="miband2_prefs_dateformat">Mi2: Date Format</string>
<string name="dateformat_time">Time</string>
<string name="dateformat_date_time"><![CDATA[Time & Date]]></string>
<string name="mi2_prefs_activate_display_on_lift">Activate display upon lift</string>
<string name="waiting_for_reconnect">waiting for reconnect</string>
@ -347,8 +351,8 @@
<!-- Strings related to Vibration Activity -->
<string name="title_activity_vibration">Vibration</string>
<string name="miband2_prefs_dateformat">Mi2: Date Format</string>
<string name="dateformat_time">Time</string>
<string name="dateformat_date_time"><![CDATA[Time & Date]]></string>
<string name="mi2_prefs_activate_display_on_lift">Activate display upon lift</string>
<!-- Strings related to Pebble Pairing Activity-->
<string name="title_activity_pebble_pairing">Pebble Pairing</string>
<string name="pebble_le_pairing_hint">A pairing dialog is expected to pop up on your Android device. If that does not happen, look in the notification drawer and accept the pairing request. After that accept the pairing request on your Pebble</string>
</resources>