public BluetoothAdapter bluetoothAdapter;
//start search
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null)return;
bluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
bluetoothflag=true;
registerReceiver(mReceiver, new IntentFilter(
BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
///////////////////////////
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String bluetooth = new String("111111111");
BluetoothDevice remote=bluetoothAdapter.getRemoteDevice(bluetooth);
if(device.equals(remote)){
deviceFoundFlag=true;
// bluetoothAdapter.cancelDiscovery();
}
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if(!deviceFoundFlag){
//////////alert if no device found
bluetoothflag=false;
}
if(bluetoothflag){
bluetoothAdapter.startDiscovery();
deviceFoundFlag=false;
}
}
}};
Wednesday, February 22, 2012
Android Wifi Connected info such as signal strength
WifiManager mainWifi;
String cSummary="Not Connected" ;
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = mainWifi.getConnectionInfo();
if(wifiInfo.getBSSID()!=null){
int strength=WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5);
int speed=wifiInfo.getLinkSpeed();
String units=WifiInfo.LINK_SPEED_UNITS;
String ssid=wifiInfo.getSSID();
cSummary=String.format("Connected to %s at %s%s. Strength %s/5", ssid,speed,units,strength);
if(strength==1)play();
}
return cSummary;
String cSummary="Not Connected" ;
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = mainWifi.getConnectionInfo();
if(wifiInfo.getBSSID()!=null){
int strength=WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5);
int speed=wifiInfo.getLinkSpeed();
String units=WifiInfo.LINK_SPEED_UNITS;
String ssid=wifiInfo.getSSID();
cSummary=String.format("Connected to %s at %s%s. Strength %s/5", ssid,speed,units,strength);
if(strength==1)play();
}
return cSummary;
Saturday, February 4, 2012
Android Camera Capture
Manifest File
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
PhotoCaptureExample.java
package com.sarath.camera;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class PhotoCaptureExample extends Activity
{
protected Button _button;
protected ImageView _image;
protected TextView _field;
protected String _path;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_image = ( ImageView ) findViewById( R.id.image );
_field = ( TextView ) findViewById( R.id.field );
_button = ( Button ) findViewById( R.id.button );
_button.setOnClickListener( new ButtonClickHandler() );
_path = Environment.getExternalStorageDirectory() + "/make_machine_example.jpg";
}
public class ButtonClickHandler implements View.OnClickListener
{
public void onClick( View view ){
Log.i("MakeMachine", "ButtonClickHandler.onClick()" );
startCameraActivity();
}
}
protected void startCameraActivity()
{
Log.i("MakeMachine", "startCameraActivity()" );
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.i( "MakeMachine", "resultCode: " + resultCode );
switch( resultCode )
{
case 0:
Log.i( "MakeMachine", "User cancelled" );
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken()
{
Log.i( "MakeMachine", "onPhotoTaken" );
_taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile( _path, options );
_image.setImageBitmap(bitmap);
_field.setVisibility( View.GONE );
}
@Override
protected void onRestoreInstanceState( Bundle savedInstanceState){
Log.i( "MakeMachine", "onRestoreInstanceState()");
if( savedInstanceState.getBoolean( PhotoCaptureExample.PHOTO_TAKEN ) ) {
onPhotoTaken();
}
}
@Override
protected void onSaveInstanceState( Bundle outState ) {
outState.putBoolean( PhotoCaptureExample.PHOTO_TAKEN, _taken );
}
}
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
PhotoCaptureExample.java
package com.sarath.camera;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class PhotoCaptureExample extends Activity
{
protected Button _button;
protected ImageView _image;
protected TextView _field;
protected String _path;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_image = ( ImageView ) findViewById( R.id.image );
_field = ( TextView ) findViewById( R.id.field );
_button = ( Button ) findViewById( R.id.button );
_button.setOnClickListener( new ButtonClickHandler() );
_path = Environment.getExternalStorageDirectory() + "/make_machine_example.jpg";
}
public class ButtonClickHandler implements View.OnClickListener
{
public void onClick( View view ){
Log.i("MakeMachine", "ButtonClickHandler.onClick()" );
startCameraActivity();
}
}
protected void startCameraActivity()
{
Log.i("MakeMachine", "startCameraActivity()" );
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.i( "MakeMachine", "resultCode: " + resultCode );
switch( resultCode )
{
case 0:
Log.i( "MakeMachine", "User cancelled" );
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken()
{
Log.i( "MakeMachine", "onPhotoTaken" );
_taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile( _path, options );
_image.setImageBitmap(bitmap);
_field.setVisibility( View.GONE );
}
@Override
protected void onRestoreInstanceState( Bundle savedInstanceState){
Log.i( "MakeMachine", "onRestoreInstanceState()");
if( savedInstanceState.getBoolean( PhotoCaptureExample.PHOTO_TAKEN ) ) {
onPhotoTaken();
}
}
@Override
protected void onSaveInstanceState( Bundle outState ) {
outState.putBoolean( PhotoCaptureExample.PHOTO_TAKEN, _taken );
}
}
Friday, February 3, 2012
PDF In Android
Here is an application to create a pdf in android using iText file
First download the iText jar file the iTextxtra jar is not needed and then use the following code to create a table view pdf
package com.myapp.qr;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PDFCreator {
public static String FILE = "c:/temp/FirstPdf.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.BOLD);
public static ArrayList headings = new ArrayList();
public static ArrayList contents = new ArrayList();
public static void create() {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addMetaData(document);
// addTitlePage(document);
addContent(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void addMetaData(Document document) {
document.addTitle("Bill");
}
private static void addContent(Document document) throws DocumentException {
createTable(document);
}
private static void createTable(Document subCatPart)
throws DocumentException {
PdfPTable table = new PdfPTable(3);
for (int i = 0; i < headings.size(); i++) {
String heading = headings.get(i);
PdfPCell c1 = new PdfPCell(new Phrase(heading));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
}
table.setHeaderRows(1);
for (int i = 3; i < contents.size(); i++) {
String conStr = contents.get(i);
table.addCell(conStr);
}
subCatPart.add(table);
}
}
First download the iText jar file the iTextxtra jar is not needed and then use the following code to create a table view pdf
package com.myapp.qr;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PDFCreator {
public static String FILE = "c:/temp/FirstPdf.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.BOLD);
public static ArrayList
public static ArrayList
public static void create() {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addMetaData(document);
// addTitlePage(document);
addContent(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void addMetaData(Document document) {
document.addTitle("Bill");
}
private static void addContent(Document document) throws DocumentException {
createTable(document);
}
private static void createTable(Document subCatPart)
throws DocumentException {
PdfPTable table = new PdfPTable(3);
for (int i = 0; i < headings.size(); i++) {
String heading = headings.get(i);
PdfPCell c1 = new PdfPCell(new Phrase(heading));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
}
table.setHeaderRows(1);
for (int i = 3; i < contents.size(); i++) {
String conStr = contents.get(i);
table.addCell(conStr);
}
subCatPart.add(table);
}
}
Android Wifi Analysis
Manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/mainText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
WifiTester.java
package com.kites.wifi;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class WifiTester extends Activity {
TextView mainText;
WifiManager mainWifi;
WifiReceiver receiverWifi;
List wifiList;
StringBuilder sb = new StringBuilder();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainText = (TextView) findViewById(R.id.mainText);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
mainText.setText(mainWifi+"");
WifiInfo wifiInfo = mainWifi.getConnectionInfo();
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
mainText.setText("\nStarting Scan...\n"+wifiInfo.toString());
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Refresh");
return super.onCreateOptionsMenu(menu);
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
mainWifi.startScan();
mainText.setText("Starting Scan");
return super.onMenuItemSelected(featureId, item);
}
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}
protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
sb = new StringBuilder();
wifiList = mainWifi.getScanResults();
for(int i = 0; i < wifiList.size(); i++){
sb.append(new Integer(i+1).toString() + ".");
sb.append((wifiList.get(i)).toString());
sb.append("\n");
}
mainText.setText(sb);
}
}
}
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/mainText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
WifiTester.java
package com.kites.wifi;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class WifiTester extends Activity {
TextView mainText;
WifiManager mainWifi;
WifiReceiver receiverWifi;
List
StringBuilder sb = new StringBuilder();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainText = (TextView) findViewById(R.id.mainText);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
mainText.setText(mainWifi+"");
WifiInfo wifiInfo = mainWifi.getConnectionInfo();
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
mainText.setText("\nStarting Scan...\n"+wifiInfo.toString());
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Refresh");
return super.onCreateOptionsMenu(menu);
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
mainWifi.startScan();
mainText.setText("Starting Scan");
return super.onMenuItemSelected(featureId, item);
}
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}
protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
sb = new StringBuilder();
wifiList = mainWifi.getScanResults();
for(int i = 0; i < wifiList.size(); i++){
sb.append(new Integer(i+1).toString() + ".");
sb.append((wifiList.get(i)).toString());
sb.append("\n");
}
mainText.setText(sb);
}
}
}
Subscribe to:
Posts (Atom)