List all suported devices and connect on tap. Remove the connect button.

This commit is contained in:
Andreas Shimokawa 2015-03-21 18:18:07 +01:00
parent f9166735ad
commit 4e465928e6
9 changed files with 169 additions and 31 deletions

View File

@ -1,5 +1,9 @@
###Changelog ###Changelog
####Version 0.1.3
* List all suported devices and connect on tap
* Remove the connect button
####Version 0.1.2 ####Version 0.1.2
* Added option to start Gadgetbridge and connect automatically when bluetooth is turned on * Added option to start Gadgetbridge and connect automatically when bluetooth is turned on
* stop service if bluetooth is turned off * stop service if bluetooth is turned off

View File

@ -8,8 +8,8 @@ android {
applicationId "nodomain.freeyourgadget.gadgetbridge" applicationId "nodomain.freeyourgadget.gadgetbridge"
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 21 targetSdkVersion 21
versionCode 3 versionCode 4
versionName "0.1.2" versionName "0.1.3"
} }
buildTypes { buildTypes {
release { release {

View File

@ -11,11 +11,13 @@ import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.IBinder; import android.os.IBinder;
import android.os.ParcelUuid; import android.os.ParcelUuid;
import android.preference.PreferenceManager;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
@ -26,7 +28,6 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.Set;
public class BluetoothCommunicationService extends Service { public class BluetoothCommunicationService extends Service {
public static final String ACTION_START public static final String ACTION_START
@ -166,17 +167,11 @@ public class BluetoothCommunicationService extends Service {
} else if (!mBtAdapter.isEnabled()) { } else if (!mBtAdapter.isEnabled()) {
Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
} else { } else {
String btDeviceAddress = null; String btDeviceAddress = intent.getStringExtra("device_address");
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices(); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
for (BluetoothDevice device : pairedDevices) { sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit();
if (device.getName().indexOf("Pebble") == 0) {
// Matching device found if (btDeviceAddress != null && (mBtSocket == null || !mBtSocket.isConnected())) {
btDeviceAddress = device.getAddress();
}
}
if (btDeviceAddress == null) {
Toast.makeText(this, "No supported device paired", Toast.LENGTH_SHORT).show();
} else if (mBtSocket == null || !mBtSocket.isConnected()) {
// currently only one thread allowed // currently only one thread allowed
if (mBtSocketIoThread != null) { if (mBtSocketIoThread != null) {
mBtSocketIoThread.quit(); mBtSocketIoThread.quit();

View File

@ -18,13 +18,17 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver {
if (!sharedPrefs.getBoolean("general_autoconnectonbluetooth", false)) { if (!sharedPrefs.getBoolean("general_autoconnectonbluetooth", false)) {
return; return;
} }
String deviceAddress = sharedPrefs.getString("last_device_address", null);
Intent startIntent = new Intent(context, BluetoothCommunicationService.class); Intent startIntent = new Intent(context, BluetoothCommunicationService.class);
startIntent.setAction(BluetoothCommunicationService.ACTION_START); startIntent.setAction(BluetoothCommunicationService.ACTION_START);
context.startService(startIntent); context.startService(startIntent);
if (deviceAddress != null) {
Intent connectIntent = new Intent(context, BluetoothCommunicationService.class); Intent connectIntent = new Intent(context, BluetoothCommunicationService.class);
connectIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT); connectIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT);
context.startService(connectIntent); connectIntent.putExtra("device_address", deviceAddress);
context.startService(connectIntent);
}
} else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { } else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
Intent stopIntent = new Intent(context, BluetoothCommunicationService.class); Intent stopIntent = new Intent(context, BluetoothCommunicationService.class);
context.stopService(stopIntent); context.stopService(stopIntent);

View File

@ -2,6 +2,8 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.app.Activity; import android.app.Activity;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -13,14 +15,25 @@ import android.support.v4.app.NotificationCompat;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; 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 class ControlCenter extends Activity {
public static final String ACTION_QUIT public static final String ACTION_QUIT
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit"; = "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit";
Button startServiceButton; ListView deviceListView;
GBDeviceAdapter mGBDeviceAdapter;
final List<GBDevice> deviceList = new ArrayList<>();
private BroadcastReceiver mReceiver = new BroadcastReceiver() { private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
@ -36,17 +49,37 @@ public class ControlCenter extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controlcenter); setContentView(R.layout.activity_controlcenter);
registerReceiver(mReceiver, new IntentFilter(ACTION_QUIT)); deviceListView = (ListView) findViewById(R.id.deviceListView);
mGBDeviceAdapter = new GBDeviceAdapter(this, deviceList);
startServiceButton = (Button) findViewById(R.id.startServiceButton); deviceListView.setAdapter(this.mGBDeviceAdapter);
startServiceButton.setOnClickListener(new View.OnClickListener() { deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @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); Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
startIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT); startIntent.setAction(BluetoothCommunicationService.ACTION_CONNECT);
startIntent.putExtra("device_address", deviceList.get(position).getAddress());
startService(startIntent); 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<BluetoothDevice> 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. * Ask for permission to intercept notifications on first run.
*/ */

View File

@ -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 "";
}
}

View File

@ -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<GBDevice> {
private final Context context;
private final List<GBDevice> deviceList;
public GBDeviceAdapter(Context context, List<GBDevice> 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;
}
}

View File

@ -6,13 +6,11 @@
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="nodomain.freeyourgadget.gadgetbridge.ControlCenter"> tools:context="nodomain.freeyourgadget.gadgetbridge.ControlCenter">
<Button <ListView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="connect" android:id="@+id/deviceListView"
android:id="@+id/startServiceButton" android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" />
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout> </RelativeLayout>

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:padding="8dp" >
<ImageView
android:id="@+id/device_image"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/device_image"
android:orientation="vertical"
android:paddingLeft="8dp" >
<TextView
android:id="@+id/device_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:singleLine="true" />
<TextView
android:id="@+id/device_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>