Mobile App communication with Inverter through Bluetooth

With Internet of Things (IoT) adoption, OEMs are building connected products. The first step to build connected ecosystem is to enable wireless communication on edge devices. The objective of this blog is to help how to use the Bluetooth technologies to communicate with devices wirelessly. Problem We are working with leading solar inverter manufacturer. They have requirement to monitor power generation, storage and consumption of remotely deployed rooftop solar panels. As remote location does not have GSM connectivity, it’s always challenge to retrive data. Considering scale and low cost business model, Bluetooth technology was clear choice solution. Solution We have build Mobile app that interact with Inverter over Bluetooth. Providing reliable and cost effective solution to the end user is main motto behind this. User has to pair with Inverter and click few buttons to retrive data. The same data would be uploaded to cloud when Mobile comes in wi-fi or data coverage. package com.example.bluetoothconnection; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } Source 1. Starting code The first programming step is to create a new Android Application Project in Android Studio. Doing so will generate code similar to that in Source 1. The first thing the program should do is determine if the Android device supports Bluetooth. To do this, create a BluetoothAdapter object using the function getDefaultAdapter(). If this returns null, then the Android device does not support Bluetooth. Source 2 shows how to do this. Add this code to OnCreate(). package com.example.bluetoothconnection; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) {...

Android Geofence – Entry and Exit

Geofencing allows you to send notifications to your users in Android. when they are in the vicinity of, for example, your store. By doing so, you only provide your users with information relevant to them and increase your app usage, foot-traffic and conversion.Using Android Geofencing ,we are identify Exit and Entry point of users. A Geofence is a virtual perimeter around a location. You can create Geofences of 50 to 50.000 meters radius to send location-based notifications to users approaching, for example, your stores, coffee shops or events. Create Geofences Creating Geofences in the Plot Dashboard is as easy as riding your bike. You can search for a place or address and then set the name and radius of the Geofence. Creating Geofences on a map gives you perfect control of where your users will receive your Location Based Notifications. public Geofence toGeofence() { // Build a new Geofence object return new Geofence.Builder() .setRequestId(Long.toString(id)) .setTransitionTypes(transitionType) .setCircularRegion(latitude, longitude, radius) .setExpirationDuration(expirationDuration) .build(); } Geofence Enter When user exit the geofence , user getting trigger and showing that you are entering geofence. public class ReceiveTransitionsIntentService extends IntentService { @Override protected void onHandleIntent(Intent intent) { int transition = LocationClient.getGeofenceTransition(intent); if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER) || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)){ if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) { //bussiness logic AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVice); Intent broadcast_intent = new Intent(this, GeofencigNotificationReciever.class); broadcast_intent.putExtra("EventId", eventId); //data to pass broadcast_intent.putExtra("TransitionType", transitionType); PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, broadcast_intent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent); } } } }   Geofence Exit When user exit the geofence,user getting trigger and showing that you are exit geofence. public class ReceiveTransitionsIntentService extends IntentService { protected void onHandleIntent(Intent intent) { int transition = LocationClient.getGeofenceTransition(intent);...

Android Push Notifications using GCM

“Google Cloud Messaging for Android (GCM) is a service that helps developers send data from servers to their Android applications on Android devices”. Using this service you can send data to your application whenever new data is available instead of making requests to server in timely fashion. Integrating GCM in your android application enhances user experience and saves lot of battery power. Google Cloud Messaging GCM API We can use GCM as a notification engine. On an event it will send a notification to an android application and vice-versa. A notification is a small piece of information. Using GCM, maximum 4kb of payload can be sent in a notification. For now GCM is a free service – lets use responsibly 🙂 Android App can receive the message from Google cloud messaging server (GCM) even if the app is not running via Intent broadcasting (our example app will demonstrate this). Manifest Let’s start with our manifest. We’ll want to add some permissions under the manifest markup. <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="your.package.name.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="your.package.name.permission.C2D_MESSAGE" /> Next step is to register our Broadcast Receiver and Service. You should already have that last line in your manifest if you added the google play services properly. <receiver android:name=".GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="your.package.name" /> </intent-filter> </receiver> <service android:name=".GcmIntentService" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> Broadcast Receiver We are going to start by creating a new class which extends WakefulBroadcastReceiver. public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName()); startWakefulService(context, (intent.setComponent(comp))); setResultCode(Activity.RESULT_OK); } } The...