We believe that the key to getting students excited about computer science is to give them opportunities at ever younger ages to explore their creativity with computer science. Thats why were running the Google Code-in contest again this year, and todays the day students can go to the contest site, register and start looking for tasks that interest them.
Ignacio Rodriguez was just 10 years old when he became curious about Sugar, the open source learning platform introduced nationally to students in Uruguay when he was in elementary school. With the encouragement of his teacher, Ignacio started asking questions of the developers writing and maintaining the code and he started playing around with things, a great way to learn to code. When he turned 14 he entered the Google Code-in contest completing tasks that included writing two new web services for Sugar and he created four new Sugar activities. He even continued to mentor other students throughout the contest period. His enthusiasm for coding and making the software even better for future users earned him a spot as a 2013 Grand Prize Winner.
Ignacio is one of the 1,575 students from 78 countries that have participated in Google Code-in since 2010. We are encouraging 13-17 year old students to explore the many varied ways they can contribute to open source software development through the Google Code-in contest. Because open source is a collaborative effort, the contest is designed as a streamlined entry point for students into software development by having mentors assigned to each task that a student works on during the contest. Students dont have to be coders to participate; as with any software project, there are many ways to contribute to the project. Students will be able to choose from documentation, outreach, research, training, user interface and quality assurance tasks in addition to coding tasks.
This year, students can choose tasks created by 12 open source organizations working on
disaster relief, content management, desktop environments, gaming, medical record systems for developing countries, 3D solid modeling computer-aided design and operating systems to name a few.
For more information on the contest, please visit the contest site where you can find the timeline, Frequently Asked Questions and information on each of the open source projects students can work with during the seven week contest.
Previous post "Connect HM-10 (BLE Module) to Android device, with BluetoothLeGatt sample project" show step-by-step to modify Android BluetoothLeGatt sample program, by-pass Heart Rate Measurement profile handling to show function of HM-10.
Here we will send dummy data of Heart Rate Measurement profile by Arduino Due + HM-10.
First of all, edit BluetoothLeService.java to resume original handling for Heart Rate Measurement profile:
/* * 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. */
/** * Service for managing connection and data communication with a GATT server hosted on a * given Bluetooth LE device. */ public class BluetoothLeService extends Service { private final static String TAG = BluetoothLeService.class.getSimpleName();
private static final int STATE_DISCONNECTED = 0; private static final int STATE_CONNECTING = 1; private static final int STATE_CONNECTED = 2;
public final static String ACTION_GATT_CONNECTED = "com.example.bluetooth.le.ACTION_GATT_CONNECTED"; public final static String ACTION_GATT_DISCONNECTED = "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"; public final static String ACTION_GATT_SERVICES_DISCOVERED = "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED"; public final static String ACTION_DATA_AVAILABLE = "com.example.bluetooth.le.ACTION_DATA_AVAILABLE"; public final static String EXTRA_DATA = "com.example.bluetooth.le.EXTRA_DATA";
public final static UUID UUID_HEART_RATE_MEASUREMENT = UUID.fromString(SampleGattAttributes.HM_10);
// Implements callback methods for GATT events that the app cares about. For example, // connection change and services discovered. private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; broadcastUpdate(intentAction); Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());
private void broadcastUpdate(final String action) { final Intent intent = new Intent(action); sendBroadcast(intent); }
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action);
// This is special handling for the Heart Rate Measurement profile. Data parsing is // carried out as per profile specifications: // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { int flag = characteristic.getProperties(); int format = -1; if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; Log.d(TAG, "Heart rate format UINT16."); } else { format = BluetoothGattCharacteristic.FORMAT_UINT8; Log.d(TAG, "Heart rate format UINT8."); } final int heartRate = characteristic.getIntValue(format, 1); Log.d(TAG, String.format("Received heart rate: %d", heartRate)); intent.putExtra(EXTRA_DATA, String.valueOf(heartRate)); } else { // For all other profiles, writes the data formatted in HEX. final byte[] data = characteristic.getValue(); if (data != null && data.length > 0) { final StringBuilder stringBuilder = new StringBuilder(data.length); for(byte byteChar : data) stringBuilder.append(String.format("%02X ", byteChar)); intent.putExtra(EXTRA_DATA, new String(data) + " " + stringBuilder.toString()); } }
public class LocalBinder extends Binder { BluetoothLeService getService() { return BluetoothLeService.this; } }
@Override public IBinder onBind(Intent intent) { return mBinder; }
@Override public boolean onUnbind(Intent intent) { // After using a given device, you should make sure that BluetoothGatt.close() is called // such that resources are cleaned up properly. In this particular example, close() is // invoked when the UI is disconnected from the Service. close(); return super.onUnbind(intent); }
private final IBinder mBinder = new LocalBinder();
/** * Initializes a reference to the local Bluetooth adapter. * * @return Return true if the initialization is successful. */ public boolean initialize() { // For API level 18 and above, get a reference to BluetoothAdapter through // BluetoothManager. if (mBluetoothManager == null) { mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (mBluetoothManager == null) { Log.e(TAG, "Unable to initialize BluetoothManager."); return false; } }
mBluetoothAdapter = mBluetoothManager.getAdapter(); if (mBluetoothAdapter == null) { Log.e(TAG, "Unable to obtain a BluetoothAdapter."); return false; }
return true; }
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback. */ public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; }
// Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); if (mBluetoothGatt.connect()) { mConnectionState = STATE_CONNECTING; return true; } else { return false; } }
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; mConnectionState = STATE_CONNECTING; return true; }
/** * Disconnects an existing connection or cancel a pending connection. The disconnection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback. */ public void disconnect() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.disconnect(); }
/** * After using a given BLE device, the app must call this method to ensure resources are * released properly. */ public void close() { if (mBluetoothGatt == null) { return; } mBluetoothGatt.close(); mBluetoothGatt = null; }
/** * Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported * asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)} * callback. * * @param characteristic The characteristic to read from. */ public void readCharacteristic(BluetoothGattCharacteristic characteristic) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.readCharacteristic(characteristic); }
/** * Enables or disables notification on a give characteristic. * * @param characteristic Characteristic to act on. * @param enabled If true, enable notification. False otherwise. */ public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// This is specific to Heart Rate Measurement. if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { BluetoothGattDescriptor descriptor = characteristic.getDescriptor( UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
/** * Retrieves a list of supported GATT services on the connected device. This should be * invoked only after {@code BluetoothGatt#discoverServices()} completes successfully. * * @return A {@code List} of supported services. */ public List<BluetoothGattService> getSupportedGattServices() { if (mBluetoothGatt == null) return null;
return mBluetoothGatt.getServices(); } }
Connect Arduino Due and HM-10: HM-10 VCC - Arduino Due 3.3V HM-10 GND - Arduino Due GND HM-10 Tx - Arduino Due Rx HM-10 Rx - Arduino Due Tx
Program on Arduino Due, DueSimHeartRate.ino. It only send dummy heart rate data to serial port.
This is a step by step guide to upgrade ones source code that was developed for an earlier version of android SDK, as is to work on a new version of SDK installed on your development environment
Pre-requisites: 1. You are using Eclipse IDE - Ganymede 2. You have installed SDK 2.1 on your machine 3. You have upgraded ADT to 0.96 on Eclipse IDE 4. You have pointed the Eclipse IDE to the new SDK 2.1 5. You have changed the default JDK compiler version on Eclipse to 1.6 6. You have created an Android Virtual Device (AVD) that uses the SDK 2.1 / Google API
NOTE: Upgrading SDK and Eclipse installations itself is provided in detail at http://developer.android.com/sdk/index.html & http://developer.android.com/sdk/installing.html
For each project that you have in your eclipse workspace that was written for the earlier version of SDK, you need to do the following for basic upgrade (this does not include the upgrade of APIs that have be deprecated):
Step-by-step guide 1. Right click the project and go to Project Properties -> Android 2. You wil see tha right pane showing the Project Build Target 3. In this pane you will see either or both: Android 2.1 & Google APIs depending on what you chose to install when upgrading your ADT to 0.96 4. Select Google Android 2.1 or Google API whichever is required by your earlier project (note you might have used only Android 1.5 for most projects. You would need Google API only for those projects that use google maps) 5. Then, go to the Project menu and click on clean. Note, this is an optional step. You may have to do this if you get the error Conversion to Dalvik format failed with error 1. 6. Then, build the project by going to Project -> build 7. You are done. You can now run the earlier Android application using the new SDK 2.1
Last example show "Get ISO country code for the given latitude/longitude, using GeoNames Web Service, using HttpURLConnection". GeoNames provide Java Client for GeoNames Webservices to help developers to easily access the geonames web services with java. This post show how to use it in Android.
To use Java Client for GeoNames Webservices in you Android Studio project, you have to download both geonames-1.1.13.jar and jdom-1.0.jar to your local machine. Visit http://www.geonames.org/source-code/ to download.
Then you have to add the JAR modules in your Android Studio Project, refer to the video.
dependencies of :geonames-1.1.13 and :jdom-1.0 will be added in your build.gradle.
Example to get ISO country code for the given latitude/longitude, using GeoNames Java Client:
try{ lat = Double.parseDouble(strLat); }catch (NumberFormatException ex){ parsable = false; Toast.makeText(MainActivity.this, "Latitude does not contain a parsable double", Toast.LENGTH_LONG).show(); }
try{ lon = Double.parseDouble(strLon); }catch (NumberFormatException ex){ parsable = false; Toast.makeText(MainActivity.this, "Longitude does not contain a parsable double", Toast.LENGTH_LONG).show(); }
if(parsable){ new GeoNamesTask(textResult).execute(lat, lon); }
} }); }
private class GeoNamesTask extends AsyncTask<Double, Void, String> { TextView tResult;
public GeoNamesTask(TextView vResult){ tResult = vResult; tResult.setText(""); }
/* Do not use the demo account for your app or your tests. It is only meant for the sample links on the documentation pages. Create your own account instead. */ WebService.setUserName("demo");
The GeoNames geographical database covers all countries and contains over eight million placenames that are available for download free of charge. We can get the iso country code for any given latitude/longitude, using GeoNames webservices; api.geonames.org/countryCode?
CountryCode / reverse geocoding
The iso country code of any given point. Webservice Type : REST Url : api.geonames.org/countryCode? Parameters : lat,lng, type, lang, radius (buffer in km for closest country in coastal areas, a positive buffer expands the positiv area whereas a negative buffer reduces it); Result : returns the iso country code for the given latitude/longitude With the parameter type=xml this service returns an xml document with iso country code and country name. The optional parameter lang can be used to specify the language the country name should be in. JSON output is produced with type=JSON Example http://api.geonames.org/countryCode?lat=47.03&lng=10.2&username=demo
Important:
Do not use the demo account for your app or your tests. It is only meant for the sample links on the documentation pages. Create your own account instead.
The parameter username needs to be passed with each request. The username for your application can be registered here. You will then receive an email with a confirmation link and after you have confirmed the email you can enable your account for the webservice on your account page
Dont forget to url encode string parameters containing special characters or spaces. (Faq entry on url encoding)
/* Do not use the demo account for your app or your tests. It is only meant for the sample links on the documentation pages. Create your own account instead. */ String queryString = "http://api.geonames.org/countryCode?lat=" + params[0] + "&lng=" + params[1] + "&username=demo";
String s = ""; try { s = sendQuery(queryString); } catch (IOException e) { e.printStackTrace(); s = e.getMessage(); } return s; }
Next: GeoNames also provide Java Client for GeoNames Webservices to help developers to easily access the geonames web services with java. Next post "Get ISO country code for the given latitude/longitude, using GeoNames Java Client" show how to use it in Android Studio project. Related: - Find addresses of given latitude and longitude using android.location.Geocoder
One of the most useful pieces of information you can get from a network interface is the list of IP addresses that are assigned to it. You can obtain this information from a NetworkInterface instance by using one of two methods. The first method, getInetAddresses(), returns an Enumeration of InetAddress. The other method, getInterfaceAddresses(), returns a list of java.net.InterfaceAddress instances. This method is used when you need more information about an interface address beyond its IP address. For example, you might need additional information about the subnet mask and broadcast address when the address is an IPv4 address, and a network prefix length in the case of an IPv6 address. ~ https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html
The following example program lists all the network interfaces and their addresses on a machine (tested on PC running Windows 10):
I got a lot of requests for alternate download sites (for the Sample Code). So, I have put all the sample code into one page and here is the link to the same. Hope this helps you.