From 613ff1fc91511eaae517d4c599ed26bba98ea32c Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sun, 26 Apr 2015 00:53:48 +0200 Subject: [PATCH] Pebble: corrections to PebbleIoThreads, better logging --- .../BluetoothCommunicationService.java | 9 ++-- .../gadgetbridge/ControlCenter.java | 13 +++--- .../freeyourgadget/gadgetbridge/GB.java | 11 ++--- .../btle/AbstractBTLEDeviceSupport.java | 10 ++--- .../gadgetbridge/btle/BtLEQueue.java | 19 ++++---- .../gadgetbridge/miband/MiBandSupport.java | 44 +++++++++---------- .../gadgetbridge/pebble/PebbleIoThread.java | 22 +++++----- .../gadgetbridge/pebble/PebbleProtocol.java | 10 +++++ 8 files changed, 75 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index 1f4f80590..d9a0d22bb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -1,9 +1,5 @@ package nodomain.freeyourgadget.gadgetbridge; -import nodomain.freeyourgadget.gadgetbridge.GBDevice.State; -import nodomain.freeyourgadget.gadgetbridge.miband.MiBandSupport; -import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleIoThread; -import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleSupport; import android.app.NotificationManager; import android.app.Service; import android.bluetooth.BluetoothAdapter; @@ -23,6 +19,11 @@ import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.widget.Toast; +import nodomain.freeyourgadget.gadgetbridge.GBDevice.State; +import nodomain.freeyourgadget.gadgetbridge.miband.MiBandSupport; +import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleIoThread; +import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleSupport; + public class BluetoothCommunicationService extends Service { public static final String ACTION_START = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.start"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java index dbaf303fc..1033bc146 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java @@ -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 { @@ -114,7 +115,7 @@ public class ControlCenter extends Activity { /** * Requests information from the {@link BluetoothCommunicationService} about the connection state, * firmware info, etc. - * + *

* Note that this method may cause an implicit device connection (for auto-connectable devices). */ private void requestDeviceInfo() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GB.java index f98c4441e..fa7a71b41 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GB.java @@ -68,13 +68,14 @@ public class GB { pm.setComponentEnabledSetting(compName, newState, PackageManager.DONT_KILL_APP); } } - public static String hexdump(byte[] buffer, int length) { + + public static String hexdump(byte[] buffer, int offset, int length) { final char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexChars = new char[length * 2]; - for (int j = 0; j < length; j++) { - int v = buffer[j] & 0xFF; - hexChars[j * 2] = hexArray[v >>> 4]; - hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + for (int i = 0; i < length; i++) { + int v = buffer[i + offset] & 0xFF; + hexChars[i * 2] = hexArray[v >>> 4]; + hexChars[i * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/btle/AbstractBTLEDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/btle/AbstractBTLEDeviceSupport.java index 382253d55..e2f3aa0a8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/btle/AbstractBTLEDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/btle/AbstractBTLEDeviceSupport.java @@ -1,5 +1,10 @@ package nodomain.freeyourgadget.gadgetbridge.btle; +import android.bluetooth.BluetoothGatt; +import android.bluetooth.BluetoothGattCharacteristic; +import android.bluetooth.BluetoothGattService; +import android.util.Log; + import java.io.IOException; import java.util.HashMap; import java.util.HashSet; @@ -8,11 +13,6 @@ import java.util.Set; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.AbstractDeviceSupport; -import android.bluetooth.BluetoothGatt; -import android.bluetooth.BluetoothGattCharacteristic; -import android.bluetooth.BluetoothGattService; -import android.bluetooth.BluetoothProfile; -import android.util.Log; /** * @see TransactionBuilder diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/btle/BtLEQueue.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/btle/BtLEQueue.java index f4a339f16..aed18e7eb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/btle/BtLEQueue.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/btle/BtLEQueue.java @@ -1,5 +1,15 @@ package nodomain.freeyourgadget.gadgetbridge.btle; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothGatt; +import android.bluetooth.BluetoothGattCallback; +import android.bluetooth.BluetoothGattCharacteristic; +import android.bluetooth.BluetoothGattService; +import android.bluetooth.BluetoothProfile; +import android.content.Context; +import android.util.Log; + import java.util.Collections; import java.util.List; import java.util.concurrent.BlockingQueue; @@ -9,15 +19,6 @@ import java.util.concurrent.LinkedBlockingQueue; import nodomain.freeyourgadget.gadgetbridge.DeviceSupport; import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDevice.State; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothGatt; -import android.bluetooth.BluetoothGattCallback; -import android.bluetooth.BluetoothGattCharacteristic; -import android.bluetooth.BluetoothGattService; -import android.bluetooth.BluetoothProfile; -import android.content.Context; -import android.util.Log; /** * One queue/thread per connectable device. diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java index adb51049d..f5f778efe 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java @@ -5,8 +5,8 @@ import android.bluetooth.BluetoothGattCharacteristic; import android.util.Log; import java.io.IOException; -import java.util.UUID; import java.util.Calendar; +import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.GBCommand; import nodomain.freeyourgadget.gadgetbridge.GBDevice.State; @@ -125,29 +125,29 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { @Override public void onSetTime(long ts) { // TODO Not sure about the ts value - Calendar now = Calendar.getInstance(); - byte[] time = new byte[] { - (byte) (now.get(Calendar.YEAR) - 2000), - (byte) now.get(Calendar.MONTH), - (byte) now.get(Calendar.DATE), - (byte) now.get(Calendar.HOUR_OF_DAY), - (byte) now.get(Calendar.MINUTE), - (byte) now.get(Calendar.SECOND), - (byte) 0x0f, - (byte) 0x0f, - (byte) 0x0f, - (byte) 0x0f, - (byte) 0x0f, - (byte) 0x0f - }; + Calendar now = Calendar.getInstance(); + byte[] time = new byte[]{ + (byte) (now.get(Calendar.YEAR) - 2000), + (byte) now.get(Calendar.MONTH), + (byte) now.get(Calendar.DATE), + (byte) now.get(Calendar.HOUR_OF_DAY), + (byte) now.get(Calendar.MINUTE), + (byte) now.get(Calendar.SECOND), + (byte) 0x0f, + (byte) 0x0f, + (byte) 0x0f, + (byte) 0x0f, + (byte) 0x0f, + (byte) 0x0f + }; try { TransactionBuilder builder = performInitialized("Set date and time"); - BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_DATE_TIME); - if (characteristic != null) { - builder.write(characteristic, time); - } else { - Log.i(TAG, "Unable to set time -- characteristic not available"); - } + BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_DATE_TIME); + if (characteristic != null) { + builder.write(characteristic, time); + } else { + Log.i(TAG, "Unable to set time -- characteristic not available"); + } } catch (IOException ex) { Log.e(TAG, "Unable to set time on MI device", ex); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java index 679ab8a44..5bcd645f3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java @@ -107,7 +107,6 @@ public class PebbleIoThread extends GBDeviceIoThread { mQuit = !mIsConnected; // quit if not connected byte[] buffer = new byte[8192]; - int bytes; while (!mQuit) { try { @@ -147,7 +146,7 @@ public class PebbleIoThread extends GBDeviceIoThread { } break; case APP_UPLOAD_CHUNK: - bytes = mZis.read(buffer); + int bytes = mZis.read(buffer); if (bytes != -1) { writeInstallApp(mPebbleProtocol.encodeUploadChunk(mAppInstallToken, buffer, bytes)); @@ -190,9 +189,10 @@ public class PebbleIoThread extends GBDeviceIoThread { break; } } - bytes = mInStream.read(buffer, 0, 4); - if (bytes < 4) + int bytes = mInStream.read(buffer, 0, 4); + if (bytes < 4) { continue; + } ByteBuffer buf = ByteBuffer.wrap(buffer); buf.order(ByteOrder.BIG_ENDIAN); @@ -207,15 +207,13 @@ public class PebbleIoThread extends GBDeviceIoThread { } bytes = mInStream.read(buffer, 4, length); - if (bytes < length) { + while (bytes < length) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } - //Log.i(TAG, "Read " + bytes + ", expected " + length + " reading remaining " + (length - bytes)); - int bytes_rest = mInStream.read(buffer, 4 + bytes, length - bytes); - bytes += bytes_rest; + bytes += mInStream.read(buffer, bytes + 4, length - bytes); } if (length == 1 && endpoint == PebbleProtocol.ENDPOINT_PHONEVERSION) { @@ -230,14 +228,14 @@ public class PebbleIoThread extends GBDeviceIoThread { write(mPebbleProtocol.encodeSetTime(-1)); } } else if (endpoint == PebbleProtocol.ENDPOINT_DATALOG) { - Log.i(TAG, "datalog to endpoint " + endpoint + " (" + bytes + " bytes)"); - if (bytes <= 64) { - Log.i(TAG, "hexdump: " + GB.hexdump(buffer, bytes)); + Log.i(TAG, "datalog to endpoint " + endpoint + " (" + length + " bytes)"); + if (length <= 64) { + Log.i(TAG, "hexdump: " + GB.hexdump(buffer, 4, length)); } } else { GBDeviceCommand deviceCmd = mPebbleProtocol.decodeResponse(buffer); if (deviceCmd == null) { - Log.i(TAG, "unhandled message to endpoint " + endpoint + " (" + bytes + " bytes)"); + Log.i(TAG, "unhandled message to endpoint " + endpoint + " (" + length + " bytes)"); } else { evaluateGBCommandBundle(deviceCmd); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java index 18f2fcc09..5df3fb28f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java @@ -402,6 +402,16 @@ public class PebbleProtocol extends GBDeviceProtocol { return buf.array(); } + public byte[] encodeDatalog(byte[] buffer) { + ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + buffer.length); + buf.order(ByteOrder.BIG_ENDIAN); + buf.putShort((short) buffer.length); + buf.putShort(ENDPOINT_DATALOG); + buf.put(buffer); + + return buf.array(); + } + public GBDeviceCommand decodeResponse(byte[] responseData) { ByteBuffer buf = ByteBuffer.wrap(responseData); buf.order(ByteOrder.BIG_ENDIAN);