mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-27 20:36:51 +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) {
|
||||
String action = intent.getAction();
|
||||
if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
||||
String deviceAddress = intent.getStringExtra("device_address");
|
||||
GBDevice.State state = GBDevice.State.values()[intent.getIntExtra("device_state", 0)];
|
||||
if (mGBDevice.getAddress().equals(deviceAddress)) {
|
||||
mGBDevice.setState(state);
|
||||
GBDevice device = intent.getParcelableExtra("device");
|
||||
if (mGBDevice.equals(device)) {
|
||||
mGBDevice = device;
|
||||
GB.setReceiversEnableState(mGBDevice.isConnected(), context);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,5 @@
|
||||
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.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
@ -24,6 +19,12 @@ import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
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 {
|
||||
|
||||
|
||||
@ -44,27 +45,19 @@ public class ControlCenter extends Activity {
|
||||
String action = intent.getAction();
|
||||
if (action.equals(ACTION_QUIT)) {
|
||||
finish();
|
||||
} else if (action.equals(ACTION_REFRESH_DEVICELIST) || action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
||||
String deviceAddress = intent.getStringExtra("device_address");
|
||||
GBDevice.State state = GBDevice.State.values()[intent.getIntExtra("device_state", 0)];
|
||||
String firmwareVersion = intent.getStringExtra("firmware_version");
|
||||
} else if (action.equals(ACTION_REFRESH_DEVICELIST)) {
|
||||
refreshPairedDevices();
|
||||
mGBDeviceAdapter.notifyDataSetChanged();
|
||||
if (deviceAddress != null) {
|
||||
for (GBDevice device : deviceList) {
|
||||
if (device.getAddress().equals(deviceAddress)) {
|
||||
device.setFirmwareVersion(firmwareVersion);
|
||||
device.setState(state);
|
||||
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");
|
||||
}
|
||||
} else if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
||||
GBDevice dev = intent.getParcelableExtra("device");
|
||||
if (dev.getAddress() != null) {
|
||||
for (int i = 0; i < deviceList.size(); i++) {
|
||||
if (dev.equals(deviceList.get(i))) {
|
||||
deviceList.set(i, dev);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
refreshPairedDevices();
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -146,7 +139,6 @@ public class ControlCenter extends Activity {
|
||||
return true;
|
||||
case R.id.action_refresh:
|
||||
refreshPairedDevices();
|
||||
mGBDeviceAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
return super.onOptionsItemSelected(item);
|
||||
@ -203,6 +195,6 @@ public class ControlCenter extends Activity {
|
||||
hintTextView.setText("tap a device to connect");
|
||||
}
|
||||
}
|
||||
mGBDeviceAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,69 +2,88 @@ package nodomain.freeyourgadget.gadgetbridge;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.util.Log;
|
||||
|
||||
public class GBDevice {
|
||||
public class GBDevice implements Parcelable {
|
||||
public static final String 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 final String name;
|
||||
private final String address;
|
||||
private final Type type;
|
||||
private String firmwareVersion = null;
|
||||
private State state = State.NOT_CONNECTED;
|
||||
|
||||
private final String mName;
|
||||
private final String mAddress;
|
||||
private final Type mType;
|
||||
private String mFirmwareVersion = null;
|
||||
private State mState = State.NOT_CONNECTED;
|
||||
private short mBatteryLevel = 50; // unknown
|
||||
|
||||
private String mBatteryState;
|
||||
|
||||
|
||||
public GBDevice(String address, String name, Type type) {
|
||||
this.address = address;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
mAddress = address;
|
||||
mName = name;
|
||||
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() {
|
||||
return name;
|
||||
return mName;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
return mAddress;
|
||||
}
|
||||
|
||||
public String getFirmwareVersion() {
|
||||
return firmwareVersion;
|
||||
return mFirmwareVersion;
|
||||
}
|
||||
|
||||
public void setFirmwareVersion(String firmwareVersion) {
|
||||
this.firmwareVersion = firmwareVersion;
|
||||
mFirmwareVersion = firmwareVersion;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return state.ordinal() >= State.CONNECTED.ordinal();
|
||||
return mState.ordinal() >= State.CONNECTED.ordinal();
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
return state.ordinal() >= State.INITIALIZED.ordinal();
|
||||
return mState.ordinal() >= State.INITIALIZED.ordinal();
|
||||
}
|
||||
|
||||
public boolean isConnecting() {
|
||||
return state == State.CONNECTING;
|
||||
return mState == State.CONNECTING;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
return mState;
|
||||
}
|
||||
|
||||
public void setState(State state) {
|
||||
this.state = state;
|
||||
mState = state;
|
||||
}
|
||||
|
||||
String getStateString() {
|
||||
switch (state) {
|
||||
switch (mState) {
|
||||
case NOT_CONNECTED:
|
||||
return "not connected"; // TODO: do not hardcode
|
||||
case CONNECTING:
|
||||
@ -78,43 +97,57 @@ public class GBDevice {
|
||||
}
|
||||
|
||||
public String getInfoString() {
|
||||
if (firmwareVersion != null) {
|
||||
return getStateString() + " (FW: " + firmwareVersion + ")";
|
||||
if (mFirmwareVersion != null) {
|
||||
return getStateString() + " (FW: " + mFirmwareVersion + ")";
|
||||
} else {
|
||||
return getStateString();
|
||||
}
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
return mType;
|
||||
}
|
||||
|
||||
// TODO: this doesn't really belong here
|
||||
public void sendDeviceUpdateIntent(Context context) {
|
||||
Intent deviceUpdateIntent = new Intent(ACTION_DEVICE_CHANGED);
|
||||
deviceUpdateIntent.putExtra("device_address", getAddress());
|
||||
deviceUpdateIntent.putExtra("device_state", getState().ordinal());
|
||||
deviceUpdateIntent.putExtra("firmware_version", getFirmwareVersion());
|
||||
|
||||
deviceUpdateIntent.putExtra("device", this);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(deviceUpdateIntent);
|
||||
}
|
||||
|
||||
public enum State {
|
||||
// Note: the order is important!
|
||||
NOT_CONNECTED,
|
||||
CONNECTING,
|
||||
CONNECTED,
|
||||
INITIALIZED
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
UNKNOWN,
|
||||
PEBBLE,
|
||||
MIBAND
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof GBDevice)) {
|
||||
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)
|
||||
*
|
||||
* @return the battery level in range 0-100
|
||||
*/
|
||||
public short getBatteryLevel() {
|
||||
@ -139,4 +172,18 @@ public class GBDevice {
|
||||
public void setBatteryState(String 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() {
|
||||
if (mData.length == 16) {
|
||||
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
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.miband;
|
||||
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -7,9 +11,6 @@ import nodomain.freeyourgadget.gadgetbridge.GBCommand;
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
|
||||
import nodomain.freeyourgadget.gadgetbridge.btle.AbstractBTLEDeviceSupport;
|
||||
import nodomain.freeyourgadget.gadgetbridge.btle.TransactionBuilder;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.util.Log;
|
||||
|
||||
public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||
|
||||
@ -181,7 +182,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||
|
||||
@Override
|
||||
public void onCharacteristicRead(BluetoothGatt gatt,
|
||||
BluetoothGattCharacteristic characteristic, int status) {
|
||||
BluetoothGattCharacteristic characteristic, int status) {
|
||||
super.onCharacteristicRead(gatt, characteristic, status);
|
||||
|
||||
UUID characteristicUUID = characteristic.getUuid();
|
||||
|
Loading…
Reference in New Issue
Block a user