package com.arduinoandroid.talktoarduino; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattDescriptor; import android.content.Intent; import android.os.Bundle; import android.speech.RecognizerIntent; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.arduinoandroid.talktoarduino.bluetooth.BluetoothUtils; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.UUID; public class SpeechActivity extends Activity { private static final int VOICE_RECOGNITION_REQUEST = 1; //Getting the name for Log Tags private final String LOG_TAG = SpeechActivity.class.getSimpleName(); //Declare U.I Elements private Button startTalk; private Button refresh; private EditText speechInput; private TextView btv; // UUIDs for UAT service and associated characteristics. public static UUID UART_UUID = UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); public static UUID TX_UUID = UUID.fromString("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"); public static UUID RX_UUID = UUID.fromString("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"); // UUID for the BTLE client characteristic which is necessary for notifications. public static UUID CLIENT_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); // BTLE stateta private BluetoothAdapter adapter; private BluetoothGatt gatt; private BluetoothGattCharacteristic tx; private BluetoothGattCharacteristic rx; private boolean areServicesAccessible = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_speech); startTalk = (Button) findViewById(R.id.talktoArduino); refresh = (Button) findViewById(R.id.refreshBtn); speechInput = (EditText) findViewById(R.id.recordedTalk); btv = (TextView) findViewById(R.id.btView); startTalk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { recordSpeech(); } }); refresh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { restartScan(); } }); } public void recordSpeech() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You can now send a command to the Arduino"); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST && resultCode == RESULT_OK) { ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); String userInput = matches.get(0); TextView textSaid = (TextView) findViewById(R.id.recordedTalk); textSaid.setText(matches.get(0)); //add an if else loop or case statement if (userInput.equalsIgnoreCase("switch on")) { String setOutputMessage = "/digital/7/1 /"; tx.setValue(setOutputMessage.getBytes(Charset.forName("UTF-8"))); if (gatt.writeCharacteristic(tx)) { writeSensorData("Sent: " + setOutputMessage); } else { writeSensorData("Couldn't write TX characteristic!"); } } else if (userInput.equalsIgnoreCase("switch off")) { String setOutputMessage = "/digital/7/0 /"; tx.setValue(setOutputMessage.getBytes(Charset.forName("UTF-8"))); if (gatt.writeCharacteristic(tx)) { writeSensorData("Sent: " + setOutputMessage); } else { writeSensorData("Couldn't write TX characteristic!"); } } } super.onActivityResult(requestCode, resultCode, data); } private void writeSensorData(final CharSequence text) { Log.e(LOG_TAG, text.toString()); btv.setText(text.toString()); } // BTLE device scanning bluetoothGattCallback. // Main BTLE device bluetoothGattCallback where much of the logic occurs. private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() { // Called whenever the device connection state changes, i.e. from disconnected to connected. @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothGatt.STATE_CONNECTED) { writeSensorData("Connected!"); // Discover services. if (!gatt.discoverServices()) { writeSensorData("Failed to start discovering services!"); } } else if (newState == BluetoothGatt.STATE_DISCONNECTED) { writeSensorData("Disconnected!"); } else { writeSensorData("Connection state changed. New state: " + newState); } } // Called when services have been discovered on the remote device. // It seems to be necessary to wait for this discovery to occur before // manipulating any services or characteristics. public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { writeSensorData("Service discovery completed!"); } else { writeSensorData("Service discovery failed with status: " + status); } // Save reference to each characteristic. tx = gatt.getService(UART_UUID).getCharacteristic(TX_UUID); rx = gatt.getService(UART_UUID).getCharacteristic(RX_UUID); // Setup notifications on RX characteristic changes (i.e. data received). // First call setCharacteristicNotification to enable notification. if (!gatt.setCharacteristicNotification(rx, true)) { writeSensorData("Couldn't set notifications for RX characteristic!"); } // Next update the RX characteristic's client descriptor to enable notifications. if (rx.getDescriptor(CLIENT_UUID) != null) { BluetoothGattDescriptor desc = rx.getDescriptor(CLIENT_UUID); desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); if (!gatt.writeDescriptor(desc)) { writeSensorData("Couldn't write RX client descriptor value!"); } } else { writeSensorData("Couldn't get RX client descriptor!"); } areServicesAccessible = true; } }; protected void onStart() { Log.d(LOG_TAG,"onStart has been called"); super.onStart(); // / Scan for all BTLE devices. // The first one with the UART service will be chosen--see the code in the scanCallback. adapter = BluetoothAdapter.getDefaultAdapter(); startScan(); } //When this Activity isn't visible anymore protected void onStop() { Log.d(LOG_TAG,"onStop has been called"); //disconnect and close Bluetooth Connection for better reliability if (gatt != null) { gatt.disconnect(); gatt.close(); gatt = null; tx = null; rx = null; } super.onStop(); } //BLUETOOTH METHODS private void startScan() { if (!adapter.isEnabled()) { adapter.enable(); } if (!adapter.isDiscovering()) { adapter.startDiscovery(); } writeSensorData("Scanning for devices..."); adapter.startLeScan(scanCallback); } private void stopScan() { if (adapter.isDiscovering()) { adapter.cancelDiscovery(); } writeSensorData("Stopping scan"); adapter.stopLeScan(scanCallback); } private void restartScan() { stopScan(); startScan(); } /** * Main callback following an LE device scan */ private BluetoothAdapter.LeScanCallback scanCallback = new BluetoothAdapter.LeScanCallback() { // Called when a device is found. @Override public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) { Log.d(LOG_TAG, bluetoothDevice.getAddress()); writeSensorData("Found device: " + bluetoothDevice.getAddress()); // Check if the device has the UART service. if (BluetoothUtils.parseUUIDs(bytes).contains(UART_UUID)) { // Found a device, stop the scan. adapter.stopLeScan(scanCallback); writeSensorData("Found UART service!"); // Connect to the device. // Control flow will now go to the bluetoothGattCallback functions when BTLE events occur. gatt = bluetoothDevice.connectGatt(getApplicationContext(), false, bluetoothGattCallback); } } }; }