Java tutorial
/* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.kraker.myocmd.TestService1; import android.app.Activity; import android.app.Dialog; import android.app.Fragment; import android.app.FragmentManager; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; import android.speech.tts.TextToSpeech; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.UUID; import com.kraker.myocmd.R; public class DeviceControlActivity extends ActionBarActivity { private final static String TAG = DeviceControlActivity.class.getSimpleName(); private final static String Chart_name = "Evolution du courant"; public static final String EXTRAS_DEVICE_NAME = "Name"; public static final String EXTRAS_DEVICE_ADDRESS = "Address"; private String mDeviceAddress; private static BluetoothLeService mBluetoothLeService; private static boolean mConnected = false; private static BluetoothGattCharacteristic characteristicTX; private static BluetoothGattCharacteristic characteristicRX; public final static UUID HM_RX_TX = UUID.fromString(SampleGattAttributes.HM_RX_TX); public final static UUID HM_10_CONF = UUID.fromString(SampleGattAttributes.HM_10_CONF); private final String LIST_NAME = "NAME"; private final String LIST_UUID = "UUID"; android.os.Handler mHandler; int repeatInterval = 100; private String[] mNavigationDrawerItemTitles; private DrawerLayout mDrawerLayout; private ListView mDrawerList; ActionBarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; private int bufferEnvoi = 0; Button buttonPost; private String[] cocktailArray = { "Vodka", "Whisky", "Rhum", "Tequilla", "Pastis", "Soft" }; private ArrayAdapter arrayAdapter; private ArrayList<String> spinnerArray; private Handler myHandler; private TextView returnedText; private ToggleButton toggleButton; private ProgressBar progressBar; private SpeechRecognizer speech = null; private Intent recognizerIntent; private String LOG_TAG = "VoiceRecognitionActivity"; private TextToSpeech tts; // Gestion du service Bluetooth. private final ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder service) { mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); if (!mBluetoothLeService.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); finish(); } mBluetoothLeService.connect(mDeviceAddress); } @Override public void onServiceDisconnected(ComponentName componentName) { mBluetoothLeService = null; } }; private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { mConnected = true; updateConnectionState(R.string.connected); invalidateOptionsMenu(); } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { mConnected = false; updateConnectionState(R.string.disconnected); invalidateOptionsMenu(); } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { // Show all the supported services and characteristics on the user interface. displayGattServices(mBluetoothLeService.getSupportedGattServices()); } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { newData(intent.getStringExtra(mBluetoothLeService.EXTRA_DATA)); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.device_control); mTitle = mDrawerTitle = getTitle(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); if (toolbar != null) { //setSupportActionBar(toolbar); } final Intent intent = getIntent(); String mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME); mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS); Intent gattServiceIntent = new Intent(this, BluetoothLeService.class); bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE); mHandler = new android.os.Handler(); mHandler.postDelayed(mStatusChecker, 5000); DisplayMetrics metrics = new DisplayMetrics(); } Runnable mStatusChecker = new Runnable() { @Override public void run() { mHandler.postDelayed(mStatusChecker, repeatInterval); } }; @Override protected void onResume() { super.onResume(); registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); if (mBluetoothLeService != null) { final boolean result = mBluetoothLeService.connect(mDeviceAddress); Log.d(TAG, "Connect request result=" + result); } } @Override protected void onPause() { super.onPause(); mHandler.removeCallbacks(mStatusChecker); mBluetoothLeService.close(); unregisterReceiver(mGattUpdateReceiver); } @Override protected void onDestroy() { super.onDestroy(); mHandler.removeCallbacks(mStatusChecker); unbindService(mServiceConnection); mBluetoothLeService.close(); mBluetoothLeService = null; } @Override public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } private void updateConnectionState(final int resourceId) { runOnUiThread(new Runnable() { @Override public void run() { } }); } private void displayGattServices(List<BluetoothGattService> gattServices) { if (gattServices == null) return; String uuid = null; String unknownServiceString = getResources().getString(R.string.unknown_service); ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>(); // Loops through available GATT Services. for (BluetoothGattService gattService : gattServices) { HashMap<String, String> currentServiceData = new HashMap<String, String>(); uuid = gattService.getUuid().toString(); currentServiceData.put(LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString)); currentServiceData.put(LIST_UUID, uuid); gattServiceData.add(currentServiceData); characteristicTX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX); characteristicRX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX); } } private static IntentFilter makeGattUpdateIntentFilter() { final IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED); intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED); intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED); intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE); return intentFilter; } public static void envoiOrdreBT(String code) { String str = code; Log.d(TAG, "Sending command=" + str); final byte[] tx = str.getBytes(); if (mConnected) { characteristicTX.setValue(tx); mBluetoothLeService.writeCharacteristic(characteristicTX); mBluetoothLeService.setCharacteristicNotification(characteristicRX, true); } } public void newData(String data) { Toast.makeText(getApplicationContext(), "New data : " + data, Toast.LENGTH_LONG).show(); } public void testBT(View v) { envoiOrdreBT("S/3/S"); } }