Add ability to receive intents to com.banglejs.uart.tx (from apps like tasker) and send them to Bangle.js

This commit is contained in:
Gordon Williams 2022-05-18 21:03:20 +01:00
parent 96654a01fb
commit 031898f538
2 changed files with 60 additions and 14 deletions

View File

@ -105,7 +105,6 @@ import javax.xml.xpath.XPathFactory;
public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(BangleJSDeviceSupport.class); private static final Logger LOG = LoggerFactory.getLogger(BangleJSDeviceSupport.class);
private final BroadcastReceiver commandReceiver;
private BluetoothGattCharacteristic rxCharacteristic = null; private BluetoothGattCharacteristic rxCharacteristic = null;
private BluetoothGattCharacteristic txCharacteristic = null; private BluetoothGattCharacteristic txCharacteristic = null;
@ -117,36 +116,83 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
private boolean realtimeStep = false; private boolean realtimeStep = false;
private int realtimeHRMInterval = 30*60; private int realtimeHRMInterval = 30*60;
// Local Intents - for app manager communication
public static final String BANGLEJS_COMMAND_TX = "banglejs_command_tx"; public static final String BANGLEJS_COMMAND_TX = "banglejs_command_tx";
public static final String BANGLEJS_COMMAND_RX = "banglejs_command_rx"; public static final String BANGLEJS_COMMAND_RX = "banglejs_command_rx";
// Global Intents
private static final String BANGLE_ACTION_UART_TX = "com.banglejs.uart.tx";
public BangleJSDeviceSupport() { public BangleJSDeviceSupport() {
super(LOG); super(LOG);
addSupportedService(BangleJSConstants.UUID_SERVICE_NORDIC_UART); addSupportedService(BangleJSConstants.UUID_SERVICE_NORDIC_UART);
registerLocalIntents();
registerGlobalIntents();
}
private void registerLocalIntents() {
IntentFilter commandFilter = new IntentFilter(); IntentFilter commandFilter = new IntentFilter();
commandFilter.addAction(BANGLEJS_COMMAND_TX); commandFilter.addAction(BANGLEJS_COMMAND_TX);
commandReceiver = new BroadcastReceiver() { BroadcastReceiver commandReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) { switch (intent.getAction()) {
case BANGLEJS_COMMAND_TX: { case BANGLEJS_COMMAND_TX: {
String data = String.valueOf(intent.getExtras().get("DATA")); String data = String.valueOf(intent.getExtras().get("DATA"));
try { try {
TransactionBuilder builder = performInitialized("TX"); TransactionBuilder builder = performInitialized("TX");
uartTx(builder, data); uartTx(builder, data);
builder.queue(getQueue()); builder.queue(getQueue());
} catch (IOException e) { } catch (IOException e) {
GB.toast(getContext(), "Error in TX: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "Error in TX: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
}
break;
} }
break;
} }
} }
}
}; };
LocalBroadcastManager.getInstance(GBApplication.getContext()).registerReceiver(commandReceiver, commandFilter); LocalBroadcastManager.getInstance(GBApplication.getContext()).registerReceiver(commandReceiver, commandFilter);
} }
private void registerGlobalIntents() {
IntentFilter commandFilter = new IntentFilter();
commandFilter.addAction(BANGLE_ACTION_UART_TX);
BroadcastReceiver commandReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case BANGLE_ACTION_UART_TX: {
/* In Tasker:
Action: com.banglejs.uart.tx
Cat: None
Extra: line:Terminal.println(%avariable)
Target: Broadcast Receiver
Variable: Number, Configure on Import, NOT structured, Value set, Nothing Exported, NOT Same as value
*/
Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()));
if (!devicePrefs.getBoolean(PREF_DEVICE_INTENTS, false)) return;
String data = intent.getStringExtra("line");
if (data==null) {
GB.toast(getContext(), "UART TX Intent, but no 'line' supplied", Toast.LENGTH_LONG, GB.ERROR);
return;
}
if (!data.endsWith("\n")) data += "\n";
try {
TransactionBuilder builder = performInitialized("TX");
uartTx(builder, data);
builder.queue(getQueue());
} catch (IOException e) {
GB.toast(getContext(), "Error in TX: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
}
break;
}
}
}
};
GBApplication.getContext().registerReceiver(commandReceiver, commandFilter);
}
@Override @Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) { protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
LOG.info("Initializing"); LOG.info("Initializing");

View File

@ -269,7 +269,7 @@
<string name="pref_title_device_internet_access">Allow Internet Access</string> <string name="pref_title_device_internet_access">Allow Internet Access</string>
<string name="pref_summary_device_internet_access">Allow apps on this device to access the internet</string> <string name="pref_summary_device_internet_access">Allow apps on this device to access the internet</string>
<string name="pref_title_device_intents">Allow Intents</string> <string name="pref_title_device_intents">Allow Intents</string>
<string name="pref_summary_device_intents">Allow apps on this device to send Android Intents</string> <string name="pref_summary_device_intents">Allow Bangle.js watch apps to send Android Intents, and allow other apps on Android (like Tasker) to send data to Bangle.js with the com.banglejs.uart.tx Intent.</string>
<string name="pref_summary_sync_calendar">Enables calendar alerts, even when disconnected</string> <string name="pref_summary_sync_calendar">Enables calendar alerts, even when disconnected</string>
<string name="pref_title_sync_caldendar">Sync calendar events</string> <string name="pref_title_sync_caldendar">Sync calendar events</string>
<string name="pref_summary_relax_firmware_checks">Relax firmware checks</string> <string name="pref_summary_relax_firmware_checks">Relax firmware checks</string>