29
29
30
30
import android .os .Looper ;
31
31
import android .provider .Settings ;
32
+ import android .speech .tts .TextToSpeech ;
33
+
32
34
import org .apache .cordova .CallbackContext ;
33
35
import org .apache .cordova .CordovaArgs ;
34
36
import org .apache .cordova .CordovaPlugin ;
@@ -47,6 +49,7 @@ public class BLECentralPlugin extends CordovaPlugin implements BluetoothAdapter.
47
49
48
50
// actions
49
51
private static final String SCAN = "scan" ;
52
+ private static final String SAY = "say" ;
50
53
private static final String PARTIAL_SCAN = "partialScan" ;
51
54
private static final String START_SCAN = "startScan" ;
52
55
private static final String STOP_SCAN = "stopScan" ;
@@ -103,8 +106,9 @@ public class BLECentralPlugin extends CordovaPlugin implements BluetoothAdapter.
103
106
String serviceUUIDString ; // When looking for a partial match this is the string to use
104
107
boolean partialMatch = false ; // Used when looking for a partial match
105
108
private int scanSeconds ;
106
- String [] validActions = {SCAN ,PARTIAL_SCAN ,START_SCAN ,STOP_SCAN ,START_SCAN_WITH_OPTIONS ,FIND_PAIRED_DEVICE ,LIST ,CONNECT ,DISCONNECT ,READ ,WRITE ,WRITE_WITHOUT_RESPONSE ,START_NOTIFICATION ,STOP_NOTIFICATION ,IS_ENABLED ,IS_CONNECTED ,ENABLE ,SETTINGS };
109
+ String [] validActions = {SCAN ,SAY , PARTIAL_SCAN ,START_SCAN ,STOP_SCAN ,START_SCAN_WITH_OPTIONS ,FIND_PAIRED_DEVICE ,LIST ,CONNECT ,DISCONNECT ,READ ,WRITE ,WRITE_WITHOUT_RESPONSE ,START_NOTIFICATION ,STOP_NOTIFICATION ,IS_ENABLED ,IS_CONNECTED ,ENABLE ,SETTINGS };
107
110
111
+ TextToSpeech speech ;
108
112
109
113
// Bluetooth state notification
110
114
CallbackContext stateCallback ;
@@ -141,6 +145,19 @@ public void onReset() {
141
145
removeStateListener ();
142
146
}
143
147
148
+ @ Override
149
+ protected void pluginInitialize () {
150
+ speech =new TextToSpeech (webView .getContext (), new TextToSpeech .OnInitListener () {
151
+ @ Override
152
+ public void onInit (int status ) {
153
+ if (status != TextToSpeech .ERROR ) {
154
+ speech .setLanguage (Locale .US );
155
+ }
156
+ }
157
+ });
158
+
159
+ }
160
+
144
161
@ Override
145
162
public boolean execute (final String action ,final CordovaArgs args , final CallbackContext callbackContext ) throws JSONException {
146
163
@@ -174,6 +191,14 @@ public void run() {
174
191
partialMatch = false ;
175
192
resetScanOptions ();
176
193
findLowEnergyDevices (callbackContext , serviceUUIDs , scanSeconds );
194
+ } else if (action .equals (SAY )) {
195
+
196
+ String textToSay = args .getString (0 );
197
+
198
+ if (speech !=null )
199
+ speech .speak (textToSay , TextToSpeech .QUEUE_FLUSH , null , "BLE_MEASUREMENTS" );
200
+
201
+
177
202
} else if (action .equals (PARTIAL_SCAN )) {
178
203
179
204
partialMatch = args .getBoolean (1 );
0 commit comments