diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bdf3b13b..8c2094c52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ###Changelog +####Version 0.1.3 +* List all suported devices and connect on tap +* Remove the connect button + ####Version 0.1.2 * Added option to start Gadgetbridge and connect automatically when bluetooth is turned on * stop service if bluetooth is turned off diff --git a/app/build.gradle b/app/build.gradle index 0764b4dba..1b3c4e316 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "nodomain.freeyourgadget.gadgetbridge" minSdkVersion 19 targetSdkVersion 21 - versionCode 3 - versionName "0.1.2" + versionCode 4 + versionName "0.1.3" } buildTypes { release { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index b9487ce22..7499413a2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -11,11 +11,13 @@ import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.IBinder; import android.os.ParcelUuid; +import android.preference.PreferenceManager; import android.provider.ContactsContract; import android.support.v4.app.NotificationCompat; import android.util.Log; @@ -26,7 +28,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.util.Set; public class BluetoothCommunicationService extends Service { public static final String ACTION_START @@ -166,17 +167,11 @@ public class BluetoothCommunicationService extends Service { } else if (!mBtAdapter.isEnabled()) { Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show(); } else { - String btDeviceAddress = null; - Set pairedDevices = mBtAdapter.getBondedDevices(); - for (BluetoothDevice device : pairedDevices) { - if (device.getName().indexOf("Pebble") == 0) { - // Matching device found - btDeviceAddress = device.getAddress(); - } - } - if (btDeviceAddress == null) { - Toast.makeText(this, "No supported device paired", Toast.LENGTH_SHORT).show(); - } else if (mBtSocket == null || !mBtSocket.isConnected()) { + String btDeviceAddress = intent.getStringExtra("device_address"); + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit(); + + if (btDeviceAddress != null && (mBtSocket == null || !mBtSocket.isConnected())) { // currently only one thread allowed if (mBtSocketIoThread != null) { mBtSocketIoThread.quit(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothStateChangeReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothStateChangeReceiver.java index c83a061e9..d82d0bd4d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothStateChangeReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothStateChangeReceiver.java @@ -18,13 +18,17 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver { if (!sharedPrefs.getBoolean("general_autoconnectonbluetooth", false)) { return; } + + String deviceAddress = sharedPrefs.getString("last_device_address", null); Intent startIntent = new Intent(context, BluetoothCommunicationService.class); startIntent.setAction(BluetoothCommunicationService.ACTION_START); context.startService(startIntent); - - Intent connectIntent = new Intent(context, BluetoothCommunicationService.class); - connectIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT); - context.startService(connectIntent); + if (deviceAddress != null) { + Intent connectIntent = new Intent(context, BluetoothCommunicationService.class); + connectIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT); + connectIntent.putExtra("device_address", deviceAddress); + context.startService(connectIntent); + } } else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { Intent stopIntent = new Intent(context, BluetoothCommunicationService.class); context.stopService(stopIntent); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java index 4279a7b52..295a61d2e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java @@ -2,6 +2,8 @@ package nodomain.freeyourgadget.gadgetbridge; import android.app.Activity; import android.app.NotificationManager; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -13,14 +15,25 @@ import android.support.v4.app.NotificationCompat; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.widget.Button; +import android.widget.AdapterView; +import android.widget.ListView; +import android.widget.Toast; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter; public class ControlCenter extends Activity { + public static final String ACTION_QUIT = "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit"; - Button startServiceButton; + ListView deviceListView; + GBDeviceAdapter mGBDeviceAdapter; + final List deviceList = new ArrayList<>(); private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override @@ -36,17 +49,37 @@ public class ControlCenter extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_controlcenter); - registerReceiver(mReceiver, new IntentFilter(ACTION_QUIT)); - - startServiceButton = (Button) findViewById(R.id.startServiceButton); - startServiceButton.setOnClickListener(new View.OnClickListener() { + deviceListView = (ListView) findViewById(R.id.deviceListView); + mGBDeviceAdapter = new GBDeviceAdapter(this, deviceList); + deviceListView.setAdapter(this.mGBDeviceAdapter); + deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override - public void onClick(View v) { + public void onItemClick(AdapterView parent, View v, int position, long id) { Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class); startIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT); + startIntent.putExtra("device_address", deviceList.get(position).getAddress()); + startService(startIntent); } }); + + registerReceiver(mReceiver, new IntentFilter(ACTION_QUIT)); + + BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); + if (btAdapter == null) { + Toast.makeText(this, "Bluetooth is not supported.", Toast.LENGTH_SHORT).show(); + } else if (!btAdapter.isEnabled()) { + Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show(); + } else { + Set pairedDevices = btAdapter.getBondedDevices(); + for (BluetoothDevice device : pairedDevices) { + if (device.getName().indexOf("Pebble") == 0) { + // Matching device found + deviceList.add(new GBDevice(device.getAddress(), device.getName())); + } + } + } + /* * Ask for permission to intercept notifications on first run. */ diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java new file mode 100644 index 000000000..3d9f2ab27 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java @@ -0,0 +1,23 @@ +package nodomain.freeyourgadget.gadgetbridge; + +public class GBDevice { + private final String name; + private final String address; + + public GBDevice(String address, String name) { + this.address = address; + this.name = name; + } + + public String getName() { + return name; + } + + public String getAddress() { + return address; + } + + public String getStatus() { + return ""; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapter.java new file mode 100644 index 000000000..f8afb38e8 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapter.java @@ -0,0 +1,44 @@ +package nodomain.freeyourgadget.gadgetbridge.adapter; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import java.util.List; + +import nodomain.freeyourgadget.gadgetbridge.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.R; + +public class GBDeviceAdapter extends ArrayAdapter { + + private final Context context; + private final List deviceList; + + public GBDeviceAdapter(Context context, List deviceList) { + super(context, 0, deviceList); + + this.context = context; + this.deviceList = deviceList; + } + + @Override + public View getView(int position, View view, ViewGroup parent) { + GBDevice device = getItem(position); + + if (view == null) { + LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + + view = inflater.inflate(R.layout.device_item, parent, false); + } + TextView deviceStatusLabel = (TextView) view.findViewById(R.id.device_status); + TextView deviceNameLabel = (TextView) view.findViewById(R.id.device_name); + deviceStatusLabel.setText(device.getStatus()); + deviceNameLabel.setText(device.getName()); + + return view; + } +} diff --git a/app/src/main/res/layout/activity_controlcenter.xml b/app/src/main/res/layout/activity_controlcenter.xml index cb4f86891..5a45e791f 100644 --- a/app/src/main/res/layout/activity_controlcenter.xml +++ b/app/src/main/res/layout/activity_controlcenter.xml @@ -6,13 +6,11 @@ android:paddingBottom="@dimen/activity_vertical_margin" tools:context="nodomain.freeyourgadget.gadgetbridge.ControlCenter"> -