mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-10 17:11:56 +01:00
Make GBDevice parcable to make passing it via Intents more convinient
This commit is contained in:
parent
cd0139cd7c
commit
fbbc808ca8
@ -64,10 +64,9 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
||||||
String deviceAddress = intent.getStringExtra("device_address");
|
GBDevice device = intent.getParcelableExtra("device");
|
||||||
GBDevice.State state = GBDevice.State.values()[intent.getIntExtra("device_state", 0)];
|
if (mGBDevice.equals(device)) {
|
||||||
if (mGBDevice.getAddress().equals(deviceAddress)) {
|
mGBDevice = device;
|
||||||
mGBDevice.setState(state);
|
|
||||||
GB.setReceiversEnableState(mGBDevice.isConnected(), context);
|
GB.setReceiversEnableState(mGBDevice.isConnected(), context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
package nodomain.freeyourgadget.gadgetbridge;
|
package nodomain.freeyourgadget.gadgetbridge;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter;
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
@ -24,6 +19,12 @@ import android.widget.ListView;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
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 {
|
||||||
|
|
||||||
|
|
||||||
@ -44,27 +45,19 @@ public class ControlCenter extends Activity {
|
|||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
if (action.equals(ACTION_QUIT)) {
|
if (action.equals(ACTION_QUIT)) {
|
||||||
finish();
|
finish();
|
||||||
} else if (action.equals(ACTION_REFRESH_DEVICELIST) || action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
} else if (action.equals(ACTION_REFRESH_DEVICELIST)) {
|
||||||
String deviceAddress = intent.getStringExtra("device_address");
|
|
||||||
GBDevice.State state = GBDevice.State.values()[intent.getIntExtra("device_state", 0)];
|
|
||||||
String firmwareVersion = intent.getStringExtra("firmware_version");
|
|
||||||
refreshPairedDevices();
|
refreshPairedDevices();
|
||||||
mGBDeviceAdapter.notifyDataSetChanged();
|
} else if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
||||||
if (deviceAddress != null) {
|
GBDevice dev = intent.getParcelableExtra("device");
|
||||||
for (GBDevice device : deviceList) {
|
if (dev.getAddress() != null) {
|
||||||
if (device.getAddress().equals(deviceAddress)) {
|
for (int i = 0; i < deviceList.size(); i++) {
|
||||||
device.setFirmwareVersion(firmwareVersion);
|
if (dev.equals(deviceList.get(i))) {
|
||||||
device.setState(state);
|
deviceList.set(i, dev);
|
||||||
mGBDeviceAdapter.notifyDataSetChanged();
|
|
||||||
if (device.isConnected()) {
|
|
||||||
hintTextView.setText("tap connected device for App Mananger");
|
|
||||||
} else if (state == GBDevice.State.NOT_CONNECTED) {
|
|
||||||
hintTextView.setText("tap a device to connect");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
refreshPairedDevices();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -146,7 +139,6 @@ public class ControlCenter extends Activity {
|
|||||||
return true;
|
return true;
|
||||||
case R.id.action_refresh:
|
case R.id.action_refresh:
|
||||||
refreshPairedDevices();
|
refreshPairedDevices();
|
||||||
mGBDeviceAdapter.notifyDataSetChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
@ -203,6 +195,6 @@ public class ControlCenter extends Activity {
|
|||||||
hintTextView.setText("tap a device to connect");
|
hintTextView.setText("tap a device to connect");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mGBDeviceAdapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,69 +2,88 @@ package nodomain.freeyourgadget.gadgetbridge;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class GBDevice {
|
public class GBDevice implements Parcelable {
|
||||||
public static final String ACTION_DEVICE_CHANGED
|
public static final String ACTION_DEVICE_CHANGED
|
||||||
= "nodomain.freeyourgadget.gadgetbride.gbdevice.action.device_changed";
|
= "nodomain.freeyourgadget.gadgetbride.gbdevice.action.device_changed";
|
||||||
|
public static final Creator CREATOR = new Creator<GBDevice>() {
|
||||||
|
@Override
|
||||||
|
public GBDevice createFromParcel(Parcel source) {
|
||||||
|
return new GBDevice(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GBDevice[] newArray(int size) {
|
||||||
|
return new GBDevice[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
private static final String TAG = GBDevice.class.getSimpleName();
|
private static final String TAG = GBDevice.class.getSimpleName();
|
||||||
|
private final String mName;
|
||||||
private final String name;
|
private final String mAddress;
|
||||||
private final String address;
|
private final Type mType;
|
||||||
private final Type type;
|
private String mFirmwareVersion = null;
|
||||||
private String firmwareVersion = null;
|
private State mState = State.NOT_CONNECTED;
|
||||||
private State state = State.NOT_CONNECTED;
|
|
||||||
|
|
||||||
private short mBatteryLevel = 50; // unknown
|
private short mBatteryLevel = 50; // unknown
|
||||||
|
|
||||||
private String mBatteryState;
|
private String mBatteryState;
|
||||||
|
|
||||||
|
|
||||||
public GBDevice(String address, String name, Type type) {
|
public GBDevice(String address, String name, Type type) {
|
||||||
this.address = address;
|
mAddress = address;
|
||||||
this.name = name;
|
mName = name;
|
||||||
this.type = type;
|
mType = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
private GBDevice(Parcel in) {
|
||||||
|
mName = in.readString();
|
||||||
|
mAddress = in.readString();
|
||||||
|
mType = Type.values()[in.readInt()];
|
||||||
|
mFirmwareVersion = in.readString();
|
||||||
|
mState = State.values()[in.readInt()];
|
||||||
|
mBatteryLevel = (short) in.readInt();
|
||||||
|
mBatteryState = in.readString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return mName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return address;
|
return mAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirmwareVersion() {
|
public String getFirmwareVersion() {
|
||||||
return firmwareVersion;
|
return mFirmwareVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirmwareVersion(String firmwareVersion) {
|
public void setFirmwareVersion(String firmwareVersion) {
|
||||||
this.firmwareVersion = firmwareVersion;
|
mFirmwareVersion = firmwareVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return state.ordinal() >= State.CONNECTED.ordinal();
|
return mState.ordinal() >= State.CONNECTED.ordinal();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInitialized() {
|
public boolean isInitialized() {
|
||||||
return state.ordinal() >= State.INITIALIZED.ordinal();
|
return mState.ordinal() >= State.INITIALIZED.ordinal();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnecting() {
|
public boolean isConnecting() {
|
||||||
return state == State.CONNECTING;
|
return mState == State.CONNECTING;
|
||||||
}
|
}
|
||||||
|
|
||||||
public State getState() {
|
public State getState() {
|
||||||
return state;
|
return mState;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setState(State state) {
|
public void setState(State state) {
|
||||||
this.state = state;
|
mState = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
String getStateString() {
|
String getStateString() {
|
||||||
switch (state) {
|
switch (mState) {
|
||||||
case NOT_CONNECTED:
|
case NOT_CONNECTED:
|
||||||
return "not connected"; // TODO: do not hardcode
|
return "not connected"; // TODO: do not hardcode
|
||||||
case CONNECTING:
|
case CONNECTING:
|
||||||
@ -78,43 +97,57 @@ public class GBDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getInfoString() {
|
public String getInfoString() {
|
||||||
if (firmwareVersion != null) {
|
if (mFirmwareVersion != null) {
|
||||||
return getStateString() + " (FW: " + firmwareVersion + ")";
|
return getStateString() + " (FW: " + mFirmwareVersion + ")";
|
||||||
} else {
|
} else {
|
||||||
return getStateString();
|
return getStateString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getType() {
|
public Type getType() {
|
||||||
return type;
|
return mType;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this doesn't really belong here
|
// TODO: this doesn't really belong here
|
||||||
public void sendDeviceUpdateIntent(Context context) {
|
public void sendDeviceUpdateIntent(Context context) {
|
||||||
Intent deviceUpdateIntent = new Intent(ACTION_DEVICE_CHANGED);
|
Intent deviceUpdateIntent = new Intent(ACTION_DEVICE_CHANGED);
|
||||||
deviceUpdateIntent.putExtra("device_address", getAddress());
|
deviceUpdateIntent.putExtra("device", this);
|
||||||
deviceUpdateIntent.putExtra("device_state", getState().ordinal());
|
|
||||||
deviceUpdateIntent.putExtra("firmware_version", getFirmwareVersion());
|
|
||||||
|
|
||||||
LocalBroadcastManager.getInstance(context).sendBroadcast(deviceUpdateIntent);
|
LocalBroadcastManager.getInstance(context).sendBroadcast(deviceUpdateIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum State {
|
@Override
|
||||||
// Note: the order is important!
|
public int describeContents() {
|
||||||
NOT_CONNECTED,
|
return 0;
|
||||||
CONNECTING,
|
|
||||||
CONNECTED,
|
|
||||||
INITIALIZED
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Type {
|
@Override
|
||||||
UNKNOWN,
|
public boolean equals(Object obj) {
|
||||||
PEBBLE,
|
if (!(obj instanceof GBDevice)) {
|
||||||
MIBAND
|
return false;
|
||||||
|
}
|
||||||
|
if (obj == this) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (((GBDevice) obj).getAddress().equals(this.mAddress)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
|
dest.writeString(mName);
|
||||||
|
dest.writeString(mAddress);
|
||||||
|
dest.writeInt(mType.ordinal());
|
||||||
|
dest.writeString(mFirmwareVersion);
|
||||||
|
dest.writeInt(mState.ordinal());
|
||||||
|
dest.writeInt(mBatteryLevel);
|
||||||
|
dest.writeString(mBatteryState);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ranges from 0-100 (percent)
|
* Ranges from 0-100 (percent)
|
||||||
|
*
|
||||||
* @return the battery level in range 0-100
|
* @return the battery level in range 0-100
|
||||||
*/
|
*/
|
||||||
public short getBatteryLevel() {
|
public short getBatteryLevel() {
|
||||||
@ -139,4 +172,18 @@ public class GBDevice {
|
|||||||
public void setBatteryState(String batteryState) {
|
public void setBatteryState(String batteryState) {
|
||||||
mBatteryState = batteryState;
|
mBatteryState = batteryState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum State {
|
||||||
|
// Note: the order is important!
|
||||||
|
NOT_CONNECTED,
|
||||||
|
CONNECTING,
|
||||||
|
CONNECTED,
|
||||||
|
INITIALIZED
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Type {
|
||||||
|
UNKNOWN,
|
||||||
|
PEBBLE,
|
||||||
|
MIBAND
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ public class DeviceInfo extends AbstractInfo {
|
|||||||
public String getFirmwareVersion() {
|
public String getFirmwareVersion() {
|
||||||
if (mData.length == 16) {
|
if (mData.length == 16) {
|
||||||
int last = 15;
|
int last = 15;
|
||||||
return String.format(Locale.US, "%d.%d.%d.%d", mData[last], mData[last-1], mData[last-2], mData[last-3]);
|
return String.format(Locale.US, "%d.%d.%d.%d", mData[last], mData[last - 1], mData[last - 2], mData[last - 3]);
|
||||||
}
|
}
|
||||||
return "(unknown)"; // TODO: localization
|
return "(unknown)"; // TODO: localization
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package nodomain.freeyourgadget.gadgetbridge.miband;
|
package nodomain.freeyourgadget.gadgetbridge.miband;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothGatt;
|
||||||
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -7,9 +11,6 @@ import nodomain.freeyourgadget.gadgetbridge.GBCommand;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
|
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.btle.AbstractBTLEDeviceSupport;
|
import nodomain.freeyourgadget.gadgetbridge.btle.AbstractBTLEDeviceSupport;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.btle.TransactionBuilder;
|
import nodomain.freeyourgadget.gadgetbridge.btle.TransactionBuilder;
|
||||||
import android.bluetooth.BluetoothGatt;
|
|
||||||
import android.bluetooth.BluetoothGattCharacteristic;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
|
|
||||||
@ -181,7 +182,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCharacteristicRead(BluetoothGatt gatt,
|
public void onCharacteristicRead(BluetoothGatt gatt,
|
||||||
BluetoothGattCharacteristic characteristic, int status) {
|
BluetoothGattCharacteristic characteristic, int status) {
|
||||||
super.onCharacteristicRead(gatt, characteristic, status);
|
super.onCharacteristicRead(gatt, characteristic, status);
|
||||||
|
|
||||||
UUID characteristicUUID = characteristic.getUuid();
|
UUID characteristicUUID = characteristic.getUuid();
|
||||||
|
Loading…
Reference in New Issue
Block a user