Friday 30 July 2021

Bluetooth LE Audio

Hi Guys !!! Hope all is well

In my previous post  I have discussed BLE Advertisement and BLE scanning in Android.

Today I am going to shed some light on BLE Audio . Yes you read correctly  BLE Audio .

Bluetooth Special Interest Group (SIG) announced a new spin-off standard for audio, called LE (low-energy) Audio. The new framework allows two key features:

  • native support for hearing aids and 
  • audio sharing

bjjuhhk,j


Thursday 29 April 2021

Ping an IP Address : Device is connected to the internet or not

Hi Guys !!! Hope all is well

Today I am going to discuss about Ping in Android. Ping an IP Address used to determine if your device is connected to the internet. Below is the Ping Features : 

  • Ping is a network utility used to test reachable of an IP address or a host. 
  • It can measure the packet trip time which is called latency. 
  • Ping uses ICMP for request packets and waits for an ICMP response. 
  • Ping is useful to check a server's availability.
I am going to create one sample Android app to ping external IP address or AP Gateway IP Address. This application by default send 2 ping every minute. But it is configurable. You can increase or decrease ping rate either via UI or adb shell command.  You can download this app from my Github account.


Below is the code used in above app to ping given IP address
public boolean pingToServer(String host) {
//host = "192.168.1.65";
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 " + host);
int exitValue = ipProcess.waitFor();
Log.d(LOG_TAG, "ping host: "+host+" exitValue: "+exitValue);
return (exitValue == 0);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
Above code used Android RunTime API to ping. It is very easy way to ping without going to much in details and no need to app get signed
public void startAlarm(int pingValue) {
final Handler h = new Handler();
Log.d(LOG_TAG, "MyPingService: PingCount : "+pingValue);
long startTime = System.currentTimeMillis();
int intervalTime = 30*1000;
if(pingValue<=10){
intervalTime = (60/pingValue)*1000;
}
final int delay = intervalTime; //milliseconds
Log.d(LOG_TAG, "MyPingService: Ping interval Time : "+delay);
h.postDelayed(new Runnable(){
public void run(){
//send Ping
if(IP_Address!=null){
if (pingToServer(IP_Address))
Log.d(LOG_TAG, "ping is reachable");
else
Log.d(LOG_TAG, "ping is not reachable");
}else {
if (pingToServer(getGatewayIP()))
Log.d(LOG_TAG, "ping is reachable");
else
Log.d(LOG_TAG, "ping is not reachable");
}
if (!stopHandler) {
h.postDelayed(this, delay);
}
}
}, intervalTime);
}
Above code set pinging on predefined value example :- 4 ping/minute. I am using Handler API  postDelayed  method to set repeating task.  
I have added Android Service to keep this app running in background . You can download my project and check all source code in details. 

Thanks
Saurabh 

Happy Coding !!! 

Saturday 30 January 2021

Android JNI Example 3 : Handling Exception

Hi Guys !!! Hope all is well

This is my third post on Android JNI example series.  Previous two post link


In this example I am going to discuss following :- 
  • Handle Java/App exception from native(jni)
  • Catch java/App exception in native and return back it to App
  • Example of Fatal exception in native

Below is application screenshot


The example code is on my github page

https://github.com/Saurabh-12/AndroidJNI

You can see code snippet for above example on my github page. So i am not doing it here copy paste.

Thanks
Saurabh 

Happy Coding !!! 

Monday 4 January 2021

Android JNI Example 2

  Hi Guys !!! Hope all is well

In my previous post Android JNI Example 1I have discussed following : 
  • How to use latest Android Studio to develop Native application(NDK, JNI,CPP)
  • Received String from Native layer to Upper layer (App)
  • Send String from Application to native layer
  • Received String Array from Native to Upper layer
Now in Example 2, I am going to discuss following :- 
  • Call and set value for POJO or Getter/setter class in Native(jni/c++)
  • Call and set value for below Java Static and instance Method in Native(jni/c++) :
  • Boolean
  • Int
  • Float
  • Array
  • String
  • Access and set following Static and Instance variable value in Native(jni/c++):
  • Int
  • String
  • float

The example code is on my github page

https://github.com/Saurabh-12/AndroidJNI


You can see code snippet for above example on my github page. So i am not doing it here copy paste.



 

Thanks
Saurabh 
Happy Coding !!!

Android JNI Example 1

 Hi Guys !!! Hope all is well

I am going to show Android JNI example 1 using Android Studio.

In my previous blog on JNI and NDK, I have written below thread.

https://saurabhsharma123k.blogspot.com/2017/02/generate-so-file-by-using-ndk-and-use.html

https://saurabhsharma123k.blogspot.com/2017/07/exception-handling-in-jni-and-throwing.html

In this example, We can discuss below topic. 
  • Received String from Native layer to Upper layer (App)
  • Send String from Application to native layer
  • Received String Array from Native to Upper layer

The example code is on my github page

https://github.com/Saurabh-12/AndroidJNI

In this example, I am using Android Studio (version 4.x) to write both java and native code. Unlike my previous example where I use NDK tool to generate shared library(.so) file and then use that file inside Android application. 

Thanks to latest version of Android Studio(AS) to incorporate CMake, so that we can develop java native application seamlessly.

Note :- 1. For Framework developer(Android System programing) , they can leverage this A.S. feature to develop and test before pushing there native app in Android BSP.

2. App package detail (java and cpp file), gradle build details and other AS stuff directly download full source code from my github page and import it in AS.

You can see code snippet for above example on my github page. So i am not doing it here copy paste.







Thanks
Saurabh 
Happy Coding !!!

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...