mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-08-24 16:21:16 +02:00
fix unpaired MI devices disappearing from view (#15)
This commit is contained in:
parent
f6208f1031
commit
1e3ba57b03
@ -130,7 +130,7 @@ public class BluetoothCommunicationService extends Service {
|
||||
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
|
||||
if (btDevice != null) {
|
||||
if (btDevice.getName() == null || btDevice.getName().equals("MI")) { //FIXME: workaround for Miband not being paired
|
||||
mGBDevice = new GBDevice(btDeviceAddress, btDevice.getName(), GBDevice.Type.MIBAND);
|
||||
mGBDevice = new GBDevice(btDeviceAddress, "MI", GBDevice.Type.MIBAND);
|
||||
mDeviceSupport = new MiBandSupport();
|
||||
} else if (btDevice.getName().indexOf("Pebble") == 0) {
|
||||
mGBDevice = new GBDevice(btDeviceAddress, btDevice.getName(), GBDevice.Type.PEBBLE);
|
||||
|
@ -1,5 +1,10 @@
|
||||
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;
|
||||
@ -19,12 +24,6 @@ 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 {
|
||||
|
||||
|
||||
@ -50,11 +49,9 @@ public class ControlCenter extends Activity {
|
||||
} 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;
|
||||
}
|
||||
int index = deviceList.indexOf(dev); // search by address
|
||||
if (index >= 0) {
|
||||
deviceList.set(index, dev);
|
||||
}
|
||||
}
|
||||
refreshPairedDevices();
|
||||
@ -151,13 +148,14 @@ public class ControlCenter extends Activity {
|
||||
}
|
||||
|
||||
private void refreshPairedDevices() {
|
||||
GBDevice connectedDevice = null;
|
||||
boolean connected = false;
|
||||
List<GBDevice> availableDevices = new ArrayList<>();
|
||||
for (GBDevice device : deviceList) {
|
||||
if (device.isConnected() || device.isConnecting()) {
|
||||
connectedDevice = device;
|
||||
connected = true;
|
||||
availableDevices.add(device);
|
||||
}
|
||||
}
|
||||
deviceList.clear();
|
||||
|
||||
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
|
||||
@ -167,29 +165,37 @@ public class ControlCenter extends Activity {
|
||||
Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
|
||||
for (BluetoothDevice device : pairedDevices) {
|
||||
for (BluetoothDevice pairedDevice : pairedDevices) {
|
||||
GBDevice.Type deviceType;
|
||||
if (device.getName().indexOf("Pebble") == 0) {
|
||||
if (pairedDevice.getName().indexOf("Pebble") == 0) {
|
||||
deviceType = GBDevice.Type.PEBBLE;
|
||||
} else if (device.getName().equals("MI")) {
|
||||
} else if (pairedDevice.getName().equals("MI")) {
|
||||
deviceType = GBDevice.Type.MIBAND;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if (connectedDevice != null && (device.getAddress().equals(connectedDevice.getAddress()))) {
|
||||
deviceList.add(connectedDevice);
|
||||
} else {
|
||||
deviceList.add(new GBDevice(device.getAddress(), device.getName(), deviceType));
|
||||
GBDevice device = new GBDevice(pairedDevice.getAddress(), pairedDevice.getName(), deviceType);
|
||||
if (!availableDevices.contains(device)) {
|
||||
availableDevices.add(device);
|
||||
}
|
||||
}
|
||||
|
||||
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
String miAddr = sharedPrefs.getString("development_miaddr", null);
|
||||
if (miAddr != null && !miAddr.equals("") && (connectedDevice == null || !miAddr.equals(connectedDevice.getAddress()))) {
|
||||
deviceList.add(new GBDevice(miAddr, "MI", GBDevice.Type.MIBAND));
|
||||
if (miAddr != null && miAddr.length() > 0) {
|
||||
GBDevice miDevice = new GBDevice(miAddr, "MI", GBDevice.Type.MIBAND);
|
||||
if (!availableDevices.contains(miDevice)) {
|
||||
availableDevices.add(miDevice);
|
||||
}
|
||||
}
|
||||
deviceList.retainAll(availableDevices);
|
||||
for (GBDevice dev : availableDevices) {
|
||||
if (!deviceList.contains(dev)) {
|
||||
deviceList.add(dev);
|
||||
}
|
||||
}
|
||||
|
||||
if (connectedDevice != null) {
|
||||
if (connected) {
|
||||
hintTextView.setText("tap connected device for App Mananger");
|
||||
} else if (!deviceList.isEmpty()) {
|
||||
hintTextView.setText("tap a device to connect");
|
||||
|
@ -35,6 +35,7 @@ public class GBDevice implements Parcelable {
|
||||
mAddress = address;
|
||||
mName = name;
|
||||
mType = type;
|
||||
validate();
|
||||
}
|
||||
|
||||
private GBDevice(Parcel in) {
|
||||
@ -46,6 +47,25 @@ public class GBDevice implements Parcelable {
|
||||
mState = State.values()[in.readInt()];
|
||||
mBatteryLevel = (short) in.readInt();
|
||||
mBatteryState = in.readString();
|
||||
validate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeString(mName);
|
||||
dest.writeString(mAddress);
|
||||
dest.writeInt(mType.ordinal());
|
||||
dest.writeString(mFirmwareVersion);
|
||||
dest.writeString(mHardwareVersion);
|
||||
dest.writeInt(mState.ordinal());
|
||||
dest.writeInt(mBatteryLevel);
|
||||
dest.writeString(mBatteryState);
|
||||
}
|
||||
|
||||
private void validate() {
|
||||
if (getAddress() == null) {
|
||||
throw new IllegalArgumentException("address must not be null");
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@ -149,15 +169,8 @@ public class GBDevice implements Parcelable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeString(mName);
|
||||
dest.writeString(mAddress);
|
||||
dest.writeInt(mType.ordinal());
|
||||
dest.writeString(mFirmwareVersion);
|
||||
dest.writeString(mHardwareVersion);
|
||||
dest.writeInt(mState.ordinal());
|
||||
dest.writeInt(mBatteryLevel);
|
||||
dest.writeString(mBatteryState);
|
||||
public int hashCode() {
|
||||
return mAddress.hashCode() ^ 37;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,34 @@
|
||||
00001800-0000-1000-8000-00805f9b34fb:Generic Access Service
|
||||
- 00002a00-0000-1000-8000-00805f9b34fb:Device Name
|
||||
- 00002a01-0000-1000-8000-00805f9b34fb:Appearance
|
||||
- 00002a02-0000-1000-8000-00805f9b34fb:Peripheral Privacy Flag
|
||||
- 00002a04-0000-1000-8000-00805f9b34fb:Peripheral Preferred Connection Parameters
|
||||
|
||||
00001801-0000-1000-8000-00805f9b34fb:Generic Attribute Service
|
||||
- 00002a05-0000-1000-8000-00805f9b34fb:Service Changed
|
||||
|
||||
0000fee0-0000-1000-8000-00805f9b34fb:MiBand Service
|
||||
- 0000ff01-0000-1000-8000-00805f9b34fb:Device Info
|
||||
- 0000ff02-0000-1000-8000-00805f9b34fb:Device Name
|
||||
- 0000ff03-0000-1000-8000-00805f9b34fb:Notification
|
||||
- 0000ff04-0000-1000-8000-00805f9b34fb:User Info
|
||||
- 0000ff05-0000-1000-8000-00805f9b34fb:Control Point
|
||||
- 0000ff06-0000-1000-8000-00805f9b34fb:Realtime Steps
|
||||
- 0000ff07-0000-1000-8000-00805f9b34fb:Activity Data
|
||||
- 0000ff08-0000-1000-8000-00805f9b34fb:Firmware Data
|
||||
- 0000ff09-0000-1000-8000-00805f9b34fb:LE Params
|
||||
- 0000ff0a-0000-1000-8000-00805f9b34fb:Date/Time
|
||||
- 0000ff0b-0000-1000-8000-00805f9b34fb:Statistics
|
||||
- 0000ff0c-0000-1000-8000-00805f9b34fb:Battery
|
||||
- 0000ff0d-0000-1000-8000-00805f9b34fb:Test
|
||||
- 0000ff0e-0000-1000-8000-00805f9b34fb:Sensor Data
|
||||
|
||||
0000fee1-0000-1000-8000-00805f9b34fb:Unknown service
|
||||
- 0000fedd-0000-1000-8000-00805f9b34fb:Unknown characteristic
|
||||
- 0000fede-0000-1000-8000-00805f9b34fb:Unknown characteristic
|
||||
- 0000fedf-0000-1000-8000-00805f9b34fb:Unknown characteristic
|
||||
|
||||
0000fee7-0000-1000-8000-00805f9b34fb:Unknown service
|
||||
- 0000fec7-0000-1000-8000-00805f9b34fb:Unknown characteristic
|
||||
- 0000fec8-0000-1000-8000-00805f9b34fb:Unknown characteristic
|
||||
- 0000fec9-0000-1000-8000-00805f9b34fb:Unknown characteristic
|
Loading…
Reference in New Issue
Block a user