[Tutorial] Programming for Sony Products [Xperia Camera.AddON][SourceCode] - Sony Cross-Device General

Programming for Xperia Smartphones (Camera Add-On)​
Camera Add-on feature on Xperia Camera App is way to gather all custom camera apps, that the may have New/Different way to Capture pictures. Maybe some Graphical Effects or as you will see in this tutorial another way to capture.
This is How Sony Mobile Devs explain the CameraW Add-On
All camera apps in one menu
The Camera Add-on API enables you to both add a shortcut to your imaging app from the camera apps mode selector, and you can add a shortcut to the smart social camera apps mode selector in your imaging app UI. This way the user can get a seamless camera experience, and make shifting in-between different camera apps really easy. read more...
Click to expand...
Click to collapse
Examples of Camera Add on App
- Live on YouTube – by Xperia™
- AR Effect
- Background Defocus
- Horror
- CameraW
### Let Start ###​
What you need is ability to create Hello World! project
In this Tutorial you Learn How to Create your Camera App
&​ Adding Xperia Camera Add-On Support
Click to expand...
Click to collapse
1. Install Sony Add-On SDK
- Open Android SDK Manager
- Tools > Manager Add-on Sites > User Defined Sites > New
- Now Enter this URL
HTML:
http://dl-developer.sonymobile.com/sdk_manager/Sony-Add-on-SDK.xml
- Close the Add-on Tools
- Relaunch SDK Manager & Go to Android API-19 section & Download
HTML:
Sony Add-on SDK-3.0
***
Ready to Programming for Xperia
^-^-^​ 2... (Click On Tutorial Link)
​ Tutorial to Start Programming for Xperia Camera Add-On
:Sample Codes:
#CaptureProximity
Don't forget to Hit Thanks​

Tutorial to Start Programming for Xperia Camera Add-On
2. Starting...
- Create New Android Project (Alt + Shift + N)
* Minimum Requirement: API 16
* Target: API 19/20
* Compile with: Sony Add-on SDK 3.0 (API-19)
Note:You can Add Xperia Camera Add-On support to your current custom camera Application by Adding few elements to your app.
- from Bottom to Top
This Sample as first sample is just modified Basic_Sample from SonyMobile
Modified to Capture & Save Image By Hovering Proximity Sensor
Click to expand...
Click to collapse
​* Open Manifest & Add require Permissions for using Add-On API, Camera Hardware & Storage.
The Storage permission is for create file in SD-Card for capture pics and Camera to get permission from system to allow us to use Camera hardware.
Code:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="com.sonymobile.permission.CAMERA_ADDON"/>
- Declare required library inside Application in manifest
Code:
<!-- If you want to create a camera app for just Xperia device you should set[B] required="true"[/B]. False means your can be install on other devices-->
<uses-library android:name="com.sonymobile.camera.addon.api" android:required="false"/>
- Then you will have your Activities declaration. You should add specific Intent-Filter & Meta-Data for the Activities that you want show on Camera Add-On menu. In this sample I set it for main activity.
Code:
<activity
android:name=".BasicCameraApp"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden"
android:launchMode="singleTask" >
<!-- The widowSoftInputMode is preferred to use But
launchMode="singleTask" must be set because there is another apps in Add-On menu that can launch or
switch between different modes -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.sonymobile.camera.addon.action.REGISTER_MODE" />
</intent-filter>
<meta-data android:name="com.sonymobile.camera.addon.MODE_ATTRIBUTES"
android:resource="@xml/sample_mode" />
<!-- This XML file save all required attributes for Info show in add-on menu and etc-->
</activity>
* Under res/ create xml folder & then .xml file to generate required meta-data
HTML:
res/xml/sample_mode.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<modes xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Do Not hard-code your won't register as add-on -->
<mode
<!-- [B]Remember [/B]the name="" because you need this to use it in classes cause this name will register
If you set incorrect your mode won't be available in modes menu -->
name="cameraw"
descriptionLabel="@string/app_name"
selectorIcon="@drawable/mode_icon"
shortcutIcon="@drawable/mode_shortcut_icon"
selectorLabel="@string/app_name"
shortcutLabel="@string/app_name" >
<uses-feature android:name="android.hardware.camera" />
</mode>
</modes>
* In layout folder you should add element for Preview of Camera. You can use TextureView, SurfaceView & FrameLayout. In this basic sample there is SurfaceView.
* In drawable you should add file for icon & shortcut of your add-on that must follow the guidelines. For better understand I upload the Xperia Camera drawable resources + what you can check in Sample Code drawable.
* Now Coding...
Code:
import com.sonymobile.camera.addon.capturingmode.CapturingModeSelector;
First you should define as specific parameter of Camera Add-On API
Code:
/**This class opens and closes the capturing mode selector. The mode selector provides access to all registered camera modes for camera add-ons available to the user. All methods must be called from the UI thread. **/
CapturingModeSelector mCapturingModeSelector;
- Then you need to define two listener for Selecting process & while closing the modes menu;
Code:
public static final String MODE_NAME = "cameraw";
Code:
private class MyOnModeSelectListener implements CapturingModeSelector.OnModeSelectListener {
Override
public void onModeSelect(String modeName) {
if (mCapturingModeSelector != null) {
mCapturingModeSelector.close();
mModeSelectorButton.setVisibility(View.VISIBLE);
}
}
}
Code:
private class MyOnModeFinishListener implements CapturingModeSelector.OnModeFinishListener {
Override
public void onModeFinish() {
if (mCapturingModeSelector != null) {
mCapturingModeSelector.close();
}
finish();
}
}
- The Handle the Click & Touch on Mode Button
Code:
private class ModeSelectorButtonTouchListener implements View.OnTouchListener {
Override
public boolean onTouch(View v, MotionEvent event) {
mModeSelectorButton.onTouchEvent(event);
if (mModeSelectorButton.isPressed()) {
mModeSelectorButton.setColorFilter(PRESSED_COLOR_FILTER);
} else {
mModeSelectorButton.clearColorFilter();
}
return true;
}
}
Code:
private class ModeSelectorButtonClickListener implements View.OnClickListener {
Override
public void onClick(View v) {
if (mCapturingModeSelector != null) {
[B]// This is name attributes in .xml file as meta-data[/B]
mCapturingModeSelector.open(MODE_NAME);
mModeSelectorButton.setVisibility(View.INVISIBLE);
mModeSelectorButton.clearColorFilter();
}
}
}
- Then at Override onCreate you should define them;
Code:
mModeSelectorButton.setOnClickListener(new ModeSelectorButtonClickListener());
mModeSelectorButton.setOnTouchListener(new ModeSelectorButtonTouchListener());
- & on @override onResume these. onResume for these to handle their action when user back to your app that she didn't close it completely so she need modes selector to switch modes;
Code:
mCapturingModeSelector.setOnModeSelectListener(new MyOnModeSelectListener());
mCapturingModeSelector.setOnModeFinishListener(new MyOnModeFinishListener());
NOTE: These are mandatory in your Activity that want to support add-on.
If you have already Camera App you should add what previously said up to here and you done!
It means these are common elements that you should add for Camera Add-On support.
^^^
^^^
*** Sample Codes ***
​* How to Capture By Proximity Sensor?
I.
Code:
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
II. implements you activity from SensorEventListener
Code:
public class BasicCameraApp extends Activity implements SensorEventListener{}
III. Override onCreate(Bundle Saved){} you must declare instance of Sensor & SensorManager
Code:
//define [COLOR=Blue][B]sensorManager [/B][/COLOR]& declare which sensor do you want to use
//that here is [COLOR=Blue][B]Proximity_SENSOR[/B][/COLOR]
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
Then you need to register the sensor to get data from it at Override onResume(){} cause this must be available for user when back to your app.
Code:
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
Now you have interaction with proximity sensor so we need to capture function and integrate it with sensorEvent.
Note: Always Unregister Sensors when user leave your app. It means you should release the camera, and all other sensor you are using in your app
Code:
Override
public void onPause(){
super.onPause();
mSensorManager.unregisterListener(this);
mCameraWrapper.mCamera.release();
}
IV. Then define PictureCallback to handle received data from Camera Lens and write them to File.
Code:
private PictureCallback mPicture = new PictureCallback() {
Override
public void onPictureTaken(byte[] data, Camera camera) {
//Here you will have array of data from camera and save these data to file as picture
// there is almost always separate function to handle name files and extension
// and here just write byte[] data to that file
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
// NOTE: Handle the image rotation after saving the file.
try {
// [B]pictureFile[/B] that created in [B]getOutputMediaFile()[/B] function now set to FileOutputStream
FileOutputStream fos = new FileOutputStream(pictureFile);
//here bye[] data write in created file.
fos.write(data);
fos.close();
Toast.makeText(getApplicationContext(), "Captured", Toast.LENGTH_SHORT).show();
}
catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
}
catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
finally {
//start camera preview after file created and data wrote in it.
mCameraWrapper.mCamera.startPreview();
}
}
};
Code:
public static final int MEDIA_TYPE_IMAGE = 1;
private File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AddOn_GeeksEmpire");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("CameraW", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
//this is empty jpg image file with timestamp to prevent overwrite
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "AddON_"+ timeStamp + ".jpg");
}
else {
return null;
}
return mediaFile;
}
>> After this we just need to call takePicture() function to take a pic...
V. When you implements the activity from SensorEventListener it will ask you to add unimplemented methods, that will be these two function at least.
Code:
Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
Override
public void onSensorChanged(SensorEvent event) {
//Proximity Sensor detect near interaction ~5cm
//in this case of sensor type system have one
float p = event.values[0];
System.out.println("Proximity >> " + p);
if(p == 0){
//here i call takePicture function when value of Sensor (destination) be 0
// like tap on sensor or hover nearly
mCameraWrapper.mCamera.takePicture(null, null, mPicture);
}
}
There should be also a Class that handle the surfaceView for preview of Camera Because It is easy to understand and not any special point in this simple example I just wrote detail inside the CameraWraper.class
Just few code...
- You define Camera parameter & then use Open(int id) function to launch camera & after this use startPreview() function to see live view through camera lens.
So First you need to add Preview (SurfaceView) & then use Camera.
In this sample...
Code:
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surface);
CameraWrapper mCameraWrapper = new CameraWrapper(); //class that handle the preview
surfaceView.getHolder().addCallback(mCameraWrapper);
Code:
Camera mCamera;
try{
mCamera.open(); /*Or you can use open(int id) the id can define the front & back camera. usually if you don't set id it will launch back camera (int id = 0)*/
} catch (RuntimeException e) {
// here is good point to handle errors if there is problem with Camera maybe from other app or System.
} finally{
mCamera.startPreview();
}
Note: You can use these inside camera preview class or directly in activity class.
Note: When there is change in surfaceView, change the orientation for instance, first you should stop the live view, apply new configuration and then startPreview().
Code:
Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
//
}
}
End of Proximity Capture |
OOOOOOO
The explanations of codes are available inside each classes.
If you find any mistake Or issue in my codes Please inform me.
Click to expand...
Click to collapse
Don't forget to Hit Thanks​

Sorry, I just forgot to write about Camera Preview Handler, So updated the Tutorial Post.
Good Luck

Related

[TOOL][APP][WIP] Native AT Command Injector

I trying to develop an application that can send AT commands directly to modem, from a native application interface (or possibly via terminal).
There are many obstacles here, but the main one is that the modem AT interface is rarely available through a simple serial device, which is why all other AT command apps always fail for the Samsung Galaxy class devices, which use a modified IPC protocol over sockets. However, by using the built-in RIL_OEM_HOOK_RAW and RIL_OEM_HOOK_STR we should be able to do this.
I then found this document for the Ublox LISA-U2 UMTS/HSPA voice and data modules, which show an example Android App, including most of the code used in that App. However, I cannot get this to compile in Eclipse. I think the reason is that there are many cosmetic changes in the AOS, while there are many typos in the code in that document.
I would very much like to get this or something very similar to work. I'm mainly developing on the Samsung Galaxy S2 (GB).
Here is a picture (from that document).
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Any help with this would be very very much appreciated!
It will also benefit the rest of the community as it will provide a foundation
for my future development of many other things to come, for free!
BTW. The BP used in that device is the very popular Intel/Infineon XGOLD-626...
When trying to run the code shown below in Eclipse (using GB 2.3.3+),
it fails with the following errors:The import com.android.internal.telephony cannot be resolved
The import android.os.AsyncResult cannot be resolved​It seem like it doesn't recognize these imports:
Code:
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import android.os.AsyncResult;
The java code is:
Code:
[SIZE=2]
/*=========================================================
Demo App Code by Ublox, modified copy and paste from:
http://www.u-blox.com/images/downloads/Product_Docs/AndroidRIL_Source_Code_ApplicationNote_%283G.G2-CS-11003%29.pdf
=========================================================== */
package com.testapp.sat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import android.view.View.OnKeyListener;
import android.view.KeyEvent;
import android.os.Message;
import android.os.Handler;
import android.os.AsyncResult;
import android.util.Log;
import android.app.AlertDialog;
public class RilOemHookTest extends Activity
{
private static final String LOG_TAG = "RILOemHookTestApp";
private RadioButton mRadioButtonAPI1 = null;
private RadioGroup mRadioGroupAPI = null;
private Phone mPhone = null;
private EditText CmdRespText = null;
private static final int EVENT_RIL_OEM_HOOK_CMDRAW_COMPLETE = 1300;
private static final int EVENT_RIL_OEM_HOOK_CMDSTR_COMPLETE = 1400;
private static final int EVENT_UNSOL_RIL_OEM_HOOK_RAW = 500;
private static final int EVENT_UNSOL_RIL_OEM_HOOK_STR = 600;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.riloemhook_layout);
mRadioButtonAPI1 = (RadioButton) findViewById(R.id.radio_api1);
mRadioGroupAPI = (RadioGroup) findViewById(R.id.radio_group_api);
// Initially turn on first button.
mRadioButtonAPI1.toggle();
//Get our main phone object.
// mPhone = PhoneFactory.getDefaultPhone();
//Register for OEM raw notification.
// mPhone.mCM.setOnUnsolOemHookRaw(mHandler, EVENT_UNSOL_RIL_OEM_HOOK_RAW, null);
//Capture text edit key press
CmdRespText = (EditText) findViewById(R.id.edit_cmdstr);
CmdRespText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
//If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
//Perform action on key press
Toast.makeText(RilOemHookTest.this,
CmdRespText.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
@Override
public void onPause()
{
super.onPause();
log("onPause()");
//Unregister for OEM raw notification.
// mPhone.mCM.unSetOnUnsolOemHookRaw(mHandler);
}
@Override
public void onResume()
{
super.onResume();
log("onResume()");
//Register for OEM raw notification.
// mPhone.mCM.setOnUnsolOemHookRaw(mHandler, EVENT_UNSOL_RIL_OEM_HOOK_RAW, null);
}
public void onRun(View view)
{
//Get the checked button
int idButtonChecked = mRadioGroupAPI.getCheckedRadioButtonId();
//Get the response field
CmdRespText = (EditText) findViewById(R.id.edit_response);
byte[] oemhook = null;
switch(idButtonChecked)
{
case R.id.radio_api1:
oemhook = new byte[1];
oemhook[0] = (byte)0xAA;
break;
case R.id.radio_api2:
oemhook = new byte[2];
oemhook[0] = (byte)0xBB;
oemhook[1] = (byte)0x55;
break;
case R.id.radio_api3:
//Send OEM notification (just echo the data bytes)
oemhook = new byte[7];
oemhook[0] = (byte)0xCC;
oemhook[1] = (byte)0x12;
oemhook[2] = (byte)0x34;
oemhook[3] = (byte)0x56;
oemhook[4] = (byte)0x78;
oemhook[5] = (byte)0x9A;
oemhook[6] = (byte)0xBC;
break;
case R.id.radio_api4:
//Send OEM command string
break;
default:
log("unknown button selected");
break;
}
if (idButtonChecked!=R.id.radio_api4) {
Message msg =
mHandler.obtainMessage(EVENT_RIL_OEM_HOOK_CMDRAW_COMPLETE);
mPhone.invokeOemRilRequestRaw(oemhook, msg);
CmdRespText.setText("");
} else {
//Copy string from EditText and add carriage return
String[] oemhookstring = { ((EditText)
findViewById(R.id.edit_cmdstr)).getText().toString()+'\r' } ;
//Create message
Message msg =
mHandler.obtainMessage(EVENT_RIL_OEM_HOOK_CMDSTR_COMPLETE);
//Send request
mPhone.invokeOemRilRequestStrings(oemhookstring, msg);
CmdRespText = (EditText) findViewById(R.id.edit_response);
CmdRespText.setText("---Wait response---");
}
}
private void logRilOemHookResponse(AsyncResult ar) {
log("received oem hook response");
String str = new String("");
if (ar.exception != null) {
log("Exception:" + ar.exception);
str += "Exception:" + ar.exception + "\n\n";
}
if (ar.result != null)
{
byte[] oemResponse = (byte[])ar.result;
int size = oemResponse.length;
log("oemResponse length=[" + Integer.toString(size) + "]");
str += "oemResponse length=[" + Integer.toString(size) + "]" + "\n";
if (size > 0) {
for (int i=0; i<size; i++) {
byte myByte = oemResponse[i];
int myInt = (int)(myByte & 0xFF);
log("oemResponse[" + Integer.toString(i) + "]=[0x" +
Integer.toString(myInt,16) + "]");
str += "oemResponse[" + Integer.toString(i) + "]=[0x" +
Integer.toString(myInt,16) + "]" + "\n";
}
}
} else {
log("received NULL oem hook response");
str += "received NULL oem hook response";
}
// Display message box
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(str);
builder.setPositiveButton("OK", null);
AlertDialog alert = builder.create();
alert.show();
}
private void logRilOemHookResponseString(AsyncResult ar) {
log("received oem hook string response");
String str = new String("");
CmdRespText = (EditText) findViewById(R.id.edit_response);
if (ar.exception != null) {
log("Exception:" + ar.exception);
str += "Exception:" + ar.exception + "\n\n";
}
if (ar.result != null) {
String[] oemStrResponse = (String[])ar.result;
int sizeStr = oemStrResponse.length;
log("oemResponseString[0] [" + oemStrResponse[0] + "]");
CmdRespText.setText( "" + oemStrResponse[0] );
} else {
log("received NULL oem hook response");
CmdRespText.setText( "No response or error received" );
}
}
private void log(String msg) {
Log.d(LOG_TAG, "[RIL_HOOK_OEM_TESTAPP] " + msg);
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
AsyncResult ar;
switch (msg.what) {
case EVENT_RIL_OEM_HOOK_CMDRAW_COMPLETE:
log("EVENT_RIL_OEM_HOOK_CMDRAW_COMPLETE");
ar = (AsyncResult) msg.obj;
logRilOemHookResponse(ar);
break;
case EVENT_RIL_OEM_HOOK_CMDSTR_COMPLETE:
log("EVENT_RIL_OEM_HOOK_CMDSTR_COMPLETE");
ar = (AsyncResult) msg.obj;
logRilOemHookResponseString(ar);
break;
case EVENT_UNSOL_RIL_OEM_HOOK_RAW:
break;
case EVENT_UNSOL_RIL_OEM_HOOK_STR:
break;
}
}
};
}
[/SIZE]
------------------ EDIT ---------------------
Of course they aren't recognized! They are internal packages not available outside the standard AOS API's. But I'm very stubborn and have now spent 2 days trying to use those damn packages anyway! I managed! I will post an update here and a whole new thread on how that is done, once I can get some other bugs out of the way...
new thread posted?
Here is my new thread on how to use and import internal packages into your project,
including instructions for hacking Eclipse ADT plugin, in order to allow for using
the com.android.internal classes.
"[APP][DEV][GUIDE] Using the Android Java Internal/Hidden API classes"
It is work in progress (WIP), and include a lot of manual file manipulations.
As such it is closely related to this thread. So keep an eye open for updates.
EDIT: 2014-01-06
That thread still work for API-17 JAR's!!
So, I managed to get it to compile but not without two main issues.
0) I got it to run on an SGS2 running GB2.3.4, but it FC's after being run.
1) I kept on getting AlertDialog builder complaints like:
"the constructor AlertDialog.Builder is undefined" so I had to comment out all code related to that. I searched for fixes, but since I'm not a Java programmer, I could not resolve this in the proper way...
2) Then I ran into some other undocumented errors from my own built (according to instructions) android.jar, so I just used the inazaruk's pre-made one, and it compiled. Here are his files:
https://github.com/inazaruk/android-sdk/tree/master/platforms
The main point is that we should not expect to blindly use the Ublox code and think it will work. Instead we need to understand the following:
a) How to properly use the RIL constants
Code:
[SIZE=2]RILConstants.java:
int RIL_REQUEST_OEM_HOOK_RAW = 59;
int RIL_REQUEST_OEM_HOOK_STRINGS = 60;
int RIL_UNSOL_OEM_HOOK_RAW = 1028;
my code:
EVENT_RIL_OEM_HOOK_CMDRAW_COMPLETE
EVENT_RIL_OEM_HOOK_CMDSTR_COMPLETE
EVENT_UNSOL_RIL_OEM_HOOK_RAW
EVENT_UNSOL_RIL_OEM_HOOK_STR
[/SIZE]
b) What the various bytecodes sent, are actually doing, and how to make them do what we want.
Code:
[SIZE=2] case R.id.radio_api1:
oemhook = new byte[1];
oemhook[0] = (byte)0xAA;
break;
case R.id.radio_api2:
oemhook = new byte[2];
oemhook[0] = (byte)0xBB;
oemhook[1] = (byte)0x55;
break;
case R.id.radio_api3:
//Send OEM notification (just echo the data bytes)
oemhook = new byte[7];
oemhook[0] = (byte)0xCC;
oemhook[1] = (byte)0x12;
oemhook[2] = (byte)0x34;
oemhook[3] = (byte)0x56;
oemhook[4] = (byte)0x78;
oemhook[5] = (byte)0x9A;
oemhook[6] = (byte)0xBC;
break;
[/SIZE]
c) If we can simplify the code to just send ONE hard-coded AT command and read the response.
d) alternatively use a completely different method, that will undoubtedly work, but will be device dependent. I'm talking about the (Samsung modified) IPC modem-communication protocols, as discussed in my older threads...
e) Find out how to use a local shell + device to send AT commands directly to BP.
To monitor the radio and related messages from the App (on a Sumsung) , you can use:
Code:
[SIZE=2]adb shell logcat AndroidRuntime:* ActivityManager:* dalvikvm:* DataRouter:E NetdConnector:D *:s
[/SIZE]
I've done similar work in my SprintDiagnostics app included in our Eos project. Its in the sprint gnex thread. There's no way to make this a user app as you have to be on the Phone looper thread. Meaning you have to declare the phone process in the manifest for the activity calling the raw ril requests. I can grab msl, write prl, and some other good stuff. And enable diagnostic mode based on shell work from Autoprime.
Edit: http://git.teameos.org/eos/device/samsung/toroplus.git/tree/SprintDiagnostics?h=jellybean
bigrushdog said:
I've done similar work in my SprintDiagnostics app included in our Eos project. Its in the sprint gnex thread. There's no way to make this a user app as you have to be on the Phone looper thread. Meaning you have to declare the phone process in the manifest for the activity calling the raw ril requests. I can grab msl, write prl, and some other good stuff. And enable diagnostic mode based on shell work from Autoprime.
Edit: http://git.teameos.org/eos/device/samsung/toroplus.git/tree/SprintDiagnostics?h=jellybean
Click to expand...
Click to collapse
Hi! Thanks for interesting info, but unfortunately I don't understand all you're saying. But I got many more questions that answers.
First of all, what thread are you referring to? (Could you give a link?) Second, how do you do that declaration? (Code example?) BTW. I looked briefly at the code and must admit it seem a bit cryptic, as I didn't quite find the where AT's goes or are created. Third, what is "msl"? Do you have an APK to try on? Finally, what specific modem (cellular) processor are you running in that device? LTE?
Screenshots?
Thanks in advance.
Hi There
Forgive me if I'm oversimplifying it here but wouldn't minicom or some similar terminal program do this? I believe minicom is included in busybox.
Surely you could just wrap that around using JNI
AT Commands in android are sent natively by the vendor implemented ril library, normally set in global properties as rild.libpath. It may be worth stepping away from the JAVA code for a moment and looking at the rild and reference-ril sources in the aosp source. The atchannel.c is where the "magic" happens. Also If you haven't already, read PDK Telephony Documentation
When you get down to it, all you really want to do is read and write from a tty character device so doing it "raw" is always an option.
Also to monitor the Radio log in android use adb logcat -b radio , You will be enlightened as you watch the AT commands fly by!
trevd said:
Forgive me if I'm oversimplifying it here but ...
Also to monitor the Radio log in android use adb logcat -b radio , You will be enlightened as you watch the AT commands fly by!
Click to expand...
Click to collapse
No, you are missing the entire point of this thread. We are trying to circumvent actually using the vendor RIL, by tunneling the commands via the OEM_RAW requests. That is because it is often filtering out non-standard OEM AT commands that are not part of the common standard. These are all dependent on the BP/CP and and varies widely from device to device. In addition on many devices the direct modem serial is either not possible (because there is no such connection/transport) or it has been blocked/disabled. The Java way of circumventing this, is what this thread is all about. In addition the "-b radio" logcat doesn't fetch ATs sent by vendor specific OEM IPCs, for example. This is the case for the SGS2 (GT-I9100) and many other devices.
E:V:A said:
No, you are missing the entire point of this thread.
Click to expand...
Click to collapse
Ah, Okay...If I had a SGS2 too play with I'd probably join in on this little adventure, but I don't
trevd said:
Ah, Okay...If I had a SGS2 too play with I'd probably join in on this little adventure, but I don't
Click to expand...
Click to collapse
Well again... The point is that: RIL_OEM_HOOK_RAW and RIL_OEM_HOOK_STR are (internally) accessible for all RIL's. Which means that you CAN join in!
E:V:A said:
Hi! Thanks for interesting info, but unfortunately I don't understand all you're saying. But I got many more questions that answers.
First of all, what thread are you referring to? (Could you give a link?) Second, how do you do that declaration? (Code example?) BTW. I looked briefly at the code and must admit it seem a bit cryptic, as I didn't quite find the where AT's goes or are created. Third, what is "msl"? Do you have an APK to try on? Finally, what specific modem (cellular) processor are you running in that device? LTE?
Screenshots?
Thanks in advance.
Click to expand...
Click to collapse
My apologies for not responding sooner. I forgot to subscribe ;( . I'd like to try and clear up my previous post as well as try to shed some light on this topic. First, I'm by no means a telephony expert. I've read some of your other threads and found them remarkable. I write Java and do feature development in a AOSP project. I currently am doing telephony work on Samsun Sprint CDMA devices including galaxy nexus, nexus s, and working on s3. As you know, those use the VIA chipset. However, working through the Android radio layer makes that irrelevant, for the most part. I've also worked with Autoprime on some telephony/modem/ril matters.
First some code and background on what I have done on Sprint Galaxy Nexus. These are some code fragments from my CDMATools app.
Manifest:
Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.teameos.settings.device"
android:sharedUserId="android.uid.system"
android:versionCode="1"
android:versionName="1.0" >
Of importance if the android.uid.system uid. This allows the app to access the phone process, which is required for PhoneFactory.getDefaultPhone(); The following activity queries ril for a Master Subsidy Lock code (msl)
Code:
<activity
android:name=".MslActivity"
android:label="@string/msl_activity"
android:process="com.android.phone" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And the actual method which is used to query the radio...
Code:
private void checkMSLCode()
{
ByteArrayOutputStream bytearrayoutputstream;
DataOutputStream dataoutputstream;
bytearrayoutputstream = new ByteArrayOutputStream();
dataoutputstream = new DataOutputStream(bytearrayoutputstream);
try {
dataoutputstream.writeByte(main_cmd_hidden);
dataoutputstream.writeByte(OEM_SUB_CMD_GET_MSL);
dataoutputstream.writeShort(device_short);
if (isTuna) {
dataoutputstream.writeByte(OEM_SUB_RAW_MSL);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPhone.invokeOemRilRequestRaw(bytearrayoutputstream.toByteArray(),
mHandler.obtainMessage(GET_MSL_DONE));
}
A snipit of the handler code to receive and process incoming messages from radio.
Code:
mHandler = new Handler() {
public void handleMessage(Message message) {
Log.i(LOG_TAG, "MSL response incoming!!");
AsyncResult asyncresult = (AsyncResult) message.obj;
if (asyncresult.exception != null)
{
Log.i(LOG_TAG, "AsyncResult Exception Occur!!!");
} else
if (asyncresult.result == null)
{
Log.i(LOG_TAG, "ar.result == NULL! - No answer for MSL response");
} else
{
byte abyte0[] = (byte[]) (byte[]) asyncresult.result;
String s = new String("");
for (int j = 0; j < 6; j++)
s = (new StringBuilder()).append(s).append((char) abyte0[j]).toString();
Intent intent = new Intent().setAction(getString(R.string.msl_action));
intent.putExtra(getString(R.string.msl_key), Integer.parseInt(s));
mContext.sendBroadcast(intent);
finish();
}
}
};
I have been able to get the raw commands from RE'ing various device proprietary apk's. Of importance to this topic is this checkMSLCode() function. This is the equivalent of raw AT commands, but being passed though the Android radio layer. From looking at the code for that AT command application you posted in the OP, it uses a similar structure. In fact, if time permits, i'm confident i can expand on it and make it somewhat more robust. One problem is see is finding an effective way to interpret the AT command responses from the radio. Requests often get a response in different forms, thus requiring a unique handler for each function. Of course, we could always do a Object.toString() to the log and try to decipher it. I hope this info sheds some light on the topic. I'll post when I come up with more.
Edit: also I should note, i build this apk inside of my AOSP repo, so it sees all the internal api automatically. I'm now looking at using reflection to do some telephony stuff, but it's proving rather tricky.
Very nice! (That was an understatement!)
I just wish I knew how to apply that...
Regarding the raw command (and their appropriate responses), I think there is both code and documents available for this, for Qualcomm chips. I know nothing about the VIA chipsets, but a whole lot more about the Qualcomm MSM stuff... Many CDMA devices use just Qualcomm, so perhaps you've been across some of these. I bet we could get this working on them, somehow.
Since, just as you said, the RIL should take care of much of the details. We just need to apply the command requests and responses. But my Java App making skills are really not even worthy a try at the moment.
Somebody else who'd care to join us, to give this a try?
As an aside and a note of reference, mostly to myself. (For a professional OEM programmer, this may be obvious, but me it's not. ) After having dug through some of the QC MSM code, I've come to the conclusion that QC like to use what they (?) call SMD (Shared memory Device) for doing their IPC stuff, which include talking to modem. While for Infineon/Intel modem, we know that Samsung have developed their own (non-standard) IPC protocol version for this communication...
While rewriting my Cdmatools app making it fragment based, I used reflection to access all the hidden apis. I'll fork the source for the at command app and apply the reflected classes.
I also got access to my Samsung Galaxy S3 via Reflection... although I had to go AOSP and sign with they same system key to run as looper. But now I just need help with what to send........ via RAW/String
bigrushdog said:
While rewriting my Cdmatools app making it fragment based, I used reflection to access all the hidden apis. I'll fork the source for the at command app and apply the reflected classes.
Click to expand...
Click to collapse
Excellent. Where can we download this?
Have you been able to make demo App or something?
enigma99a said:
I also got access to my Samsung Galaxy S3 via Reflection... although I had to go AOSP and sign with they same system key to run as looper. But now I just need help with what to send........ via RAW/String
Click to expand...
Click to collapse
Can you elaborate? Do you need info on what AT commands to send, or are you asking about the protocol?
PS. I would be great if you could say something more about how you did it, so that perhaps other interested people can join in. We need more people on this, so that we can start working on THIS project.
my apologies that i've been unable to continue efforts on that project. My aosp project consumes all of my development time and then some. Here is the source for CDMATools. I was looking for ways to monetize it. However, due to android's permissions scheme, it's not gonna happen lol. so here ya go.
www.miuimods.com/bigrushdog/CDMATools.tar.bz2
compile in eclipse then sign with aosp platorm test keys. I use some libraries that require it be compiled in eclipse. and because of the internal telephony imports, it must have the platform signature. Also, because it's compiled in eclipse, i use reflection to access internal telephony. Of interest will be the Phone service class. everything else is just ui. Look at how i structure the reflection. You can reflect all the internal telephony calls in the same fashion. any issues, just hit me. but my time now is very limited as I have a large team to manage and my plate is more than full. Good luck!
feel free to post on github, distribute, or whatever.
So I have not yet had time to modify OP app or using bigrushdog's CDMAtools. But I will soon. In the meantime I just post the internals for the _OEM_HOOK_ statements.
The code base is here:
https://android.googlesource.com/platform/frameworks/base.git/
So from the JellyBean RIL.java:
Code:
[SIZE=2]public void invokeOemRilRequestRaw(byte[] data, Message response) {
RILRequest rr = RILRequest.obtain(RIL_REQUEST_OEM_HOOK_RAW, response);
if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
+ "[" + IccUtils.bytesToHexString(data)
+ "]");
rr.mp.writeByteArray(data);
send(rr);
}
public void invokeOemRilRequestStrings(String[] strings, Message response) {
RILRequest rr = RILRequest.obtain(RIL_REQUEST_OEM_HOOK_STRINGS, response);
if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
rr.mp.writeStringArray(strings);
send(rr);
}[/SIZE]
And the expected response can be found in the ril.h:
Code:
[SIZE=2]#define [B]RIL_REQUEST_DATA_REGISTRATION_STATE[/B] 21
/**
* RIL_REQUEST_DATA_REGISTRATION_STATE
*
* Request current DATA registration state
*
* "data" is NULL
* "response" is a "char **"
* ((const char **)response)[0] is registration state 0-5 from TS 27.007 10.1.20 AT+CGREG
* ((const char **)response)[1] is LAC if registered or NULL if not
* ((const char **)response)[2] is CID if registered or NULL if not
* ((const char **)response)[3] indicates the available data radio technology,
* valid values as defined by RIL_RadioTechnology.
* ((const char **)response)[4] if registration state is 3 (Registration
* denied) this is an enumerated reason why
* registration was denied. See 3GPP TS 24.008,
* Annex G.6 "Additonal cause codes for GMM".
* 7 == GPRS services not allowed
* 8 == GPRS services and non-GPRS services not allowed
* 9 == MS identity cannot be derived by the network
* 10 == Implicitly detached
* 14 == GPRS services not allowed in this PLMN
* 16 == MSC temporarily not reachable
* 40 == No PDP context activated
* ((const char **)response)[5] The maximum number of simultaneous Data Calls that can be
* established using RIL_REQUEST_SETUP_DATA_CALL.
*
* [/SIZE]The values at offsets 6..10 are [COLOR=Blue][B]optional LTE[/B][/COLOR] location information in decimal.[SIZE=2]
* If a value is unknown that value may be NULL. If all values are NULL,
* none need to be present.
* ((const char **)response)[6] is TAC, a 16-bit Tracking Area Code.
* ((const char **)response)[7] is CID, a 0-503 Physical Cell Identifier.
* ((const char **)response)[8] is ECI, a 28-bit E-UTRAN Cell Identifier.
* ((const char **)response)[9] is CSGID, a 27-bit Closed Subscriber Group Identity.
* [COLOR=Red]((const char **)response)[10] is TADV, a 6-bit timing advance value.[/COLOR]
*
* LAC and CID are in hexadecimal format.
* valid LAC are 0x0000 - 0xffff
* valid CID are 0x00000000 - 0x0fffffff
*
* Please note that registration state 4 ("unknown") is treated
* as "out of service" in the Android telephony system
*
* Valid errors:
* SUCCESS
* RADIO_NOT_AVAILABLE
* GENERIC_FAILURE
*/
#define [B]RIL_REQUEST_OEM_HOOK_RAW[/B] 59
/**
* RIL_REQUEST_OEM_HOOK_RAW
*
* This request reserved for OEM-specific uses. It passes raw byte arrays
* back and forth.
*
* It can be invoked on the Java side from
* com.android.internal.telephony.Phone.invokeOemRilRequestRaw()
*
* "data" is a char * of bytes copied from the byte[] data argument in java
* "response" is a char * of bytes that will returned via the
* caller's "response" Message here:
* (byte[])(((AsyncResult)response.obj).result)
*
* An error response here will result in
* (((AsyncResult)response.obj).result) == null and
* (((AsyncResult)response.obj).exception) being an instance of
* com.android.internal.telephony.gsm.CommandException
*
* Valid errors:
* All
*/
#define [B]RIL_REQUEST_OEM_HOOK_STRINGS[/B] 60
/**
* RIL_REQUEST_OEM_HOOK_STRINGS
*
* This request reserved for OEM-specific uses. It passes strings
* back and forth.
*
* It can be invoked on the Java side from
* com.android.internal.telephony.Phone.invokeOemRilRequestStrings()
*
* "data" is a const char **, representing an array of null-terminated UTF-8
* strings copied from the "String[] strings" argument to
* invokeOemRilRequestStrings()
*
* "response" is a const char **, representing an array of null-terminated UTF-8
* stings that will be returned via the caller's response message here:
*
* (String[])(((AsyncResult)response.obj).result)
*
* An error response here will result in
* (((AsyncResult)response.obj).result) == null and
* (((AsyncResult)response.obj).exception) being an instance of
* com.android.internal.telephony.gsm.CommandException
*
* Valid errors:
* All
*/
[SIZE=2]#define [COLOR=Purple][B]RIL_REQUEST_RADIO_POWER[/B][/COLOR] 23
/**
* RIL_REQUEST_RADIO_POWER
*
* Toggle radio on and off (for "airplane" mode)
* If the radio is is turned off/on the radio modem subsystem
* is expected return to an initialized state. For instance,
* any voice and data calls will be terminated and all associated
* lists emptied.
*
* "data" is int *
* ((int *)data)[0] is > 0 for "Radio On"
* ((int *)data)[0] is == 0 for "Radio Off"
*
* "response" is NULL
*
* Turn radio on if "on" > 0
* Turn radio off if "on" == 0
*
* Valid errors:
* SUCCESS
* RADIO_NOT_AVAILABLE
* GENERIC_FAILURE
*/
[/SIZE]
[/SIZE]
The last one seem to imply that there could be other "bytes" to be read from that data[] array... Some suggestions have been the Transmitted power... But this was assuming a Qualcomm based modem, so there is no telling if there is another standard there. We don't know...
Here are links to two highly relevant threads on Gmane/GoogleGroups with code excerpts similar to what we need...
http://article.gmane.org/gmane.comp.handhelds.android.ndk/10555
http://article.gmane.org/gmane.comp.handhelds.android.platform/8436
https://groups.google.com/forum/?fromgroups=#!topic/android-platform/tVyNMnXtcEI
With a link to Google Phone G1 Field Test:
http://phoneftd.blogspot.com/2009/03/google-g1-phone-field-test.html

[GUIDE-DEV][HTC Sense SDK, Sense 4,5,6] How to build apps for HTC Sense

Complete GUIDE to develop an application using HTC OpenSense SDK​
Requirements:
Eclipse
Basic knowledge of Android App Development. I will not explain basic things (Like making activity, listview and etc)
HTC OpenSense SDK Installed - See post 4 How to add Reference Library
HTC Phone with HTC Sense. Not For AOSP ROMs
Create Project with OpenSense SDK
Create new project. Name it as you want. These are requirements. Other you can state as you want
Minimum Required SDK - 15
Target SDK - >= 19 (or older is 16)
Compile with - HTC OpenSense API 19 (or older is 16)
Theme - Holo Light with dark action bar
Create activity - Blank
Navigation type - None
Check if SDK is choosen correctly
In your project in Android Dependencies should be HTCExtension.jar file
Above Android Dependencies should be stated which SDK api you are using. HTC OpenSense APIs [Android 4.4.2] (or older is 4.1.2)
You can start building your application with HTC OpenSense SDK.
Guide content:
Add HTC Carousel with Tabs
[*]Add 3 Dot menu to actionbar
[*]HTC AlertDialog example
[*]HTC ListView and HtcListActivity example
[*]Add HTC Sense Skin support (Only Sense 3.6 to Sense 4.1)
[*]Using HTCPreference in your App
[*]Add Sense 6 theme support to your application
[*]Add HTC's Swipe2Update to ListView
[*]Add SlidingMenu
[*]Expandable List View. Thx to DHD22800
[*]Quick tips. Thx to DHD22800
​
If I helped you to create your first Application using HTC OpenSense SDK simply rate thread and hit thanks button, give credits and link to this Thread. If you are brave enought to admitt that this thread is helped you.
In any case Im doing it to help you to learn more​
HTC Carousel Activity/Fragment (Swipeable tabs) - Sense 3.6 up to Sense 6 Samples
HTC Carousel with Tabs for Sense 3.6 up to Sense 4 (Using Activities)
Create classes and Carousel (HTC Sense Tabs)
Create simple activities
Create two classes Tab1.java; Tab2.java
Create two layout xml files - tab_1.xml; tab_2.xml;
Place any two different png icons for Tab1 and Tab2 reference
tab_1.xml
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Tab1" />
</RelativeLayout>
tab_2.xml
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Tab2" />
</RelativeLayout>
Tab1.java
Code:
package com.yourpackage.name;
import android.app.Activity;
import android.os.Bundle;
public class Tab1 extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_1);
}
}
Tab2.java
Code:
package com.yourpackage.name;
import android.app.Activity;
import android.os.Bundle;
public class Tab2 extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_2);
}
}
Create carousel (HTC Sense Tabs) and put tabs together
Create class: TabProvider.java; Remove everything inside class and place this code:
com.yourpackage.name - it is your name of package.
TabProvider.java
Code:
package com.yourpackage.name;
import com.htc.content.CarouselProvider;
[user=1299008]@supp[/user]ressWarnings("deprecation")
public class TabProvider extends CarouselProvider {
final static String AUTHORITY =
"com.yourpackage.name.TabProvider";
public TabProvider() {
super();
setupCarousel(AUTHORITY);
}
}
Open MainActivity, remove everything from class and paste this code:
MainActivity.java
Code:
package com.yourpackage.name;
import android.content.Intent;
import android.os.Bundle;
import com.htc.widget.CarouselActivity;
import com.htc.widget.CarouselHost;
public class MainActivity extends CarouselActivity {
final static String AUTHORITY =
"com.yourpackage.name.TabProvider";
public MainActivity() {
super(AUTHORITY);
}
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setGId(1);
super.onCreate(savedInstanceState);
final CarouselHost mPanelHost = getCarouselHost();
mPanelHost.addTab("Tab1", this, R.string.tab_1,
R.drawable.ic_tab1,
R.drawable.ic_tab1,
R.drawable.ic_tab1,
(new Intent("com.yourpackage.name.Tab1")));
mPanelHost.addTab("Tab2", this, R.string.tab_2,
R.drawable.ic_tab2,
R.drawable.ic_tab2,
R.drawable.ic_tab2,
(new Intent("com.yourpackage.name.Tab2")));
}
}
Configuring manifest
Dont forget, all classes have to be in Manifest.
Code:
<activity
android:name=".Tab1"
android:screenOrientation="portrait"
android:configChanges="orientation"
android:label="Tab1" >
<intent-filter>
<action android:name="com.yourpackage.name.Tab1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Tab2"
android:screenOrientation="portrait"
android:configChanges="orientation"
android:label="Tab2" >
<intent-filter>
<action android:name="com.yourpackage.name.Tab2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Also for HTC SDK you have to state Provider: Create provider at the end of Manifest before </application> tag
Code:
<provider
android:name="com.yourpackage.name.TabProvider"
android:authorities="com.yourpackage.name.TabProvider" />
Create ActionBar in HTC Sense Style
For all tabs in your main activity you dont need to create actionbar for each of them, you need only one Actionbar for all Tabs.
That means all Activities which will be part of TabCarousel it will use the same action bar from MainActivity.
Make Changes in your mainactivity as follow:
MainActivity.java with ActionBar
Code:
package com.yourpackage.name;
import android.content.Intent;
import android.os.Bundle;
import com.htc.widget.CarouselActivity;
import com.htc.widget.CarouselHost;
[COLOR="Red"]import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;[/COLOR]
public class MainActivity extends CarouselActivity {
final static String AUTHORITY =
"com.yourpackage.name.TabProvider";
[COLOR="red"]public static ActionBarText mActionText;[/COLOR]
public MainActivity() {
super(AUTHORITY);
}
[COLOR="red"] private void SetupActionBar()
{
Object obj = new ActionBarExt(this, getActionBar());
((ActionBarExt)obj).setFullScreenEnabled(true);
((ActionBarExt)obj).enableHTCLandscape(false);
mActionText = new ActionBarText(this);
mActionText.setPrimaryText(R.string.app_name);
obj = ((ActionBarExt)obj).getCustomContainer();
((ActionBarContainer)obj).setRightDividerEnabled(true);
((ActionBarContainer)obj).addCenterView(mActionText);
}[/COLOR]
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setGId(1);
super.onCreate(savedInstanceState);
final CarouselHost mPanelHost = getCarouselHost();
[COLOR="red"]SetupActionBar();[/COLOR]
mPanelHost.addTab("Tab1", this, R.string.tab_1,
R.drawable.ic_tab1,
R.drawable.ic_tab1,
R.drawable.ic_tab1,
(new Intent("com.yourpackage.name.Tab1")));
mPanelHost.addTab("Tab2", this, R.string.tab_2,
R.drawable.ic_tab2,
R.drawable.ic_tab2,
R.drawable.ic_tab2,
(new Intent("com.yourpackage.name.Tab2")));
}
}
HTC Carousel with Tabs for Sense 4.1 up to Sense 5.5 (Using Fragments)
Create Carousel Fragment, Tabs, MainActivity
Create Tab Fragments
Now instead of Activities we will use Fragments, and it is difficult for some users. And I will try to explain how to build Carousel and not how to build Fragment Activity. But as example you can refer to my Open Source project myStore (which now converted to Fragments)
Create two classes Tab1Fragment and Tab2Fragment
Tab1Fragment.java
Code:
package your.package.name;
[COLOR="Lime"]#2[/COLOR] import android.app.Fragment;
[COLOR="lime"]#1[/COLOR] public class Tab1Fragment [B][COLOR="red"]extends Fragment[/COLOR][/B] {
Button button;
[COLOR="lime"]#3[/COLOR] public Tab1Fragment () {
}
[COLOR="lime"]#4[/COLOR] [user=439709]@override[/user]
public [B][COLOR="Red"]View onCreateView[/COLOR][/B](LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1, container, false);
[COLOR="Lime"]#7[/COLOR] button = (Button) [B][COLOR="Red"]view.[/COLOR][/B]findViewById(R.id.button);
[COLOR="RoyalBlue"][I]//All your code that you need when application is first time created (see onCreate method example)[/I][/COLOR]
[COLOR="lime"]#5[/COLOR] [B][COLOR="red"]return view;[/COLOR][/B]
}
[COLOR="lime"]#6[/COLOR]
}
As you can see the Class structure is different from what you might get used.
Now you need to extend class as Fragment (#1) and import android.app.Fragment; (#2)
Then you need to have public method which represent the entire Class with the name of the Class (#3)
And Fragment uses onCreateView method instead of onCreate in Activity (#4)
And you also need return statement for the view (#5) which will be at the end after all your code inside onCreateView method
Outside onCreateView you will have all your methods for the purpose of application and those methods you will call from onCreateView method.
Example of (#7) is that how you need to use findViewById method. you need to add view. before the method. Other than tha is the same
Now create Tab2Fragment and use the same method but different layout resources.
Create Tab Provider
Code:
package your.package.name;
import com.htc.fragment.content.CarouselProvider;
public class TabProvider extends CarouselProvider {
public TabProvider(){
super();
setupCarousel(MainActivity.AUTHORITY);
}
}
Create CarouselFragment
Now to store tabs in your application we will use separate Carousel class where you will identify each tab, name, Class, icons , etc
For each tab create method for it for example
Code:
private void addTab1(CarouselHost host, String tag, int icon, int str) {
host.addTab(getActivity(), new CarouselTabSpec(tag,
str, icon, icon, icon, Tab1Fragment.class.getName()));
}
private void addAnotherTab(CarouselHost host, String tag, int icon, int str) {
host.addTab(getActivity(), new CarouselTabSpec(tag,
str, icon, icon, icon, AnotherTabFragment.class.getName()));
}
and in onActivityCreated method add your tab as in example
Code:
addAnotherTab(host, "AnotherTab", R.drawable.another_icon,
R.string.another_tab);
Carousel.java
Code:
package your.package.name;
import android.os.Bundle;
import com.htc.fragment.widget.CarouselFragment;
import com.htc.fragment.widget.CarouselHost;
import com.htc.fragment.widget.CarouselTabSpec;
public class Carousel extends CarouselFragment {
public Carousel() {
super(MainActivity.AUTHORITY);
requestCarouselFeature(CarouselFragment.FEATURE_CUSTOM_TITLE);
}
private void addTab1(CarouselHost host, String tag, int icon, int str) {
host.addTab(getActivity(), new CarouselTabSpec(tag,
str, icon, icon, icon, Tab1Fragment.class.getName()));
}
private void addTab2(CarouselHost host, String tag, int icon, int str) {
host.addTab(getActivity(), new CarouselTabSpec(tag,
str, icon, icon, icon, Tab2Fragment.class.getName()));
}
[user=439709]@override[/user]
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final CarouselHost host = getCarouselHost();
addTab1(host, "Tab1", R.drawable.ic_tab1,
R.string.tab1);
addTab2(host, "Tab2", R.drawable.ic_tab2,
R.string.tab2);
}
}
Create MainActivity
Now MainActivity will be simple Activity with reference to Carousel class.
Code:
package your.package.name;
public class MainActivity extends Activity {
final static String AUTHORITY = "your.package.name.MainActivity";
private Carousel mCarousel = null;
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
final int rootId = 1;
FrameLayout viewRoot = new FrameLayout(this);
viewRoot.setId(rootId);
setContentView(viewRoot);
mCarousel = new Carousel();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(rootId, mCarousel);
ft.commit();
registerForContextMenu(viewRoot);
}
}
Configuration of Manifest
For Fragments you dont need anymore to add permission for them in Manifest (Only for Activities)
So basically with One mainactivity and two Tabs your manifest should look like
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name"
android:versionCode="10"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
allowSkinChange="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="your.package.name.MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="your.package.name.TabProvider"
android:authorities="your.package.name.MainActivity" />
</application>
</manifest>
Make sure your provider is exaclty as follow.​
HTC Carousel with Tabs for Sense 6 (Using Fragments)
Create CarouselFragment, MainActivity, Tabs
CarouselFragment:
Code:
package com.your.pkg;
[COLOR="red"]import com.htc.fragment.widget.CarouselFragment; <!-- Make sure the imports from com.htc.fragment.*-->
import com.htc.fragment.widget.CarouselHost;
import com.htc.fragment.widget.CarouselTabSpec;[/COLOR]
importcom.your.pkg.MainActivity;
import com.your.pkg.R;
import android.os.Bundle;
public class Carousel extends [COLOR="Red"]CarouselFragment[/COLOR] {
public Carousel() {
super(MainActivity.AUTHORITY);
requestCarouselFeature(CarouselFragment.FEATURE_CUSTOM_TITLE);
}
private void addTab(CarouselHost host, String tag, int icon, int str, String tag5) {
host.addTab(getActivity(), new CarouselTabSpec(tag,
str, tag5));
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final CarouselHost host = getCarouselHost();
addTab(host, [COLOR="Blue"]"FirstTab"[/COLOR], R.drawable.ic_launcher,
R.string.[COLOR="Blue"]first[/COLOR], First.class.getName());
addTab(host, [COLOR="Blue"]"SecondTab"[/COLOR], R.drawable.ic_launcher,
R.string.[COLOR="Blue"]second[/COLOR], Second.class.getName());
[COLOR="Red"]<!-- Add as many addTab(); methods as you need Tabs. The addTab() is universal-->[/COLOR]
}
}
MainActivity
Code:
public class MainActivity extends MfMainActivity {
public final static String AUTHORITY = "mikrosmile.kontrol.MainActivity";
private Carousel mCarousel = null;
static Window window;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
final int rootId = 1;
FrameLayout viewRoot = new FrameLayout(this);
viewRoot.setId(rootId);
setContentView(viewRoot);
mCarousel = new Carousel();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(rootId, mCarousel);
ft.commit();
registerForContextMenu(viewRoot);
getWindow().setFormat(1);
}
}
Tabs
Code:
Any Activity or Fragment
HTC's Release to Refresh method (Swipe2Refresh)
The Release to Refresh is like this:
View attachment 2711413
It can be added to any View. I will show example how to add it to ListView
Code:
[COLOR="red"]import com.htc.widget.OnPullDownListener;[/COLOR]
public static ActionBarRefresh aRefresh;
@Override
protected void onCreate(Bundle bundle) {
//Adding inside onCreate method of you listActivity
list.[COLOR="Red"]setOnPullDownListener(new PullDown());[/COLOR]
}
public class PullDown implements OnPullDownListener{
//just a class inside your listActivity
@Override
public void onGapChanged(int top, int bottom) {
if(top != 0){
actionbarext.getCustomContainer().setRotationMax(top);
actionbarext.getCustomContainer().setRotationProgress(bottom);
//actionbarext.getCustomContainer is your ActionBar container
}
}
@Override
public void onPullDownCancel() {
actionbarext.getCustomContainer().setUpdatingState(0);
}
@Override
public void onPullDownRelease() {
actionbarext.getCustomContainer().setUpdatingState(0);
//do whatever you need to update the list here, you can call a method or AsyncTask from here
}
@Override
public void onPullDownToBoundary() {
actionbarext.getCustomContainer().setRotationProgress(actionbarext.getCustomContainer().getRotationMax());
}
}
Code:
On your method to update list add this to onPreExecute
aRefresh.setVisibility(View.VISIBLE);
aRefresh.setPrimaryText("Updating...");
actionbartext.setPrimaryVisibility(View.GONE);
actionbartext.setSecondaryVisibility(View.GONE);
When you finish your task to update list, inside onPostExecute add this
actionbarext.getCustomContainer().setUpdatingState(0);
Code:
Inside your method to SetupActionBar add this
aRefresh = new ActionBarRefresh(c);
aRefresh.setVisibility(View.GONE);
actionbarext.getCustomContainer().addCenterView(aRefresh);
​
Add HTC Sense 6 Theme support to your app
Inside your MainActivity add this method
Code:
public static int getHtcThemeID(Context context, int i)
{
return HtcWrapConfiguration.getHtcThemeId(context, i);
}
And in onCreate method add this
Code:
setTheme(getHtcThemeID(context, 0));
This method is returning current Choosen theme ID by Variable from 0 to 3.
So, when you open Personalization - Theme. You see 4 Themes. First 3 has different Color boxes at the top. The first 3 is the actual variable integer from 0 - 3.
So, if you want to get , let's say Red color from Theme 2. You have to Choose Theme 2 in the Theme Settings, and in the app use this:
Code:
setTheme(getHtcThemeID(context, 3));
Once you change Theme, it will also use The Orange color from Theme 1, and the Purple Color from Theme 3
​
Add SlidingMenu to your Sense application
To make the sliding menu in your application like Mail app or File manager app, follow this:
View attachment 2876504
Code:
public class MainActivity extends [COLOR="Red"]SlidingActivity [/COLOR]{
[COLOR="red"]private SlidingMenu mSlidingMenu;[/COLOR]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
window = getWindow();
setContentView(R.layout.activity_main);
[COLOR="red"]setBehindContentView(R.layout.activity_behind);
initSlidingMenu();[/COLOR]
SetupActionBar(this);
}
private void initSlidingMenu()
{
mSlidingMenu = getSlidingMenu();
[COLOR="red"]mSlidingMenu.setBehindWidth(500);[/COLOR] [COLOR="Lime"]//any width as you want[/COLOR]
mSlidingMenu.setFadeDegree(0.5F);
mSlidingMenu.setFadeEnabled(true);
mSlidingMenu.setMode(0);
mSlidingMenu.setTouchModeAbove(0);
mSlidingMenu.setOnOpenedListener(new com.htc.widget.SlidingMenu.OnOpenedListener() {
public void onOpened()
{
initSlidingMenuContent();
}
});
}
private void initSlidingMenuContent(){
[COLOR="red"] mSlidingMenu.setShadowWidth(50);
mSlidingMenu.setShadowDrawable(ICservices.getShadowDrawable());[/COLOR][COLOR="Lime"]//any shadow drawable you want
//here is all your code that represent the behind layout. it can be listview or any other View you need[/COLOR]
}
[COLOR="SeaGreen"]//if you want to have a toggle at the actionbar, also add OnClickListener to ActionbarTextView and add IconView to Actionbar[/COLOR]
}
add this permission to Manifest:
[COLOR="red"]<uses-permission android:name="com.htc.permission.APP_DEFAULT" />[/COLOR]
Add 3 Dot menu to actionbar
For the Menu in actionbar you just implement simple menu method, but you dont need to use menu.xml for it.
Also you dont need to create string values for Menu, it will generate automatically for you, the icon, and the name.
This is method to create Menu in ActionBar. Add it in MainActivity before SetupActionBar() method
Code:
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("ContextMenu");
}
[user=439709]@override[/user]
public boolean onCreateOptionsMenu (Menu menu) {
menu.add(1, 1, 1, R.string.settings);
return true;
}
public boolean onOptionsItemSelected(MenuItem menuitem)
{
boolean flag = true;
switch (menuitem.getItemId())
{
case 1:
startActivity(new Intent(this, Settings.class));
break;
}
return flag;
}
This is your new MainActivity with Menu
Code:
package com.yourpackage.name;
import android.content.Intent;
import android.os.Bundle;
import com.htc.widget.CarouselActivity;
import com.htc.widget.CarouselHost;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;
[COLOR="Red"]import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;[/COLOR]
public class MainActivity extends CarouselActivity {
final static String AUTHORITY =
"com.yourpackage.name.TabProvider";
public static ActionBarText mActionText;
public MainActivity() {
super(AUTHORITY);
}
[COLOR="red"]public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("ContextMenu");
}
[user=439709]@override[/user]
public boolean onCreateOptionsMenu (Menu menu) {
menu.add(1, 1, 1, R.string.settings);
return true;
}
public boolean onOptionsItemSelected(MenuItem menuitem)
{
boolean flag = true;
switch (menuitem.getItemId())
{
case 1:
startActivity(new Intent(this, Settings.class));
break;
}
return flag;
}[/COLOR]
private void SetupActionBar()
{
Object obj = new ActionBarExt(this, getActionBar());
((ActionBarExt)obj).setFullScreenEnabled(true);
((ActionBarExt)obj).enableHTCLandscape(false);
mActionText = new ActionBarText(this);
mActionText.setPrimaryText(R.string.app_name);
obj = ((ActionBarExt)obj).getCustomContainer();
((ActionBarContainer)obj).setRightDividerEnabled(true);
((ActionBarContainer)obj).addCenterView(mActionText);
}
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setGId(1);
super.onCreate(savedInstanceState);
final CarouselHost mPanelHost = getCarouselHost();
SetupActionBar();
mPanelHost.addTab("Tab1", this, R.string.tab_1,
R.drawable.ic_tab1,
R.drawable.ic_tab1,
R.drawable.ic_tab1,
(new Intent("com.yourpackage.name.Tab1")));
mPanelHost.addTab("Tab2", this, R.string.tab_2,
R.drawable.ic_tab2,
R.drawable.ic_tab2,
R.drawable.ic_tab2,
(new Intent("com.yourpackage.name.Tab2")));
}
}
HTC AlertDialog​
In order to have alertdialog you need to have OnClickListener and inside OnClickListener you paste alertDialog.
Code:
public void onItemClick(HtcAdapterView<?> parent, View view, int position, long id) {
[COLOR="red"]HtcAlertDialog.Builder[/COLOR] alertDialog = new [COLOR="red"]HtcAlertDialog.Builder[/COLOR]([B]YourActivity.this[/B]);
alertDialog.setTitle(R.string.title_txt);
alertDialog.setMessage(R.string.message_txt);
alertDialog.setIcon(R.drawable.icon);
alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
//Do your actions here when user click Yes
}
});
alertDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Do your action here when user click No.. or just cancel dialog using follow..
dialog.cancel();
}
});
alertDialog.show();
}
As you can see Dialog is not so difficult, but need to identify exaclty which Builder you are gonna use.
YourActivity.this - means the activity where you create the dialog
You can also see available options of what you can implement inside dialog.. like 3 buttons.
Start typing alertDialog. finish with the dot and in Eclipse there will be new pop-up window and you can add some more
Also dont forget alertDialog.show(); at the end, otherwise Dialog wont shows​
HTC ListView and HtcListActivity. ​
There are two ways of implementing Htc List view. You can use Simple Activity, Fragment or just easy use HtcListActivity extension.
For full htc style you need to use Main List layout, and Details layout + List adapter.
Example using Activity
Code:
public class AboutActivity extends Activity {
[COLOR="Red"]HtcListView lv1;[/COLOR]
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_activity);
ArrayList<AboutDetails> image_details = GetSearchResults();
[COLOR="red"]final HtcListView lv1 = (HtcListView) findViewById(R.id.about_list);[/COLOR]
[user=1299008]@supp[/user]ressWarnings("unused")
lv1.setAdapter(new AboutListBaseAdapter(this, image_details));
lv1.setOnItemClickListener(new HtcAdapterView.OnItemClickListener() {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
[user=439709]@override[/user]
public void onItemClick(HtcAdapterView<?> a, View v, int position, long id) {
vibrator.vibrate(50);
Object o = lv1.getItemAtPosition(position);
final AboutDetails obj_itemDetails = (AboutDetails)o;
}
});
}
private ArrayList<AboutDetails> GetSearchResults(){
ArrayList<AboutDetails> results = new ArrayList<AboutDetails>();
AboutDetails item_details = new AboutDetails();
item_details.setName(R.string.mikrosmile);
item_details.setItemDescription(R.string.mikrosmile_info);
item_details.setImageNumber(1);
results.add(item_details);
return results;
}
}
AboutDetails
Code:
public class AboutDetails {
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public int getItemDescription() {
return itemDescription;
}
public void setItemDescription(int itemDescription) {
this.itemDescription = itemDescription;
}
public int getImageNumber() {
return imageNumber;
}
public void setImageNumber(int imageNumber) {
this.imageNumber = imageNumber;
}
private int name ;
private int itemDescription;
private int imageNumber;
}
AboutListBaseAdapter
public class AboutListBaseAdapter extends BaseAdapter {
private static ArrayList<AboutDetails> aboutDetailsrrayList;
private Integer[] imgid = {
R.drawable.mikrosmile,
};
private LayoutInflater l_Inflater;
public AboutListBaseAdapter(Context context, ArrayList<AboutDetails> results) {
aboutDetailsrrayList = results;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return aboutDetailsrrayList.size();
}
public Object getItem(int position) {
return aboutDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.about_htc_details, null);
holder = new ViewHolder();
holder.txt_itemName = ([COLOR="red"]HtcListItem2LineText[/COLOR]) convertView.findViewById(R.id.list_item);
holder.itemImage = ([COLOR="red"]HtcListItemTileImage[/COLOR]) convertView.findViewById(R.id.list_item_img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setPrimaryText(aboutDetailsrrayList.get(position).getName());
holder.txt_itemName.setSecondaryTextSingleLine(false);
holder.txt_itemName.setSecondaryText(aboutDetailsrrayList.get(position).getItemDescription());
holder.itemImage.setTileImageResource(imgid[aboutDetailsrrayList.get(position).getImageNumber() - 1]);
return convertView;
}
static class ViewHolder {
[COLOR="red"]HtcListItem2LineText txt_itemName;
HtcListItemTileImage itemImage;[/COLOR]
}
}
Layout - about_activity
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_white">
<[COLOR="red"]com.htc.widget.HtcListView[/COLOR]
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/about_list"
android:background="@drawable/common_app_bkg"
/>
</LinearLayout>
Layout - about_htc_details
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<[COLOR="red"]com.htc.widget.HtcListItemSeparator [/COLOR]
android:id="@+id/Lseparator"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
[COLOR="red"]<com.htc.widget.HtcListItem
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.htc.widget.HtcListItemTileImage
android:id="@+id/list_item_img" />
<com.htc.widget.HtcListItem2LineText
android:id="@+id/list_item" />
</com.htc.widget.HtcListItem>[/COLOR]
</LinearLayout>
HTC ListView Example by xcesco89
Add HTC Sense Skin support.
In AndroidManifest add this line inside <application tag and before first <activity tag where you have your MainActivity
Code:
<application
[COLOR="Red"]allowSkinChange="true"[/COLOR]
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.yourpackage.name.MainActivity"
There will be Error in Manifest, says "Attribute is missing the Android namespace prefix"
It is fine, to fix it go to - Project -> Clean; Choose your Application and click Ok​
Add reference library to use full HTC app experience
Sense 6
Download this package - View attachment addon-htc_opensense_apis-htc-19.zip
Place the extracted folder to <location of android SDK>\android\sdk\add-ons\
Choose HTC OpenSense SDK 19 when you start building your application. Profit
Sense 5 and below
Refer to this, thanks to Jonny
Make sure HTCExtension lib is appear in your Eclipse project​
Use HTCPreference Activity
Htc Preference is the same as normal Preference but you need to follow some rules when you want to have this.
First in your Preference activity (for example you already have one) change PreferenceActivity to HtcPreferenceActivity.
import com.htc.preference.*;
In prefs.xml (or any other xml file you have your preferences in) make sure everything is using this way
This is original android way.
<PreferenceScreen> </PreferenceScreen>
This is Htc way
<com.htc.preference.HtcPreferenceScreen> </com.htc.preference.HtcPreferenceScreen>
So basically to every single item in preferences you need to add com.htc.preference.Htc****
Here is prefs.xml example
Code:
<?xml version="1.0" encoding="utf-8"?>
<[COLOR="Red"]com.htc.preference.Htc[/COLOR]PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<[COLOR="red"]com.htc.preference.Htc[/COLOR]CheckBoxPreference
android:title="@string/load_icons"
android:summary="@string/load_icons_summary"
android:defaultValue="true"
android:key="show_dialog">
</[COLOR="red"]com.htc.preference.Htc[/COLOR]CheckBoxPreference>
<[COLOR="red"]com.htc.preference.Htc[/COLOR]SwitchPreference
android:title="@string/load_skins"
android:summary="@string/load_skins_summary"
android:switchTextOn="@string/on"
android:switchTextOff="@string/off"
android:defaultValue="true"
android:key="skins_on_start">
</[COLOR="red"]com.htc.preference.Htc[/COLOR]SwitchPreference>
</[COLOR="red"]com.htc.preference.Htc[/COLOR]PreferenceScreen>
This is how Pref activity should be
Code:
package com.yourpackage.name;
import android.os.Bundle;
import android.view.View;
import com.htc.widget.ActionBarContainer;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;
[COLOR="red"]import com.htc.preference.*;[/COLOR]
public class Settings extends [COLOR="red"]Htc[/COLOR]PreferenceActivity {
private ActionBarExt actionBarExt=null;
private ActionBarText actionBarText=null;
private ActionBarContainer actionBarContainer=null;
private void SetupActionBar() {
actionBarExt=new ActionBarExt(this,getActionBar());
actionBarExt.enableHTCLandscape(false);
actionBarContainer=actionBarExt.getCustomContainer();
actionBarText=new ActionBarText(this);
actionBarText.setPrimaryText(R.string.settings);
actionBarContainer.addCenterView(actionBarText);
actionBarContainer.setRightDividerEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
SetupActionBar();
}
}
If you cannot import com.htc.preference.*; (Eclipse will give you error) it means you dont have HtcExtension lib. Make sure you added it before doing this.
​
Great guide! HTC Dialog isn't hard, just the same as the normal dialog, but putting Htc before the dialog stuff, right?
Looking forward to the ListView guide!
MaartenXDA said:
Great guide! HTC Dialog isn't hard, just the same as the normal dialog, but putting Htc before the dialog stuff, right?
Looking forward to the ListView guide!
Click to expand...
Click to collapse
yep dialog ist that hard.. but still need to clarify )
Subscribed ! :good:
Hai So, how could I change the tab's image? Like the one in the DarkSense screenshot?
MaartenXDA said:
Hai So, how could I change the tab's image? Like the one in the DarkSense screenshot?
Click to expand...
Click to collapse
This is not image actually, it is skin support..
I was planning to write about it later ))
OK in manifest after <application tag add this line
allowSkinChange="true"
You will have error. Go to Project and perform a Clean . try and report back
Sent from my Galaxy Nexus using Tapatalk 2
Weird.. Cannot edit ..
So this line should be in the same pack where you have android icon and theme info before the first activity tag
Sent from my Galaxy Nexus using Tapatalk 2
mikrosmile said:
This is not image actually, it is skin support..
I was planning to write about it later ))
OK in manifest after <application tag add this line
allowSkinChange="true"
You will have error. Go to Project and perform a Clean . try and report back
Sent from my Galaxy Nexus using Tapatalk 2
Click to expand...
Click to collapse
Thanks, works great!
Saw the dialog part, nice
Waiting for the listview guide c:
Sent from my awesome fridge
MaartenXDA said:
Saw the dialog part, nice
Waiting for the listview guide c:
Sent from my awesome fridge
Click to expand...
Click to collapse
add an HtcListView it's quite simple!
this works exactly as the android ListView
a simple example:
(as base i've used TabPlus4Demo a sample project in OpenSense SDK samples )
main.xml
HTML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.htc.widget.HtcListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
(Note that you need to use com.htc.widget.HtcListView instead of ListView )
the code:
Code:
public class SimpleTab extends Fragment {
public HtcListView lv;
[user=439709]@override[/user]
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(getArguments() != null) {
int resId = getArguments().getInt("LAYOUT");
if(resId != 0)
return inflater.inflate(resId, null);
}
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.main, container, false);
[B]//Find the view[/B]
lv = (HtcListView) rootView.findViewById(R.id.listView1);
[B]//create the list[/B]
List<String> list = new ArrayList<String>();
[B]//add some list Items ( if you want a custom text , just use list.add("blahblah")[/B]
for (int i =0; i<40; i++) {
list.add("Test"+i);
}
[B]//create the adapter[/B]
ArrayAdapter<?> adapter = new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_1, list);
[B]//Set the adapter[/B]
lv.setAdapter(adapter);
[B]/*
create and set the Listener
you can also implement this in your activity with
"public class youractivity extends Activity implements OnItemClickListener"
then use lv.setOnItemClickListener(this)
*/[/B]
lv.setOnItemClickListener(new OnItemClickListener() {
[user=439709]@override[/user]
public void onItemClick(HtcAdapterView<?> arg0, View v,
int pos, long id) {
[B]//make a Toast Message that displays the Selected item NAME[/B]
Toast.makeText(v.getContext(),"pressed: "+ lv.getItemAtPosition(pos), Toast.LENGTH_SHORT).show();
[B]/*
if you want t perform something different for each item you can use the case statement
switch(pos){
case 0:
Toast.makeText(v.getContext(), "HI!", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(v.getContext(), "HELLO!", Toast.LENGTH_SHORT).show();
break;
[...]
case 39:
//do something
break;
*/[/B]
}
});
return rootView;
}
}
Result:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
please take a look at this snippet too! ======> LINK
Thanks, good to see you around here
EDIT: and, how could I change it to look like the real HTC listview? I mean with dividers and white background?
Sent from my awesome fridge
MaartenXDA said:
Thanks, good to see you around here
EDIT: and, how could I change it to look like the real HTC listview? I mean with dividers and white background?
Sent from my awesome fridge
Click to expand...
Click to collapse
this is not part of OpenSense SDK.. they wont open it. You need to create own framework..
this is actually part of HtcPreferenceActivity which you cant create in Eclipse without proper libs..
Other than that you can create similar png.9 image and make it looks like )
mikrosmile said:
this is not part of OpenSense SDK.. they wont open it. You need to create own framework..
this is actually part of HtcPreferenceActivity which you cant create in Eclipse without proper libs..
Click to expand...
Click to collapse
Couldn't dex2jar be used on HTCExtension.jar then adding HTCExtension to the build path?
Jonny said:
Couldn't dex2jar be used on HTCExtension.jar then adding HTCExtension to the build path?
Click to expand...
Click to collapse
i didnt try it..
MaartenXDA said:
Thanks, good to see you around here
EDIT: and, how could I change it to look like the real HTC listview? I mean with dividers and white background?
Sent from my awesome fridge
Click to expand...
Click to collapse
i think you can create a custom row layout, create a new custom adapter and set the content with your new adapter
Suggestion for the guide: Htc Preferences

Simple Android 4.0 Launcher Manager?

I know this feature or activity is pretty much useless in android 4.0+ but I still like the idea of managing Launchers in your rom.
How would one go about displaying the Launchers installed in their system and when clicked, launches the launcher selected? Even maybe delete/ manage them?
Almost like how LG had Home Managers in their older devices
There is an Intent for the laucher. Google for it.
Then get the PackageManager using
Code:
getPackageManager()
and get all Activities for that Intent by invoking
Code:
queryIntentActivities(Intent intent, int flags)
on the PackageManager (for flags use 0).
If you want to launch them, get the ResolveInfo from the list you get by calling the query method and then its activityInfo. You can get the packageName from the field packageName of the ActivityInfo.
Then launch that package by using
Code:
startActivity(getLaunchIntentForPackage(String package))
Ok, found this on Google. Would be even better: http://stackoverflow.com/questions/12504954/how-to-start-an-intent-from-a-resolveinfo
nikwen said:
There is an Intent for the laucher. Google for it.
Then get the PackageManager using
Code:
getPackageManager()
and get all Activities for that Intent by invoking
Code:
queryIntentActivities(Intent intent, int flags)
on the PackageManager (for flags use 0).
If you want to launch them, get the ResolveInfo from the list you get by calling the query method and then its activityInfo. You can get the packageName from the field packageName of the ActivityInfo.
Then launch that package by using
Code:
startActivity(getLaunchIntentForPackage(String package))
Ok, found this on Google. Would be even better: http://stackoverflow.com/questions/12504954/how-to-start-an-intent-from-a-resolveinfo
Click to expand...
Click to collapse
Thanks for the advice! Il see if I can work with this. Id like to incorperate something like this in my future AOSP based rom and allowing users to manage their launchers (whether it be system or user installed) should be helpful.
So this is what I came up with off hand but all I would get when I debugged it on my device in eclipse was a blank activity. "This code was wrote from memory, excuse any small errors. just to give you an idea what I did"
Code:
package com.Biotic.JBHomeManager;
public class HomeMain extends PreferenceFragment {
protected PackageManager getPackageManager() {
return getActivity().getPackageManager();
}
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.home);
populateLaunchers();
}
private void populateLaunchers() {
Intent intent = new Intent(Intent.CATEGORY.HOME);
final PackageManager pm = getPackageManager();
List<ResolveInfo> rList = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
final PreferenceScreen parent = getPreferenceScreen();
parent.setOrderingAsAdded(false);
for (ResolveInfo info : rList) {
Preference pref = new Preference(getActivity());
Intent prefIntent = new Intent(intent);
prefIntent.setComponent(new ComponentName(
info.activityInfo.packageName, info.activityInfo.name));
pref.setIntent(prefIntent);
CharSequence label = info.loadLabel(pm);
if (label == null) label = info.activityInfo.packageName;
pref.setTitle(label);
parent.addPreference(pref);
}
}
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Android Launchers">
</PreferenceScreen>
Any Ideas?

[Q] How to add function to toggle switches?

I am pretty new to coding and app development. After searching around this is an easy thing to do. But I'm so stupid I can figure it out.
So what I need is to add a hardware function to a toggle switch. If you know how to add function to it. What I need is to send all audio through the earpiece when the toggle switch is on. Can you please put the whole code I need to do this? Please help me out!
Makbrar3215 said:
I am pretty new to coding and app development. After searching around this is an easy thing to do. But I'm so stupid I can figure it out.
So what I need is to add a hardware function to a toggle switch. If you know how to add function to it. What I need is to send all audio through the earpiece when the toggle switch is on. Can you please put the whole code I need to do this? Please help me out!
Click to expand...
Click to collapse
After you define your toggle switch in your xml, you can add a id and then you can
Code:
android:onClick="onSwitchClicked"
anywhere between the toggle block. This will say what to do when the toggle is clicked. Now put the below method in your class to control the toggle status:
Code:
public void onSwitchClicked(View view) {
switch(view.getId()){
case R.id.switch1:
if(switch1.isChecked()) {
// To do when 1st switch is on
}
else {
//To do when 1st switch is off
}
break;
case R.id.switch2:
if(switch2.isChecked()) {
//To do when 2nd switch is on
}
else {
//To do when 2nd switch is off
}
break;
}
}
You can extended to as many switches you want by providing different id's for the switch in xml and controlling that with case statements in the java.
And for controlling the audio, You can use audiomanager class
Code:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setSpeakerphoneOn(true); //sets audio via speaker
am.setWiredHeadsetOn(true); //sets audio via headset
Problem!
vijai2011 said:
After you define your toggle switch in your xml, you can add a id and then you can
Code:
android:onClick="onSwitchClicked"
anywhere between the toggle block. This will say what to do when the toggle is clicked. Now put the below method in your class to control the toggle status:
Code:
public void onSwitchClicked(View view) {
switch(view.getId()){
case R.id.switch1:
if(switch1.isChecked()) {
// To do when 1st switch is on
}
else {
//To do when 1st switch is off
}
break;
case R.id.switch2:
if(switch2.isChecked()) {
//To do when 2nd switch is on
}
else {
//To do when 2nd switch is off
}
break;
}
}
You can extended to as many switches you want by providing different id's for the switch in xml and controlling that with case statements in the java.
And for controlling the audio, You can use audiomanager class
Code:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setSpeakerphoneOn(true); //sets audio via speaker
am.setWiredHeadsetOn(true); //sets audio via headset
Click to expand...
Click to collapse
I did what you said. Yet I encountered a problem. Please see the attached screenshot.
When I hover over the X it says "Attribute name "public" associated with an element type "RadioButton" must be followed by the ' = '
character."
Where do I put the "="
Thanks by the way. You helped me out a lot!
you are mixing java with XML. Honestly, I suggest you start on a few beginner tutorials.
Just tell me what to do. I can't go through the guide. I need this app done by August 14.
Sent from my SGH-I337M using xda app-developers app
Bad Attitude to have. This isn't a "do my work for me" forum.
And like zalez was kind enough to point out, your putting java in xml. If you expect to make an app you need to read some beginner guides first.
you have almost a month to do it.
Thanks, I didn't mean to be rude. Where can I find the guide? :thumbup:
Sent from my SGH-I337M using xda app-developers app
Makbrar3215 said:
Thanks, I didn't mean to be rude. Where can I find the guide? :thumbup:
Sent from my SGH-I337M using xda app-developers app
Click to expand...
Click to collapse
http://developer.android.com/training/index.html
But you should probably start with generic Java 101 stuff
Set Switch status from incoming JSON data
Sorry to bump this thread after such a long time but I am facing a problem with switches myself and would appreciate some help. I have a JSON parser class which is retrieving data from a database. The retrieved info contains 'ID', 'name' and 'status' fields related to a device. I can display ID and name of each of the devices (there are several rows) using a listview but I don't know how to use the 'status' field value (it is either 1 or 0) to set an associated Switch component. I have to set the Switch associated with each listview item to ON if incoming 'status' value is 1 and set it OFF if 'status' is 0. I am lost on where to put the 'setChecked' method for every listview item. Here's the code of activity that shows these list items:
Code:
package com.iotautomationtech.androidapp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.widget.ToggleButton;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Switch;
import android.widget.TextView;
public class AllDevicesActivity extends ListActivity {
Switch mySwitch = (Switch) findViewById(R.id.switchButton);
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "external_link_removed";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "devices";
private static final String TAG_PID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_STATUS = "status";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_devices);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditDeviceActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllDevicesActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String status = c.getString(TAG_STATUS);
int toggleValue = Integer.parseInt(c.getString(TAG_STATUS));
boolean sBool = false;
if (toggleValue == 1) {
sBool = true;
}
if (status.equals("1")) {
status = "Status: ON";
}
else {
status = "Status: OFF";
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewDeviceActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllDevicesActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME, TAG_STATUS},
new int[] { R.id.pid, R.id.name, R.id.status });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
The XML file:
Code:
<RelativeLayout xmlns:android="schemas.android"
android:layout_width="fill_parent"
android:layout_height="65dip"
android:orientation="vertical"
android:layout_centerVertical="true"
>
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/pid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:textSize="17dip"
android:layout_below="@+id/name"
/>
<Switch
android:id="@+id/switchButton"
android:layout_alignParentRight="true"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:layout_centerVertical="true"
android:textOff="OFF"
android:textOn="ON"
android:onClick="onSwitchClicked"
/>
</RelativeLayout>
I have reached thus far with the code: [attached-image]
Now I only have to set those Switches according to status values from database. I have tried putting the setChecked in doInBackground method and onPostExecute method but it doesn't work. Do I have to save all status values in an array and start a loop to set all Switches? Where would that code go?
ANY kind of help/guidance here is appreciated!

[Tutorial] Android Data Transferring | All Methods

Data Transferring Tool for All Android Powered Devices​
Data transmission, digital transmission, or digital communications is the physical transfer of data (a digital bit stream or a digitized analog signal) over a point-to-point or point-to-multipoint communication channel. Examples of such channels are copper wires, optical fibers, wireless communication channels, storage media and computer buses. The data are represented as an electromagnetic signal, such as an electrical voltage, radiowave, microwave, or infrared signal. READ MORE
Click to expand...
Click to collapse
There are several ways to transfer data between different type of devices. For Example, from SmartPhones to Another SmartDevices By Bluetooth that devices should connect together. Your App should handle these Processes. Beside using sensors to transfer data between devices there is In-App feature that allows Send/Receive data inside app. For Example Send Data from App on SmartPhone to client side on SmartWatch. In this way the application use system connection and it will handle data and not processes of detecting & connecting to peer(s).
In this thread I will write several Programming Tutorials for Data Transferring in Android.
Then I will use these tutorials to create a Tool for Send/Receive all types of Data to All Android Powered Devices.
android Auto | Tutorial
android Wear | Watch-To-Phone Tutorial | Phone-To-Watch Tutorial
android TV | Tutorial
Server Download/Upload | Tutorial
Bluetooth | Tutorial
NFC | Tutorial
Wifi Direct| Tutorial
I will use Source Code of Current Geeks Empire Open Source Projects as example.
​
If you want to Contribute in this Project (Tutorials/Tool) Send Message Or Email.
Click to expand...
Click to collapse
Don't Forget to Hit Thanks​
XDA:DevDB Information
Data Transferring, Tool/Utility for all devices (see above for details)
Contributors
Geeks Empire
Source Code: https://play.google.com/store/apps/dev?id=6231624576518993324
Version Information
Status: Testing
Created 2015-08-19
Last Updated 2016-11-16
Android Auto [Data Transferring]
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
​
Android Auto is a smartphone projection standard developed by Google to allow mobile devices running the Android operating system (version 5.0 "Lollipop" and later) to be operated in automobiles through the dashboard's head unit. Android Auto was announced on June 25, 2014, at Google I/O 2014. The Android Auto mobile app was released on March 19, 2015. READ MORE
Click to expand...
Click to collapse
Android Auto was designed with safety in mind. With a simple and intuitive interface, integrated steering wheel controls, and powerful new voice actions, it's designed to minimize distraction so you can stay focused on the road. READ MORE
Click to expand...
Click to collapse
NOTE: Currently Only Media Player & Short Messaging App allowed on Google Play Store for Android Auto Distribution.
But Soon there will be another category for more Hands-Free App on Android Auto.
Android Auto is now available on these cars
Hyundai Sonata (May 2015) Take it to Service Center & Ask for Upgrade to Android Auto
Skoda Fabia (June 2015)
Skoda Superb (June 2015)
Skoda Octavia (June 2015)
Honda Accord (August 2015)
VW Golf/GTI (September 2015)
Chevrolet Cruze (2016)
& Enabled Hardware are
GPS and high-quality GPS antennas
Steering-wheel controls
Sound system
Directional speakers
Directional microphones
Wheel speed
Compass
Mobile antennas
### Let Start Coding ###
What you need is ability to create Hello World! project
Click to expand...
Click to collapse
You will learn How To Use
BroadcastReceiver, Custom Intent-Filet, Send/Receive Text to AndroidAuto,
Notification & CarExtender-Notification & etc.
Click to expand...
Click to collapse
. from Bottom to Top
First you need to Add XML file as meta-data for AndroidManifest to declare app as AndroidAuto Enabled app for system.
Under the .../res/ create xml folder. inside xml folder create xml resource file: automotive_app_desc.xml and Paste this
Code:
<?xml version="1.0" encoding="utf-8"?>
<automotiveApp>
<uses name="[B]notification[/B]"/>
<!-- [I]notification attribute shows your app is messaging app that only create androidAuto compatible notification[/I] -->
</automotiveApp>
then you need to link this file into manifest under <application />
Code:
<meta-data
android:name="com.google.android.gms.car.application"
android:resource="@xml/automotive_app_desc" /
>
After Installing App, It will notify Android Auto System.
There is 2 parts important for AndroidAuto Notification When TTS read content & when want to Reply Or send reply.
So to handle these parts we should define two BroadcastReceiver in manifest & creating classes.
Code:
<receiver android:name=".[B]MessageReadReceiver[/B]">
<intent-filter>
<action android:name="net.test.cartest.[B]ACTION_MESSAGE_READ[/B]"/>
</intent-filter>
</receiver>
<receiver android:name=".[B]ReplyReceiver[/B]" >
<intent-filter>
<action android:name="net.test.cartest.[B]ACTION_MESSAGE_REPLY[/B]" />
</intent-filter>
</receiver>
Then Create Classes & setup both of them as extends BroadcastReceiver.
Inside Activity class we need to create Android-Auto Compatible Notification.
For Messaging App It should be after users receive new message. I used it for Translator App > geTranslate. It means Notification will show translated text on Car Monitor Read result & use Reply action to reuse translator. However Translator App is not allowed for Android Auto.
Before creating notification we should define Intent Action to invoke BroadcastReceiver previously defined.
Code:
private Intent getMessageReadIntent(int conversationId) {
return new Intent().setAction(READ_ACTION).putExtra(CONVERSATION_ID, id); //[FONT=Impact](Check Source Code)[/FONT]
}
//Conversation ID must be unique.
private Intent getMessageReplyIntent(int conversationId) {
return new Intent().setAction(REPLY_ACTION).putExtra(CONVERSATION_ID, conversationId); //[FONT=Impact](Check Source Code)[/FONT]
}
Creating Notification itself is similar to phone notification But we need to extend it for Cars. to do this we need another component UnreadConversation.Builder
Code:
//This element will handle new how create new notifications
UnreadConversation.Builder unreadConversationBuilder = new UnreadConversation.Builder(sender)
.setLatestTimestamp(timestamp)
.setReadPendingIntent(readPendingIntent)
.setReplyAction(replyIntent, remoteInput);
unreadConversationBuilder.addMessage(message);
Then we should use it for CarExtender()
Code:
new CarExtender().setUnreadConversation(unreadConversationBuilder.build();
Here is Complete Function to define and create Android Auto Compatible Notification
Code:
private void sendNotificationToCar(int conversationId, String sender, String message, long timestamp) {
PendingIntent readPendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
conversationId,
getMessageReadIntent(conversationId),
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel("Remote Inpute").build();
PendingIntent replyIntent = PendingIntent.getBroadcast(
getApplicationContext(),
conversationId,
getMessageReplyIntent(conversationId),
PendingIntent.FLAG_UPDATE_CURRENT);
UnreadConversation.Builder unreadConversationBuilder = new UnreadConversation.Builder(sender)
.setLatestTimestamp(timestamp)
.setReadPendingIntent(readPendingIntent)
.setReplyAction(replyIntent, remoteInput);
unreadConversationBuilder.addMessage(message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_launcher))
.setContentText(message)
.setWhen(timestamp)
.setContentTitle(sender)
.setContentIntent(readPendingIntent)
.extend(new CarExtender().setUnreadConversation(unreadConversationBuilder.build()))
.setColor(Color.RED);
NotificationManagerCompat.from(this).notify(conversationId, builder.build());
}
Now you should handle Receivers. inside MessageReadReceiver you need to define actions to occur after TTS Engine read the content of notification & inside ReplyReceiver How reply actions work, in geTranslate I setup ReplayReceiver to invoke translator to translate what user spoke through car microphone.
Code:
public class [B]ReplyReceiver [/B]extends BroadcastReceiver { //[FONT=Impact](Check Source Code)[/FONT]
@Override
public void onReceive(Context context, Intent intent) {
if (MainActivity.REPLY_ACTION.equals(intent.getAction())) {
CharSequence reply = getMessageText(intent);
//Do What you want...
}
}
private CharSequence getMessageText(Intent intent) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
return remoteInput.getCharSequence(MainActivity.EXTRA_VOICE_REPLY);
}
return null;
}
}
Up to Here you did what necessary for your app to support Android Auto. If you have access to real AndroidAuto Powered Vehicle Just Install you app and connect it to car & test your application.
But If you don't have access to androidAuto car you need to use simulator.
Code:
Install [COLOR=RoyalBlue][URL="http://forum.xda-developers.com/attachment.php?attachmentid=3444494&stc=1&d=1439998349"][B]CarNotification.APK[/B][/URL][/COLOR]
Code:
Install [B][URL="http://forum.xda-developers.com/attachment.php?attachmentid=3444491&stc=1&d=1439998135"]Simulator APK[/URL][/B] & Go to Setting > Notification Access > Enable Simulator
HOW TO Test Sample
Code:
[B]Open [/B]CarTest [B]Click [/B]on Text Then Click [B]Home [/B]& [B]Run Simulator [/B]> Wait [B]15Seconds [/B]to See Notification
If you find any mistake Or issue in my codes Please inform me.
Click to expand...
Click to collapse
Don't forget to Hit Thanks
​
Android Wear [Watch-To-Phone][Data Transferring]
​
Android Wear is a version of Google's Android operating system designed for smartwatches and other wearables.
By pairing with mobile phones running Android version 4.3+, Android Wear integrates Google Now technology and mobile notifications into a smartwatch form factor. It also adds the ability to download applications from the Google Play Store.
Android Wear supports both Bluetooth and Wi-Fi connectivity, as well as a range of features and applications. Watch face styles include round, square and rectangular. Released devices include Motorola Moto 360,the LG G Watch, and the Samsung Gear Live.Hardware manufacturing partners include ASUS, Broadcom, Fossil, HTC, Intel, LG, MediaTek, Imagination Technologies, Motorola, Qualcomm, and Samsung.
In the first six months of availability, Canalys estimates that over 720,000 Android Wear smartwatches were shipped.As of 15 May 2015, Android Wear had between one and five million application installations.
Click to expand...
Click to collapse
### Let Start Coding ###​There is just few different between AndroidApp on Phones & AndroidApp on Wearable.
The most important part is Optimizing User-Interface for AndroidWear & then keep in mind which API not available on AndroidWear. These APIs are not available on AndroidWear
android.webkit
android.print
android.app.backup
android.appwidget
android.hardware.usb
NOTE: Do Not set all processes of your app on SmartWatch Module. Cause there is smaller Battery, Memory& weaker Processor.
NOTE: You should use Android Studio to create AndroidWear App.
(However there is tricky way to use eclipse for androidWear But It s better to migrate to AndroidStudio ASAP.)
. from Bottom to Top /Watch Client
When you want to create New Android Application you can choose AndroidWear App Module then AndroidStudio will add necessary AndroidWear Components and Gradle Config automatically.
But you can add AndroidWear Module manually in your current project.
Code:
Go to
File > New > New Module > Select AndroidWear & Next to Finish.
Then you need to add required configuration in your Host Application to Link AndroidWear Module.
Open build.gradle (Module: name-of-project) of Host Application. & Add wearProject to Dependencies
Code:
dependencies{
...
wearApp project{':NAME-OF-PROJECT'}
...
}
Then Save & Sync.
Open AndroidManifest.xml & Add Uses-Features & Uses-Permissions
Code:
<uses-feature android:name="android.hardware.type.watch" />
NOTE: If you want to set new permission that is not in Phone Module you have to set on both Phone & Watch AndroidManifest. (Check Source Code)
There other components are same But for app that want to communicate between Phone & Watch add this meta-data under Application tag
Code:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
There is different between phone layout design & watch. There is 3 layout.xml files for each Activity on SmartWatch.
main.xml Just Hold the View >>
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/watch_view_stub"
android:layout_width="match_parent" android:layout_height="match_parent"
app:rectLayout="@layout/rectangle_main"
app:roundLayout="@layout/round_main"
tools:context=".WearActivity" tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>
Click to expand...
Click to collapse
rectangle_main.xml for Rectangle SmartWatch Display that All GUI components must Add here
round_main.xml for Round SmartWatch Display that All GUI components must Add here
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
[B] tools:context=".MainWear"[/B]
android:background="@android:color/white"
[B]tools:deviceIds="wear_square" [/B]>
[B]<!--
You should set context for activity that handle the components and Id to call on main view.
+
All other components like Buttons, TextViews, EditTextx should call here.
-->[/B]
</RelativeLayout>
Now I should add functionality to GUI components in Java.Classes.
To Communicate with Phone First of all I should Initiate GoogleApiClient to Get Node (Phone) connected to watch & get it ready for data communication
Code:
GoogleApiClient client = new GoogleApiClient.Builder(context).addApi(Wearable.API).build();
new Thread(new Runnable() {
@Override
public void run() {
client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(client).await();
List<Node> nodes = result.getNodes();
if (nodes.size() > 0) {
nodeId = nodes.get(0).getId();
}
client.disconnect();
}
}).start();
Then I should define WatchViewStub Listener & then other components inside it
Code:
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
//Here I should add all other functionalities
}
});
The proper Input Method on Android Wear is Voice Input. The main reason for this is small screens.
To Launch Voice Recognition Engine Call This Method
Code:
private static final int SPEECH_REQUEST_CODE = 0;
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
After This I need to get result from engine so i should define onActivityResult
Code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
//Here I should Handle Results
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
final String speech = results.get(0).toString();[B]
[COLOR=Red]//Here you can send this data to phone[/COLOR][/B]
}
To send data to app client on phone you can use Wearable.MessageApi.sendMessage()
Code:
if (nodeId != null) {
new Thread(new Runnable() {
@Override
public void run() {
client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
Wearable.MessageApi.sendMessage(client, nodeId, MESSAGE, null);
client.disconnect();
}
}).start();
}
. from Bottom to Top /Phone Client
On the Phone Side Open AndroidManifest.xml & Add Service to Handle Received data from Watch with specific <intent-filter/>
<service android:name=".WearReceiver" >
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
Then Create Class extends of WearableListenerService to get data and add all operation we want.
Code:
public class WearReceiver extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
String getWear = messageEvent.getPath(); [COLOR=Red][B]//Text from Watch[/B][/COLOR]
System.out.println("DEVICE >> " + messageEvent.getPath());
}
}
Done! But You Always you need to Test your App.
If you create Signed APK you can Install it on Phone & It will install on AndroidWear automatically.
NOTE: buildToolsVersion must be same on both Phone & Watch Module.
If you have AndroidWear Device Just Connect it & install it like normal app on phones. It same if you run Emulator But to use emulator you need to do few steps before
First go to Android Virtual Device (AVD) Manager & Create Emulator for both Square & Round Display Watches.
Install AndroidWear App from Google Play Store
In AndroidWear App select Emulator
Open Terminal & Go to ADB directory & type this command
Code:
adb -d forward tcp:5601 tcp:5601
On AndroidWear App Select Connect Emulator
Here is 2 Open Source Projects of Geeks Empire to Help you better for tutorial.
geTranslate
APK + Source Code 1-2-3 (Full) | G Drive
Keep Note
APK + Source Code (Full)
If you find any mistake Or issue in my codes Please inform me.
Click to expand...
Click to collapse
Don't forget to Hit Thanks​
Android Wear [Phone-To-Watch][Data Transferring]
​
Android Wear is a version of Google's Android operating system designed for smartwatches and other wearables.
By pairing with mobile phones running Android version 4.3+, Android Wear integrates Google Now technology and mobile notifications into a smartwatch form factor. It also adds the ability to download applications from the Google Play Store.
Android Wear supports both Bluetooth and Wi-Fi connectivity, as well as a range of features and applications. Watch face styles include round, square and rectangular. Released devices include Motorola Moto 360,the LG G Watch, and the Samsung Gear Live.Hardware manufacturing partners include ASUS, Broadcom, Fossil, HTC, Intel, LG, MediaTek, Imagination Technologies, Motorola, Qualcomm, and Samsung.
In the first six months of availability, Canalys estimates that over 720,000 Android Wear smartwatches were shipped.As of 15 May 2015, Android Wear had between one and five million application installations.
Click to expand...
Click to collapse
### Let Start Coding ###
​There is just few different between AndroidApp on Phones & AndroidApp on Wearable.
The most important part is Optimizing User-Interface for AndroidWear & then keep in mind which API not available on AndroidWear. These APIs are not available on AndroidWear
android.webkit
android.print
android.app.backup
android.appwidget
android.hardware.usb
NOTE: Do Not set all processes of your app on SmartWatch Module. Cause there is smaller Battery, Memory& weaker Processor.
NOTE: You should use Android Studio to create AndroidWear App.
(However there is tricky way to use eclipse for androidWear But It s better to migrate to AndroidStudio ASAP.)
WHY Should You Transfer Data to AndroidWear Client?
- Beside using Wear Notification Extender to deliver info to SmartWatch you can transfer data to Watch Client of your App directly. In this way you will be able to do some modification on data before displaying to users on watches OR sharing data securely inside your app.
from Bottom to Top /Watch Client
When you want to create New Android Application you can choose AndroidWear App Module then AndroidStudio will add necessary AndroidWear Components and Gradle Config automatically.
But you can add AndroidWear Module manually in your current project.
Code:
Go to
File > New > New Module > Select AndroidWear & Next to Finish.
Then you need to add required configuration in your Host Application to Link AndroidWear Module.
Open build.gradle (Module: name-of-project) of Host Application. & Add wearProject to Dependencies
Code:
dependencies{
...
wearApp project{':NAME-OF-PROJECT'}
...
}
Then Save & Sync.
Open AndroidManifest.xml & Add Uses-Features & Uses-Permissions
Code:
<uses-feature android:name="android.hardware.type.watch" />
NOTE: If you want to set new permission that is not in Phone Module you have to set on both Phone & Watch AndroidManifest. (Check Source Code)
There other components are same But for app that want to communicate between Phone & Watch add this meta-data under Application tag
Code:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
***​AndroidWear GUI Guide
There is different between phone layout design & watch. There is 3 layout.xml files for each Activity on SmartWatch.
main.xml Just Hold the View >>
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/watch_view_stub"
android:layout_width="match_parent" android:layout_height="match_parent"
app:rectLayout="@layout/rectangle_main"
app:roundLayout="@layout/round_main"
tools:context=".WearActivity" tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>
rectangle_main.xml for Rectangle SmartWatch Display that All GUI components must Add here
round_main.xml for Round SmartWatch Display that All GUI components must Add here
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainWear"
android:background="@android:color/white"
tools:deviceIds="wear_square" >
<!--
You should set context for activity that handle the components and Id to call on main view.
+
All other components like Buttons, TextViews, EditTextx should call here.
-->
</RelativeLayout>
You should set context for activity that handle the components and Id to call on main view.
+
All other components like Buttons, TextViews, EditTextx should call here.
***​
I should define an Activity & Service extends WearableListenerService for Wear Module.
The Service must have intent filter to Bind it.
Code:
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
Inside the listener you can handle the Received Data But in this example I just get the Intent-Filter-Action & launch the activity by getting the data path
Code:
if(messageEvent.getPath().equalsIgnoreCase("String Intent Filter Action")){
//Start Activity
}
In this example I used simple text as data so to get text data I can setup data like this
Code:
@Override
public void onMessageReceived(MessageEvent messageEvent) {
String receivedTextFromPhoneClient = messageEvent.getData().toString();
}
In case you want to launch an activity you have to setup some implementation to receive data
Code:
public class MainActivity extends Activity implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks{}
& put all necessary override functions
Code:
public void onMessageReceived( final MessageEvent messageEvent ) {}
public void onConnected(Bundle bundle) {}
public void onConnectionSuspended(int i) {}
Inside activity I must initialize the GoogleApiClient to connect to peer and get data
Code:
GoogleApiClient mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.addConnectionCallbacks( this )
.build();
if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )
mApiClient.connect();
Make Sure to release Listener when quitting the activity
Code:
@Override
protected void onStop() {
if ( mApiClient != null ) {
Wearable.MessageApi.removeListener( mApiClient, this );
if ( mApiClient.isConnected() ) {
mApiClient.disconnect();
}
}
super.onStop();
}
To get Received Data from peer you should do it inside onMessageReceived function //(Check Source Code)
Code:
@Override
public void onMessageReceived( final MessageEvent messageEvent ) {
runOnUiThread( new Runnable() {
@Override
public void run() {
if( messageEvent.getPath().equalsIgnoreCase( WEAR_MESSAGE_PATH ) ) {
mAdapter.add(new String([B]messageEvent.getData()[/B])); //Data from Phone
mAdapter.notifyDataSetChanged();
}
}
});
}
But Where is Data come from?
Let Code the Phone Client to Send Data to SmartWatch.
from Bottom to Top /Phone Client
First of All Don't forget to add <meta-data/> to AndroidManifest.xml under <Application/>
Inside Activity Setup Views as you prefer to get user input & send it to watch.
In this example I set EditText & Button to get input and send data.
Like Watch Client First initialize GoogleApiClient so that I need to add implementation to GoogleApiClient.ConnectionCallbacks & its override functions.
Just Call these three functions at onCreate session.
Code:
private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.build();
mApiClient.connect();
}
Code:
private void init() {
mSendButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = mEditText.getText().toString();
if (!TextUtils.isEmpty(text)) {
sendMessage(WEAR_MESSAGE_PATH, text);
}
}
});
}
& final Send function to Watch Client
Code:
private void [COLOR=Red][B]sendMessage[/B][/COLOR]( final String path, final String text ) {
new Thread( new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mApiClient ).await();
for(Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
mApiClient, node.getId(), path, text.getBytes() ).await();
}
runOnUiThread( new Runnable() {
@Override
public void run() {
mEditText.setText( "" );
}
});
}
}).start();
}
NOTE: There is two parameters that the values & names must be same inside both Phone & Watch Client
The String of Intent-Filter-Action & Message-Path
Code:
private static final String ACTION = "/start_activity";
private static final String WEAR_MESSAGE_PATH = "/message";
NOTE: buildToolsVersion must be same on both Phone & Watch Module.
If you have AndroidWear Device Just Connect it & install it like normal app on phones. It same if you run Emulator But to use emulator you need to do few steps before
First go to Android Virtual Device (AVD) Manager & Create Emulator for both Square & Round Display Watches.
Install AndroidWear App from Google Play Store
In AndroidWear App select Emulator
Open Terminal & Go to ADB directory & type this command
Code:
adb -d forward tcp:5601 tcp:5601
On AndroidWear App Select Connect Emulator
Here is 2 Open Source Projects of Geeks Empire to Help you better for tutorial.
geTranslate
APK + Source Code 1-2-3 (Full)
Keep Note
[URL="http://forum.xda-developers.com/attachment.php?attachmentid=3427464&d=1438637814"]APK + Source Code (Full)[/URL]
NOTE:
If you find any mistake Or issue in my codes Please inform me.
Click to expand...
Click to collapse
Don't forget to Hit Thanks
Android TV [Data Transferring]
​
Android offers a rich user experience that's optimized for apps running on large screen devices, such as high-definition televisions. Apps on TV offer new opportunities to delight your users from the comfort of their couch...
Android TV apps use the same structure as those for phones and tablets. This similarity means you can modify your existing apps to also run on TV devices or create new apps based on what you already know about building apps for Android.
Click to expand...
Click to collapse
### Let Start Coding ###​For TV Apps you must just Focus on GUI. You can use all your codes for Phone App but for Bigger Layout Components.
Also There might be some limitation for Hardware on TV.
This is list of Un-Supported Hardware feature on Android TV
Code:
Touchscreen - android.hardware.touchscreen
Touchscreen emulator - android.hardware.faketouch
Telephony - android.hardware.telephony
Camera - android.hardware.camera
Near Field Communications (NFC) - android.hardware.nfc
GPS - android.hardware.location.gps
Microphone - android.hardware.microphone
Sensors - android.hardware.sensor
. from Bottom to Top
In AndroidManifest.xml you must declare <activity /> for TV class with different Category for Intent Filter & Theme.
Code:
<activity
android:name="com.example.android.TvActivity"
android:label="@string/app_name"
android:theme="@style/[COLOR=Red][B]Theme.Leanback[/B][/COLOR]">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="[COLOR=Red][B]android.intent.category.LEANBACK_LAUNCHER[/B][/COLOR]" />
</intent-filter>
</activity>
& Under <manifest/> declare TV Support Library usage.
Code:
<uses-feature android:name="android.software.leanback"
android:required="false" />
Before/After this declaration you need to configue Hardware feature requirement. For Example:
Code:
...
<uses-feature android:name="android.hardware.touchscreen"
android:required="false" />
...
Also When you want to Add TV support you should design new graphical element as banner for LeanBackLauncher
Under <application /> add
Code:
...
android:banner="@drawable/banner"
...
After this Focus on GUI & Test App. You need to define bigger graphical component that user can see from longer distance compare when look at phone or tablet.
GUI Guide for Android TV App
NOTE: Creating TV Module Using Android Studio Will Add Everything you need for TV App automatically.
If you find any mistake Or issue in my codes Please inform me.
Click to expand...
Click to collapse
Don't forget to Hit Thanks
​
Server [Upload | Download][Data Transferring]
HOW TO Create Cloud Base App Using Google Cloud
HOW TO Create Cloud Base App Using Custom Server (Coming Soon)
Bluetooth [Data Transferring]
​
The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.
Using the Bluetooth APIs, an Android application can perform the following:
Scan for other Bluetooth devices
Query the local Bluetooth adapter for paired Bluetooth devices
Establish RFCOMM channels
Connect to other devices through service discovery
Transfer data to and from other devices
Manage multiple connections
Click to expand...
Click to collapse
Using Bluetooth can be so Helpful in many cases.
Send to & Receive Data from many devices. SmartPhone,any Bluetooth stations & etc.
But If you want to have control of what happen with data sharing with Bluetooth you must learn How to Transfer Data through Bluetooth Socket. For many simple cases Android Provided Intent base Bluetooth Sharing But It is not enough. So I will write here how to use Bluetooth for both situation.
### Let Start Coding ###​
Additionally in this tutorial you will learn How to access Files & Converting to byteArray.
. from Bottom to Top /Watch Client
Start New Project & Leave everything to default.
After Sync Gradle Open AndroidManifest.xml to Add Permissions.
Code:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> //to Enable/Disable Bluetooth
<uses-permission android:name="android.permission.BLUETOOTH"/>
Also for Android API 23+ you should ask for Runtime Permission too.
Code:
if(Build.VERSION.SDK_INT >= 23) {
String[] Permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN};
requestPermissions(Permissions, 10296);
}
Then Open your Bluetooth Activity and Add this Code.
Here is Simple Way to Share File using Bluetooth. Like other Intent sharing Just Invoke its intent-filter
Code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");//define type
File f = new File(Environment.getExternalStorageDirectory(), "DIR_TO_FILE");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(intent);
Little More Complex Way is to use Bluetooth Socket.
It finds List of All Paired Bluetooth Devices.
Code:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
System.out.println(device.getName() + "\n" + device.getAddress() + "\n\n");
}
}
to connect to another device you need to have its MacAddress.
Code:
String macAddress = new BluetoothDevice().getAddress();
BluetoothDevice [COLOR=Red][B]device [/B][/COLOR]= mBluetoothAdapter.getRemoteDevice(macAddress);
After this you should invoke instance of Socket to start Connection.
NOTE: Nothing should define on Main View. In other hand you have to use Thread(); for background Process.
Code:
Connecting [B]connectThread [/B]= new Connecting([COLOR=Red][B]device [/B][/COLOR]/*Pass Device Information to Thread()*/);
[B]connectThread[/B].start();
Code:
private class Connecting extends Thread {
private final BluetoothSocket [COLOR=Red]bluetoothSocket [/COLOR];
private final BluetoothDevice bluetoothDevice ;
private String mSocketType;
public ConnectThread(BluetoothDevice device) {
bluetoothDevice = device;
[B]BluetoothSocket [/B][COLOR=Red]tmp [/COLOR]= null;
try {
[COLOR=Red]tmp [/COLOR]= device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
catch (IOException e) {e.printStackTrace();}
[COLOR=Red]bluetoothSocket [/COLOR]= tmp;
}
public void run() {
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
try {
[COLOR=Red][B]bluetoothSocket.connect();[/B][/COLOR]
} catch (IOException e) {
try {
[COLOR=Red]bluetoothSocket[/COLOR].close();
} catch (IOException e2) {e2.printStackTrace();break;}
return;
}
[COLOR=RoyalBlue][B]//Handle Actions After Connecting.
//Ask to Write & Read Socket Data
// SO HERE you should start another Thread(); to handle [COLOR=Red]Write/Read[/COLOR] Process
[/B][/COLOR][COLOR=RoyalBlue][B][COLOR=Red][COLOR=SeaGreen]globalConnectedThread [/COLOR]= new ConnectedThread(socket, socketType).start();[/COLOR][/B][/COLOR][COLOR=RoyalBlue][COLOR=Black]//pass the socket Information[/COLOR][/COLOR]}
public void cancel() {
try {
bluetoothSocket.close();
} catch (IOException e) {e.printStackTrace();}
}
}
So Check this snippet Carefully and Read Comments
Code:
private class ConnectedThread extends Thread {
private final BluetoothSocket [B]mmSocket[/B];
private final InputStream [COLOR=Red][B]mmInStream[/B][/COLOR];
private final OutputStream [COLOR=Red][B]mmOutStream[/B][/COLOR];
public ConnectedThread(BluetoothSocket socket, String socketType) {
[B]mmSocket [/B]= socket;[COLOR=Red] //you call it on Connecting thread after successful connecting process So pass the socket to this thread[/COLOR]
InputStream [B]tmpIn [/B]= null;
OutputStream [B]tmpOut [/B]= null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = [B]socket[/B].getInputStream();
tmpOut = [B]socket[/B].getOutputStream();
} catch (IOException e) {}
[COLOR=Red][B]mmInStream [/B][/COLOR]= tmpIn;
[COLOR=Red][B]mmOutStream [/B][/COLOR]= tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (/*define indicator to check if sockets are connected you can do it by using a integer or boolean var*/) {
try {
// Read from the InputStream in loop until connection not lost
bytes = [B]mmInStream[/B].[B][COLOR=Red]read([COLOR=Black]buffer[/COLOR])[/COLOR][/B];
// Send the obtained bytes to the UI Activity OR SAVE Data
//so we call a function save data to file.
saveFile("name_of_file");
//pass read data to views
} catch (IOException e) {
// Start the service over to restart listening mode
this.start();
break;
}
}
}
public void [B]write[/B](byte[] buffer) {//next snippet will show how to use it.
try {
[B]mmOutStream[/B].[COLOR=Red][B]write([COLOR=Black]buffer[/COLOR])[/B][/COLOR];
// Share the sent message back to the UI Activity
} catch (IOException e) {}
}
public void cancel() {
try {
[B]mmSocket[/B].close();
} catch (IOException e) {}
}
}
To Write to Socket from File you must read content of File (that is why we got permission for reading sdcard) and convert it to array of byte and pass to socket. So define ConnectedThread and call write(byte[]) function
Code:
ConnectedThread connectedThread = [COLOR=SeaGreen][B]globalConnectedThread[/B][/COLOR];//this is the value defined after socket connected.
byte[] sendData = [B]readFile[/B]("file_name_on_sdcard").getBytes();//Check out next snippet for readFile Function
[B]connectedThread.[COLOR=Red]write[/COLOR](sendData);[/B]
This Function to read simple text file from sdcard
Code:
public String [B]readFile[/B](String fileName){
String temp = "";
File G = new File(Environment.getExternalStorageDirectory(), fileName);
if(!G.exists()){
System.out.println(fileName + " NOT FOUND");
}
else{
try{
BufferedReader br = new BufferedReader(new FileReader(G));
int c;
temp = "";
while((c = br.read()) != -1){
temp = temp + Character.toString((char)c);
}
}
catch(Exception e){e.printStackTrace();}
}
return temp;//content of file
}
In other hand you must Handle read data on socket and save it.
Code:
public void [B]saveFile[/B](String fileName, String content){
File G = new File(Environment.getExternalStorageDirectory(), fileName);
try {
FileOutputStream fOut = new FileOutputStream(G);
fOut.write((content).getBytes());
fOut.flush();
fOut.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
NOTE: As you may realized this tutorial contained functionality for both Client & Server Side of Bluetooth connection.
So when compiled your test app, install it on 2 devices & pair them.
Download SourceCode & Enjoy
If you find any mistake Or issue in my codes Please inform me
Click to expand...
Click to collapse
Don't forget to Hit Thanks
​
WiFi Direct | Android Beam [Client|Server][Data Transferring]
Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later devices with the appropriate hardware to connect directly to each other via Wi-Fi without an intermediate access point (Android's Wi-Fi P2P framework complies with the Wi-Fi Alliance's Wi-Fi Direct™ certification program). Using these APIs, you can discover and connect to other devices when each device supports Wi-Fi P2P, then communicate over a speedy connection across distances much longer than a Bluetooth connection. This is useful for applications that share data among users, such as a multiplayer game or a photo sharing application.
The Wi-Fi P2P APIs consist of the following main parts:
Methods that allow you to discover, request, and connect to peers are defined in the WifiP2pManager class.
Listeners that allow you to be notified of the success or failure of WifiP2pManager method calls. When calling WifiP2pManager methods, each method can receive a specific listener passed in as a parameter.
Intents that notify you of specific events detected by the Wi-Fi P2P framework, such as a dropped connection or a newly discovered peer.
You often use these three main components of the APIs together. For example, you can provide a WifiP2pManager.ActionListener to a call to discoverPeers(), so that you can be notified with the ActionListener.onSuccess() and ActionListener.onFailure() methods. A WIFI_P2P_PEERS_CHANGED_ACTION intent is also broadcast if the discoverPeers() method discovers that the peers list has changed.
Click to expand...
Click to collapse
WiFi Direct can be used as main part of data transferring Or you can use it after initial connection with NFC. Android System uses Wifi Direct & Bluetooth to transfer a large amount of data after NFC connection.
### Let Start Coding ###​
Wifi Direct has 2 part: Client & Server.
Your app must have contains both parts to be able to transfer data via Wifi Direct.
Both sides need 3 classes: Activity - BroadcastReceiver - Server.
Client Side needs more work to do, so I begin with Wifi Direct - Client
Click to expand...
Click to collapse
Note: Developing for Socket Communication needs vivid vision to understand how it works. Then use all components correctly step by step. So I will explain more about each section. Read Carefully.
. Wifi Direct | Client
Create a Project on Android Studio. Set minSdk to API 14 & Open AndroidManifest.xml to add required permissions and features
Code:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I don't enter to layout.xml & won't talk about GUI/UX in this tutorial. So you can design your layout and put codes and expanding functionality of your apps.
Click to expand...
Click to collapse
So create 3 classes & register them to Manifest.
ClientActivity
ClientReceiver
ClientService
Open ClientActivity. First, I need a file for sending over Wifi. You can use a file inside your app as assets, from SdCard Or create general FilePicker.
I set an image from /sdcard/Download/ directory. @onCreate(Bundle)
Code:
File [B][COLOR="Red"]fileToSend [/COLOR][/B]= new File(Environment.getExternalStorageDirectory().getPath() + "/Download/shuttle.jpg");
Now, You should initialize instance WifiP2pManager @onCreate(Bundle)
Code:
WifiP2pManager wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
Channel wifichannel = wifiManager.initialize(this, getMainLooper(), null);
Then, You should Register a BroadcastReceiver with proper IntentFilter Actions.
Code:
IntentFilter wifiClientReceiverIntentFilter = new IntentFilter();
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
registerReceiver([B]ClientReceiver[/B]/*I will edit receiver later*/, wifiClientReceiverIntentFilter);
After this, You should invoke Peer Descovery. Also, It makes device discoverable
Code:
wifiManager.discoverPeers(wifichannel, new WifiP2pManager.ActionListener() {
public void onSuccess() {
//done
}
public void onFailure(int i) {
}});
And wait in ClientReceiver to setup peers. But, It is better to create a function in ClientActivity & pass instance of activity to receiver to access the functions. Check Sample Codes
Code:
public void [B][COLOR="red"]listOfPeers[/COLOR][/B](final WifiP2pDeviceList peers) {
ListView peerView = (ListView) findViewById(R.id.peers_listview);
ArrayList<String> peersStringArrayList = new ArrayList<String>();
for(WifiP2pDevice wd : peers.getDeviceList()) {
peersStringArrayList.add(wd.deviceName);
}
peerView.setClickable(true);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, peersStringArrayList.toArray());
peerView.setAdapter(arrayAdapter);
peerView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {
TextView tv = (TextView) view;
WifiP2pDevice device = null;
for(WifiP2pDevice wd : peers.getDeviceList()) {
if(wd.deviceName.equals(tv.getText()))
device = wd;
}
[B][COLOR="red"]if(device != null) {
/*Connect to first Device*/
targetDevice = device;
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
wifiManager.connect(wifichannel, config, new WifiP2pManager.ActionListener() {
public void onSuccess() {}
public void onFailure(int reason) {}
});
}[/COLOR][/B]
}});}
Open ClientReceiver to request list of peers when proper action occurs Check Sample Codes
Code:
if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
manager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
public void onPeersAvailable(WifiP2pDeviceList peers) {
[COLOR="DarkOrchid"]activity[/COLOR].[B][COLOR="red"]listPeers[/COLOR][/B](peers);
}});
}
I set codes for connecting to a peer. But rewrite it here to consider it here in details
Code:
public void connectToPeer(final WifiP2pDevice wifiPeer){
targetDevice = wifiPeer;
/*[COLOR="Red"]Wi-Fi P2p configuration for setting up a connection[/COLOR]*/
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = wifiPeer.deviceAddress;
wifiManager.connect([COLOR="red"]wifichannel[/COLOR], [COLOR="red"]config[/COLOR], new WifiP2pManager.ActionListener() {
public void onSuccess() {}
public void onFailure(int reason) {}
}); }
Now, It is time to perform sending actions in ClientService and for this you should pass required info to service to open socket and write on it...
Port Number
WifiP2pInfo
File
So, After registering a receiver to get information when proper actions occur & getting list of peers, connection stablished & Now you can write data to Socket.OutputStream
Code:
Intent clientServiceIntent = new Intent(this, ClientService.class);
clientServiceIntent.putExtra("fileToSend", [B][COLOR="red"]fileToSend[/COLOR][/B]);
clientServiceIntent.putExtra("port", Integer.valueOf([B][COLOR="red"]port[/COLOR][/B]/*set what you want*/));
clientServiceIntent.putExtra("wifiInfo", [B][COLOR="Red"]wifiInfo[/COLOR][/B]);
startService(clientServiceIntent);
Then Open ClientService & set to get passed data
Code:
port = (Integer) intent.getExtras().get("port");
fileToSend = (File) intent.getExtras().get("fileToSend");
clientResult = (ResultReceiver) intent.getExtras().get("clientResult");
wifiInfo = (WifiP2pInfo) intent.getExtras().get("wifiInfo");
Now you can define socket and try to write data.
Before that you need proper address of target device
Code:
InetAddress [COLOR="RoyalBlue"]targetIP [/COLOR]= [B][COLOR="red"]wifiInfo[/COLOR][/B].groupOwnerAddress;
Now you can open socket and writing data Check Sample Codes
Code:
try {
Socket clientSocket = new Socket([B][COLOR="royalblue"]targetIP[/COLOR][/B], [B][COLOR="Red"]port[/COLOR][/B]);
OutputStream os = clientSocket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
InputStream is = clientSocket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
byte[] buffer = new byte[4096];
FileInputStream fis = new FileInputStream(fileToSend);
BufferedInputStream bis = new BufferedInputStream(fis);
[B][COLOR="RoyalBlue"]while(true) {
int bytesRead = bis.read(buffer, 0, buffer.length);
if(bytesRead == -1) {break;}
[SIZE="4"]os.write(buffer,0, bytesRead);[/SIZE]
os.flush();
}[/COLOR][/B]
fis.close();
bis.close();
br.close();
isr.close();
is.close();
pw.close();
os.close();
clientSocket.close();
}
catch(Exception e) {e.printStackTrace();}
. Wifi Direct | Server
First, Register a BroadcastReceiver with Same IntentFilter Actions to get notification of connection information & specify a Path to store receiving data
Code:
/*this should be a directory*/
File downloadTarget = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/");
Then start service and pass required data and prepare it to listen on socket with same port
Path to Store Data
Port Number
Code:
Intent serverServiceIntent = new Intent(this, ServerService.class);
serverServiceIntent.putExtra("saveLocation", downloadTarget);
serverServiceIntent.putExtra("port", Integer.valueOf(port));
startService(serverServiceIntent);
Now you can accept socket from specified port on server side and listen to read data Check Sample Codes
Code:
try {
[B][COLOR="royalblue"]ServerSocket welcomeSocket = new ServerSocket(port);
Socket socket = welcomeSocket.accept();[/COLOR][/B]
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
String inputData = "";
String savedAs = "WifiDirectDemo." + System.currentTimeMillis();
File file = new File(saveLocation, savedAs);
byte[] buffer = new byte[4096];
int bytesRead;
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
/*[B][COLOR="Red"]Read data from Socket & Write it to a file[/COLOR][/B]*/
while(true)
{
bytesRead = is.read(buffer, 0, buffer.length);
if(bytesRead == -1){break;}
bos.write(buffer, 0, bytesRead);
bos.flush();
}
bos.close();
socket.close();
}
catch(Exception e) {e.printStackTrace();}
Download Source Code & Enjoy
If you find any mistake Or issue in my codes Please inform me
Click to expand...
Click to collapse
Don't forget to Hit Thanks​
NFC [Data Transferring][Send File][Write Data to NFC Tag][Read Data from NFC Tag]
Android allows you to transfer large files between devices using the Android Beam file transfer feature. This feature has a simple API and allows users to start the transfer process by simply touching devices. In response, Android Beam file transfer automatically copies files from one device to the other and notifies the user when it's finished.
Click to expand...
Click to collapse
NFC (Near Field Communication) is the best way to transfer important data.
The data can be simple URI of File (Image & etc) from your SdCard, Info of Music Track Or Bank Account Information for transferring money.
And I think, It will be inside all electronic devices in near future.
Read More
Different Usage of NFC | How Secure is NFC
What is NFC Tag​
You will learn How to...
I. Transfer Simple File to another Device by NFC
II. Write Data to NFC Tag
III. Read Data from NFC Tag
Click to expand...
Click to collapse
### Let Start Coding ###​
. Send a File
Create a Project on Android Studio. Set minSdk to API 16 & Open AndroidManifest.xml to add required permissions and features
Code:
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BIND_NFC_SERVICE"/>
<uses-feature android:name="android.hardware.nfc" android:required="true" />
Open layout.xml and add <Button /> & <ImageView/> whereever you like.
Then Open Activity.Java to initialize NfcAdapter & Set URI of Data.
For example, I used a simple ImagePicker to get URI of Image File and set it for NFC transferring.
Code:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 143);
/*then call [COLOR="Red"]onActivityResult[/COLOR] to get URI of file*/
}
});
After clicking on ImagePicker Button, it will redirect you to select an image. Then you will back to the app and get information of the picked image. Also, you can define Uri imageUri; to store the data and use is later.
Code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 143:
if(resultCode == RESULT_OK){
try {
/*this is what app need to set for nfc*/
/*you can also set it to nfcAdapter here*/
[B][COLOR="red"]Uri imageUri[/COLOR];[/B] = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
}
catch (FileNotFoundException e) {e.printStackTrace();}
}}}
You can also set the URI to nfcAdapter here. I set when image show to user and He/She get what exactly going on and then by clicking on ImageView confirm & set it to NfcAdapter.
Code:
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*NfcAdapter init*/
[B]nfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());[/B]
// Check whether NFC is enabled on device
if(!nfcAdapter.[B]isEnabled[/B]()){
// NFC is disabled, show the settings to enable NFC
Toast.makeText(getApplicationContext(), "Turn On NFC", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
}
// Check whether Android Beam feature is enabled on device
else if(!nfcAdapter.[B]isNdefPushEnabled[/B]()) {
// Android Beam is disabled, show the settings to enable Android Beam
Toast.makeText(getApplicationContext(), "Enable Android Beam", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
}
else {
// NFC and Android Beam both are enabled
if (imageUri != null) {
/*URI if File set to NfcAdapter for transfering*/
nfcAdapter.setBeamPushUris(new Uri[]{[B][COLOR="red"]Uri imageUri[/COLOR];[/B]}, MainActivity.this);
}
else {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 143);
}
}}});
Now, users should touch their devices and wait for files.
Check out this line.
Code:
nfcAdapter.setBeamPushUris(new Uri[]{[B][COLOR="red"]Uri imageUri[/COLOR];[/B]}, MainActivity.this);
You can call it whenever you want & as you can see, there can be several URI of files.
. Write Data to NFC Tag
There is no additional permissions & features for this. Check If your NFC Tags are Writable.
Initialize NfcAdapter @oncreate(Bundle)
Code:
NfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());
Then add <Intent-filter/> to detect NFC connections. I set filters as a PendingIntent [COLOR="RoyalBlue" [user=815433]@onres[/user]ume()[/COLOR][/B]
[CODE]IntentFilter ACTION_TAG_DISCOVERED = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter ACTION_NDEF_DISCOVERED = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter ACTION_TECH_DISCOVERED = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter[] nfcIntentFilter = new IntentFilter[]{ACTION_TAG_DISCOVERED, ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED};
PendingIntent pendingIntent = PendingIntent.
getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
if(nfcAdapter != null) {
[B]nfcAdapter[/B].[COLOR="red"]enableForegroundDispatch[/COLOR](this, pendingIntent, nfcIntentFilter, null);
/*enableForegroundDispatch; This will give give priority to the foreground activity when dispatching a discovered Tag to an application.*/
}
[/CODE]
Now, you can detect when NfcTag touch your device. So you need to initialize NFC.Tag instance & store is in Tag tag;
Code:
@Override
protected void onNewIntent(Intent intent) {
[B][COLOR="red"]tag [/COLOR][/B]= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
I use ImagePicker Button to perform writing action on press and hold.
I will write String of Image URI to NFC tag.
Code:
picker.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if([B][COLOR="red"]tag [/COLOR][/B]!= null) {
Ndef ndef = Ndef.get([B][COLOR="red"]tag[/COLOR][/B]);
if (ndef != null) {
try {
ndef.connect();
NdefRecord mimeRecord = NdefRecord.createMime("text/plain", [B][COLOR="red"]imageUriString[/COLOR][/B].getBytes());
ndef.writeNdefMessage(new NdefMessage(mimeRecord));
ndef.close();
Toast.makeText(getApplicationContext(), "WriteDone!", Toast.LENGTH_LONG).show();
}
catch (Exception e) {e.printStackTrace();}
}
}return true;}});
It is mandatory for all Android devices with NFC to correctly enumerate Ndef on NFC Forum Tag Types 1-4, and implement all NDEF operations as defined in this class.
Click to expand...
Click to collapse
. Read Data from NFC Tag
Initialize NfcAdapter, define PendingIntent with specific NfcAdapter.ACTIONS @onresume() & initialize NFC.Tag.
When you have instance of Tag then you can Read/Write.
Now, I get String of imageURI from NFC.Tag and set image to ImageView.
Code:
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if([B][COLOR="red"]tag [/COLOR][/B]!= null) {
Ndef ndef = Ndef.get([B][COLOR="red"]tag[/COLOR][/B]);
try {
ndef.connect();
NdefMessage ndefMessage = ndef.getNdefMessage();
[B]String nfcInfo = new String(ndefMessage.getRecords()[0].getPayload());
[COLOR="red"]imageUri [/COLOR]= Uri.parse(nfcInfo);[/B]
InputStream imageStream = getContentResolver().openInputStream(imageUri);
Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
ndef.close();
Toast.makeText(getApplicationContext(), "ReadDone!", Toast.LENGTH_LONG).show();
}
catch (Exception e) {e.printStackTrace();}
} return true; } });
Download Source Code & Enjoy
If you find any mistake Or issue in my codes Please inform me
Click to expand...
Click to collapse
Don't forget to Hit Thanks​
Reserved
Reserved
Index of Data Transferring Tutorials
Latest Data Transferring Tutorial
android Auto | Tutorial
android Wear | Watch-To-Phone Tutorial | Phone-To-Watch Tutorial
android TV | Tutorial
Server Download/Upload | Tutorial
Bluetooth | Tutorial
NFC | Tutorial
Wifi Direct| Tutorial
Don't forget to Hit Thanks​

Categories

Resources