1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 11:26:47 +01:00

Pebble: Emulator support

NOTE:
- supports aplite and basalt emulator
- needs recompilation of Gadgetbridge with INTERNET permission

TODO:
- fix disconnect issues
- emulator special packet support
- string localization
- ...
This commit is contained in:
Andreas Shimokawa 2015-08-19 00:03:52 +02:00
parent 121baa19ec
commit a7796ecbc6
6 changed files with 130 additions and 24 deletions

View File

@ -5,7 +5,9 @@
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<!--
<uses-permission android:name="android.permission.INTERNET" />
-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

View File

@ -340,13 +340,23 @@ public class ControlCenter extends Activity {
}
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String miAddr = sharedPrefs.getString(MiBandConst.PREF_MIBAND_ADDRESS, null);
if (miAddr != null && miAddr.length() > 0) {
String miAddr = sharedPrefs.getString(MiBandConst.PREF_MIBAND_ADDRESS, "");
if (miAddr.length() > 0) {
GBDevice miDevice = new GBDevice(miAddr, "MI", DeviceType.MIBAND);
if (!availableDevices.contains(miDevice)) {
availableDevices.add(miDevice);
}
}
String pebbleEmuAddr = sharedPrefs.getString("pebble_emu_addr", "");
String pebbleEmuPort = sharedPrefs.getString("pebble_emu_port", "");
if (pebbleEmuAddr.length() >= 7 && pebbleEmuPort.length() > 0) {
GBDevice pebbleEmuDevice = new GBDevice(pebbleEmuAddr + ":" + pebbleEmuPort, "Pebble qemu", DeviceType.PEBBLE);
if (!availableDevices.contains(pebbleEmuDevice)) {
availableDevices.add(pebbleEmuDevice);
}
}
deviceList.retainAll(availableDevices);
for (GBDevice dev : availableDevices) {
if (!deviceList.contains(dev)) {

View File

@ -3,6 +3,7 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.support.v4.content.LocalBroadcastManager;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity;
@ -35,5 +36,37 @@ public class SettingsActivity extends AbstractSettingsActivity {
}
});
final Preference pebbleEmuAddr = findPreference("pebble_emu_addr");
pebbleEmuAddr.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
Intent refreshIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent);
preference.setSummary(newVal.toString());
return true;
}
});
final Preference pebbleEmuPort = findPreference("pebble_emu_port");
pebbleEmuPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
Intent refreshIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent);
preference.setSummary(newVal.toString());
return true;
}
});
}
@Override
protected String[] getPreferenceKeysWithSummary() {
return new String[]{
"pebble_emu_addr",
"pebble_emu_port"
};
}
}

View File

@ -25,11 +25,16 @@ public class DeviceSupportFactory {
}
public synchronized DeviceSupport createDeviceSupport(String deviceAddress) throws GBException {
DeviceSupport deviceSupport = createBTDeviceSupport(deviceAddress);
DeviceSupport deviceSupport;
if (deviceAddress.indexOf(":") == deviceAddress.lastIndexOf(":")) { // only one colon
deviceSupport = createTCPDeviceSupport(deviceAddress);
} else {
deviceSupport = createBTDeviceSupport(deviceAddress);
}
if (deviceSupport != null) {
return deviceSupport;
}
// support for other kinds of transports
// no device found, check transport availability and warn
checkBtAvailability();
@ -68,4 +73,16 @@ public class DeviceSupportFactory {
}
return null;
}
private DeviceSupport createTCPDeviceSupport(String deviceAddress) throws GBException {
try {
GBDevice gbDevice = new GBDevice(deviceAddress, "Pebble qemu", DeviceType.PEBBLE); //FIXME, do not hardcode
DeviceSupport deviceSupport = new ServiceDeviceSupport(new PebbleSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
return deviceSupport;
} catch (Exception e) {
throw new GBException("cannot connect to " + deviceAddress, e); // FIXME: localize
}
}
}

View File

@ -16,6 +16,8 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.ZipInputStream;
@ -37,14 +39,17 @@ public class PebbleIoThread extends GBDeviceIoThread {
private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class);
private final PebbleProtocol mPebbleProtocol;
private final PebbleSupport mPebbleSupport;
private boolean mIsTCP = false;
private BluetoothAdapter mBtAdapter = null;
private BluetoothSocket mBtSocket = null;
private Socket mTCPSocket = null; // for emulator
private InputStream mInStream = null;
private OutputStream mOutStream = null;
private boolean mQuit = false;
private boolean mIsConnected = false;
private boolean mIsInstalling = false;
private int mConnectionAttempts = 0;
private PBWReader mPBWReader = null;
private int mAppInstallToken = -1;
private ZipInputStream mZis = null;
@ -66,14 +71,25 @@ public class PebbleIoThread extends GBDeviceIoThread {
@Override
protected boolean connect(String btDeviceAddress) {
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
ParcelUuid uuids[] = btDevice.getUuids();
GBDevice.State originalState = gbDevice.getState();
try {
// contains only one ":"? then it is addr:port
int firstColon = btDeviceAddress.indexOf(":");
if (firstColon == btDeviceAddress.lastIndexOf(":")) {
mIsTCP = true;
InetAddress serverAddr = InetAddress.getByName(btDeviceAddress.substring(0, firstColon));
mTCPSocket = new Socket(serverAddr, Integer.parseInt(btDeviceAddress.substring(firstColon + 1)));
mInStream = mTCPSocket.getInputStream();
mOutStream = mTCPSocket.getOutputStream();
} else {
mIsTCP = false;
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
ParcelUuid uuids[] = btDevice.getUuids();
mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
mBtSocket.connect();
mInStream = mBtSocket.getInputStream();
mOutStream = mBtSocket.getOutputStream();
}
} catch (IOException e) {
e.printStackTrace();
gbDevice.setState(originalState);
@ -187,11 +203,14 @@ public class PebbleIoThread extends GBDeviceIoThread {
break;
}
}
if (mIsTCP) {
mInStream.skip(6);
}
int bytes = mInStream.read(buffer, 0, 4);
if (bytes < 4) {
continue;
}
ByteBuffer buf = ByteBuffer.wrap(buffer);
buf.order(ByteOrder.BIG_ENDIAN);
short length = buf.getShort();
@ -214,6 +233,10 @@ public class PebbleIoThread extends GBDeviceIoThread {
bytes += mInStream.read(buffer, bytes + 4, length - bytes);
}
if (mIsTCP) {
mInStream.skip(2);
}
GBDeviceEvent deviceEvent = mPebbleProtocol.decodeResponse(buffer);
if (deviceEvent == null) {
LOG.info("unhandled message to endpoint " + endpoint + " (" + length + " bytes)");
@ -261,15 +284,31 @@ public class PebbleIoThread extends GBDeviceIoThread {
gbDevice.sendDeviceUpdateIntent(getContext());
}
private void write_real(byte[] bytes) {
try {
if (mIsTCP) {
ByteBuffer buf = ByteBuffer.allocate(bytes.length + 8);
buf.order(ByteOrder.BIG_ENDIAN);
buf.putShort((short) 0xfeed);
buf.putShort((short) 1);
buf.putShort((short) bytes.length);
buf.put(bytes);
buf.putShort((short) 0xbeef);
mOutStream.write(buf.array());
mOutStream.flush();
} else {
mOutStream.write(bytes);
mOutStream.flush();
}
} catch (IOException e) {
}
}
@Override
synchronized public void write(byte[] bytes) {
// block writes if app installation in in progress
if (mIsConnected && (!mIsInstalling || mInstallState == PebbleAppInstallState.WAIT_SLOT)) {
try {
mOutStream.write(bytes);
mOutStream.flush();
} catch (IOException e) {
}
write_real(bytes);
}
}
@ -371,13 +410,8 @@ public class PebbleIoThread extends GBDeviceIoThread {
if (!mIsInstalling) {
return;
}
int length = bytes.length;
LOG.info("got " + length + "bytes for writeInstallApp()");
try {
mOutStream.write(bytes);
mOutStream.flush();
} catch (IOException e) {
}
LOG.info("got " + bytes.length + "bytes for writeInstallApp()");
write_real(bytes);
}
public void installApp(Uri uri, int appId) {

View File

@ -77,6 +77,16 @@
android:key="pebble_force_untested"
android:title="@string/pref_title_pebble_forceuntested"
android:summary="@string/pref_summary_pebble_forceuntested" />
<EditTextPreference
android:digits="0123456789."
android:key="pebble_emu_addr"
android:maxLength="15"
android:title="Emulator IP" />
<EditTextPreference
android:inputType="number"
android:key="pebble_emu_port"
android:maxLength="5"
android:title="Emulator Port" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceCategory>