mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
Add ACTION_SETTIME to Service and add a corresponding button in the ControlCenter activity
This commit is contained in:
+59
-11
@@ -5,6 +5,7 @@ import android.app.PendingIntent;
|
|||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
|
import android.bluetooth.BluetoothServerSocket;
|
||||||
import android.bluetooth.BluetoothSocket;
|
import android.bluetooth.BluetoothSocket;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
@@ -17,22 +18,27 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class BluetoothCommunicationService extends Service {
|
public class BluetoothCommunicationService extends Service {
|
||||||
private static final String TAG = "BluetoothCommunicationService";
|
private static final String TAG = "BluetoothCommunicationService";
|
||||||
|
|
||||||
// TODO: put in separate static class
|
public static final String ACTION_START
|
||||||
public static final String ACTION_STARTBLUETOOTHCOMMUNITCATIONSERVICE
|
|
||||||
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.start";
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.start";
|
||||||
public static final String ACTION_STOPBLUETOOTHCOMMUNITCATIONSERVICE
|
public static final String ACTION_STOP
|
||||||
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.stop";
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.stop";
|
||||||
public static final String ACTION_SENDBLUETOOTHMESSAGE
|
public static final String ACTION_SENDMESSAGE
|
||||||
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.sendbluetoothmessage";
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.sendbluetoothmessage";
|
||||||
|
public static final String ACTION_SETTIME
|
||||||
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.settime";
|
||||||
|
|
||||||
private BluetoothAdapter mBtAdapter = null;
|
private BluetoothAdapter mBtAdapter = null;
|
||||||
private String mBtDeviceAddress = null;
|
private String mBtDeviceAddress = null;
|
||||||
private BluetoothSocket mBtSocket = null;
|
private BluetoothSocket mBtSocket = null;
|
||||||
private BtSocketIoThread mBtSocketIoThread = null;
|
private BtSocketIoThread mBtSocketIoThread = null;
|
||||||
|
private BtSocketAcceptThread mBtSocketAcceptThread = null;
|
||||||
|
private static final UUID PEBBLE_UUID = UUID.fromString("00000000-deca-fade-deca-deafdecacafe");
|
||||||
|
private boolean mPassiveMode = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
@@ -41,7 +47,7 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
if (intent.getAction().equals(ACTION_STARTBLUETOOTHCOMMUNITCATIONSERVICE)) {
|
if (intent.getAction().equals(ACTION_START)) {
|
||||||
Intent notificationIntent = new Intent(this, ControlCenter.class);
|
Intent notificationIntent = new Intent(this, ControlCenter.class);
|
||||||
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
@@ -73,7 +79,10 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (mBtSocket == null || !mBtSocket.isConnected()) {
|
if (mPassiveMode) {
|
||||||
|
mBtSocketAcceptThread = new BtSocketAcceptThread();
|
||||||
|
mBtSocketAcceptThread.start();
|
||||||
|
} else if (mBtSocket == null || !mBtSocket.isConnected()) {
|
||||||
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(mBtDeviceAddress);
|
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(mBtDeviceAddress);
|
||||||
ParcelUuid uuids[] = btDevice.getUuids();
|
ParcelUuid uuids[] = btDevice.getUuids();
|
||||||
mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
|
mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
|
||||||
@@ -87,7 +96,7 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
}
|
}
|
||||||
startForeground(1, notification); //FIXME: don't hardcode id
|
startForeground(1, notification); //FIXME: don't hardcode id
|
||||||
}
|
}
|
||||||
} else if (intent.getAction().equals(ACTION_STOPBLUETOOTHCOMMUNITCATIONSERVICE)) {
|
} else if (intent.getAction().equals(ACTION_STOP)) {
|
||||||
try {
|
try {
|
||||||
mBtSocketIoThread.join();
|
mBtSocketIoThread.join();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -103,12 +112,16 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
|
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
stopSelf();
|
stopSelf();
|
||||||
} else if (intent.getAction().equals(ACTION_SENDBLUETOOTHMESSAGE)) {
|
} else if (intent.getAction().equals(ACTION_SENDMESSAGE)) {
|
||||||
String title = intent.getStringExtra("notification_title");
|
String title = intent.getStringExtra("notification_title");
|
||||||
String content = intent.getStringExtra("notification_content");
|
String content = intent.getStringExtra("notification_content");
|
||||||
if (mBtSocketIoThread != null) {
|
if (mBtSocketIoThread != null) {
|
||||||
byte[] msg;
|
byte[] msg = PebbleProtocol.encodeSMS(title, content);
|
||||||
msg = PebbleProtocol.encodeSMS(title, content);
|
mBtSocketIoThread.write(msg);
|
||||||
|
}
|
||||||
|
} else if (intent.getAction().equals(ACTION_SETTIME)) {
|
||||||
|
if (mBtSocketIoThread != null) {
|
||||||
|
byte[] msg = PebbleProtocol.encodeSetTime(-1);
|
||||||
mBtSocketIoThread.write(msg);
|
mBtSocketIoThread.write(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,6 +138,40 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class BtSocketAcceptThread extends Thread {
|
||||||
|
private final BluetoothServerSocket mmServerSocket;
|
||||||
|
|
||||||
|
public BtSocketAcceptThread() {
|
||||||
|
BluetoothServerSocket tmp = null;
|
||||||
|
try {
|
||||||
|
tmp = mBtAdapter.listenUsingRfcommWithServiceRecord("PebbleListener", PEBBLE_UUID);
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
mmServerSocket = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
mBtSocket = mmServerSocket.accept();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (mBtSocket != null) {
|
||||||
|
try {
|
||||||
|
mBtSocketIoThread = new BtSocketIoThread(mBtSocket.getInputStream(), mBtSocket.getOutputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
mBtSocketIoThread.start();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class BtSocketIoThread extends Thread {
|
private class BtSocketIoThread extends Thread {
|
||||||
private final InputStream mmInStream;
|
private final InputStream mmInStream;
|
||||||
private final OutputStream mmOutStream;
|
private final OutputStream mmOutStream;
|
||||||
@@ -141,6 +188,7 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
byte[] ping = {0, 0, 0, 0};
|
byte[] ping = {0, 0, 0, 0};
|
||||||
|
byte[] version = {0x00, 0x0d, 0x00, 0x11, 0x01, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x72};
|
||||||
// Read from the InputStream
|
// Read from the InputStream
|
||||||
//bytes = mmInStream.read(buffer,0,2);
|
//bytes = mmInStream.read(buffer,0,2);
|
||||||
//ByteBuffer buf = ByteBuffer.wrap(buffer);
|
//ByteBuffer buf = ByteBuffer.wrap(buffer);
|
||||||
@@ -149,7 +197,7 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
//Log.e(TAG,Integer.toString(length+4) + " as total length");
|
//Log.e(TAG,Integer.toString(length+4) + " as total length");
|
||||||
bytes = mmInStream.read(buffer);
|
bytes = mmInStream.read(buffer);
|
||||||
Log.e(TAG, Integer.toString(bytes) + ": " + PebbleProtocol.decodeResponse(buffer));
|
Log.e(TAG, Integer.toString(bytes) + ": " + PebbleProtocol.decodeResponse(buffer));
|
||||||
write(ping);
|
//write(version);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public class ControlCenter extends ActionBarActivity {
|
|||||||
Button sendButton;
|
Button sendButton;
|
||||||
Button testNotificationButton;
|
Button testNotificationButton;
|
||||||
Button startServiceButton;
|
Button startServiceButton;
|
||||||
|
Button setTimeButton;
|
||||||
EditText editTitle;
|
EditText editTitle;
|
||||||
EditText editContent;
|
EditText editContent;
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@ public class ControlCenter extends ActionBarActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
|
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
|
||||||
startIntent.setAction(BluetoothCommunicationService.ACTION_STARTBLUETOOTHCOMMUNITCATIONSERVICE);
|
startIntent.setAction(BluetoothCommunicationService.ACTION_START);
|
||||||
startService(startIntent);
|
startService(startIntent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -46,12 +47,21 @@ public class ControlCenter extends ActionBarActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
|
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
|
||||||
startIntent.setAction(BluetoothCommunicationService.ACTION_SENDBLUETOOTHMESSAGE);
|
startIntent.setAction(BluetoothCommunicationService.ACTION_SENDMESSAGE);
|
||||||
startIntent.putExtra("notification_title", editTitle.getText().toString());
|
startIntent.putExtra("notification_title", editTitle.getText().toString());
|
||||||
startIntent.putExtra("notification_content", editTitle.getText().toString());
|
startIntent.putExtra("notification_content", editTitle.getText().toString());
|
||||||
startService(startIntent);
|
startService(startIntent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
setTimeButton = (Button) findViewById(R.id.setTimeButton);
|
||||||
|
setTimeButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
|
||||||
|
startIntent.setAction(BluetoothCommunicationService.ACTION_SETTIME);
|
||||||
|
startService(startIntent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
testNotificationButton = (Button) findViewById(R.id.testNotificationButton);
|
testNotificationButton = (Button) findViewById(R.id.testNotificationButton);
|
||||||
testNotificationButton.setOnClickListener(new View.OnClickListener() {
|
testNotificationButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class NotificationListener extends NotificationListenerService {
|
|||||||
|
|
||||||
if (content != null) {
|
if (content != null) {
|
||||||
Intent startIntent = new Intent(NotificationListener.this, BluetoothCommunicationService.class);
|
Intent startIntent = new Intent(NotificationListener.this, BluetoothCommunicationService.class);
|
||||||
startIntent.setAction(BluetoothCommunicationService.ACTION_SENDBLUETOOTHMESSAGE);
|
startIntent.setAction(BluetoothCommunicationService.ACTION_SENDMESSAGE);
|
||||||
startIntent.putExtra("notification_title", title);
|
startIntent.putExtra("notification_title", title);
|
||||||
startIntent.putExtra("notification_content", content);
|
startIntent.putExtra("notification_content", content);
|
||||||
startService(startIntent);
|
startService(startIntent);
|
||||||
|
|||||||
@@ -92,9 +92,11 @@ public class PebbleProtocol {
|
|||||||
return encodeMessage(ENDPOINT_NOTIFICATION, NOTIFICATION_EMAIL, parts);
|
return encodeMessage(ENDPOINT_NOTIFICATION, NOTIFICATION_EMAIL, parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] encodeSetTime() {
|
public static byte[] encodeSetTime(long ts) {
|
||||||
long ts = System.currentTimeMillis() / 1000;
|
if (ts == -1) {
|
||||||
ts += SimpleTimeZone.getDefault().getOffset(ts) / 1000;
|
ts = System.currentTimeMillis() / 1000;
|
||||||
|
ts += SimpleTimeZone.getDefault().getOffset(ts) / 1000;
|
||||||
|
}
|
||||||
ByteBuffer buf = ByteBuffer.allocate(LENGTH_SETTIME);
|
ByteBuffer buf = ByteBuffer.allocate(LENGTH_SETTIME);
|
||||||
buf.order(ByteOrder.BIG_ENDIAN);
|
buf.order(ByteOrder.BIG_ENDIAN);
|
||||||
buf.putShort((short) (LENGTH_SETTIME - LENGTH_PREFIX));
|
buf.putShort((short) (LENGTH_SETTIME - LENGTH_PREFIX));
|
||||||
|
|||||||
@@ -60,4 +60,13 @@
|
|||||||
android:layout_above="@+id/sendButton"
|
android:layout_above="@+id/sendButton"
|
||||||
android:layout_alignEnd="@+id/sendButton"
|
android:layout_alignEnd="@+id/sendButton"
|
||||||
android:layout_alignParentStart="true" />
|
android:layout_alignParentStart="true" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="set time"
|
||||||
|
android:id="@+id/setTimeButton"
|
||||||
|
android:layout_above="@+id/startServiceButton"
|
||||||
|
android:layout_alignEnd="@+id/startServiceButton"
|
||||||
|
android:layout_alignParentStart="true" />
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|||||||
Reference in New Issue
Block a user