1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-19 03:20:21 +02:00

Pebble: corrections to PebbleIoThreads, better logging

This commit is contained in:
Andreas Shimokawa 2015-04-26 00:53:48 +02:00
parent 7a4330c324
commit 613ff1fc91
8 changed files with 75 additions and 63 deletions

View File

@ -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";

View File

@ -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.
*
* <p/>
* Note that this method may cause an implicit device connection (for auto-connectable devices).
*/
private void requestDeviceInfo() {

View File

@ -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);
}

View File

@ -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

View File

@ -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.

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);