Skip to content

Commit 120558e

Browse files
author
Mayur Patel
committed
Added TTS support
1 parent a3d15fd commit 120558e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/android/BLECentralPlugin.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import android.os.Looper;
3131
import android.provider.Settings;
32+
import android.speech.tts.TextToSpeech;
33+
3234
import org.apache.cordova.CallbackContext;
3335
import org.apache.cordova.CordovaArgs;
3436
import org.apache.cordova.CordovaPlugin;
@@ -47,6 +49,7 @@ public class BLECentralPlugin extends CordovaPlugin implements BluetoothAdapter.
4749

4850
// actions
4951
private static final String SCAN = "scan";
52+
private static final String SAY = "say";
5053
private static final String PARTIAL_SCAN = "partialScan";
5154
private static final String START_SCAN = "startScan";
5255
private static final String STOP_SCAN = "stopScan";
@@ -103,8 +106,9 @@ public class BLECentralPlugin extends CordovaPlugin implements BluetoothAdapter.
103106
String serviceUUIDString; // When looking for a partial match this is the string to use
104107
boolean partialMatch = false; // Used when looking for a partial match
105108
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};
107110

111+
TextToSpeech speech;
108112

109113
// Bluetooth state notification
110114
CallbackContext stateCallback;
@@ -141,6 +145,19 @@ public void onReset() {
141145
removeStateListener();
142146
}
143147

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+
144161
@Override
145162
public boolean execute(final String action,final CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
146163

@@ -174,6 +191,14 @@ public void run() {
174191
partialMatch = false;
175192
resetScanOptions();
176193
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+
177202
} else if (action.equals(PARTIAL_SCAN)) {
178203

179204
partialMatch = args.getBoolean(1);

src/ios/BLECentralPlugin.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#import "BLECentralPlugin.h"
2020
#import <Cordova/CDV.h>
21+
@import AVFoundation;
2122

2223
@interface BLECentralPlugin() {
2324
NSDictionary *bluetoothStates;
@@ -66,6 +67,16 @@ - (void)pluginInitialize {
6667

6768
#pragma mark - Cordova Plugin Methods
6869

70+
- (void)say: (CDVInvokedUrlCommand *)command {
71+
72+
NSString *textToSpeak = [command.arguments objectAtIndex:0];
73+
AVSpeechUtterance *utterance = [AVSpeechUtterance
74+
speechUtteranceWithString:textToSpeak];
75+
76+
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
77+
[synth speakUtterance:utterance];
78+
79+
}
6980
- (void)connect:(CDVInvokedUrlCommand *)command {
7081

7182
NSLog(@"connect");

www/ble.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cordova.define("cordova-plugin-ble-central.ble", function(require, exports, module) {
12
// (c) 2014-2016 Don Coleman
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -49,6 +50,11 @@ function convertToNativeJS(object) {
4950

5051
module.exports = {
5152

53+
54+
say: function (textToSay, success, failure) {
55+
56+
cordova.exec(success, failure, 'BLE', 'say', [textToSay]);
57+
},
5258
scan: function (services, seconds, success, failure) {
5359
var successWrapper = function(peripheral) {
5460
convertToNativeJS(peripheral);
@@ -169,3 +175,5 @@ module.exports = {
169175
}
170176

171177
};
178+
179+
});

0 commit comments

Comments
 (0)