· 6 years ago · Oct 30, 2019, 10:52 AM
1Experiment No.1
2
3Aim: Develop an application that uses GUI components.
4
5 Description:
6
7 1)Open eclipse or android studio and select new android project .
8 2)Give project name and select next
9 3) Choose the android version. Choose the lowest android version(Android 2.2) and select next
10 4) Enter the package name .package name must be two word separated by comma and click finish
11 5)Go to package explorer in the left hand side. select our project.
12 6)Go to res folder and select layout. Double click the main.xml file
13 7)Now you can see the Graphics layout window.
14
15Source code:
16
17<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18android:layout_width="fill_parent"
19android:layout_height="fill_parent"
20android:orientation="vertical" >
21<TextView
22android:id="@+id/textView1"
23android:layout_width="match_parent"
24android:layout_height="wrap_content"
25android:layout_margin="20sp"
26android:gravity="center"
27android:text="HELLO WORLD"
28android:textSize="20sp"
29android:textStyle="bold" />
30<Button
31android:id="@+id/button1"
32android:layout_width="match_parent"
33android:layout_height="wrap_content"
34android:gravity="center"
35android:text="Change font size"
36android:textSize="20sp" />
37<Button
38android:id="@+id/button2"
39android:layout_width="match_parent"
40android:layout_height="wrap_content"
41android:gravity="center"
42android:text="Change color"
43android:textSize="20sp" />
44<Button
45android:id="@+id/button3"
46android:layout_width="match_parent"
47android:layout_height="wrap_content"
48android:gravity="center"
49android:text="Change font"
50android:textSize="20sp" />
51</LinearLayout>
52
53
54import android.app.Activity;
55import android.graphics.Color;
56import android.graphics.Typeface;
57import android.os.Bundle;
58import android.view.View;
59import android.widget.Button;
60import android.widget.TextView;
61public class AndroidActivity extends Activity {
62float font =24;
63int i=1;
64@Override
65public void onCreate(Bundle savedInstanceState) {
66super.onCreate(savedInstanceState);
67setContentView(R.layout.main);
68final TextView t1=(TextView) findViewById(R.id.textView1);
69Button b1 = (Button) findViewById(R.id.button1);
70b1.setOnClickListener(new View.OnClickListener() {
71public void onClick(View view) {
72t1.setTextSize(font);
73font=font+4;
74if(font==40)
75font=20;
76}
77});
78Button b2 = (Button) findViewById(R.id.button2);
79b2.setOnClickListener(new View.OnClickListener() {
80public void onClick(View view) {
81switch(i)
82{
83case 1:
84t1.setTextColor(Color.parseColor("#0000FF"));
85break;
86case 2:
87t1.setTextColor(Color.parseColor("#00FF00"));
88break;
89case 3:
90t1.setTextColor(Color.parseColor("#FF0000"));
91break;
92case 4:
93t1.setTextColor(Color.parseColor("#800000"));
94break;
95}
96i++;
97if(i==5)
98i=1;
99}
100});
101}
102}
103
104////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105Experiment No.2
106
107Aim: To understand the cellular frequency reuse concept to find the co-channel cells for a particular cell.
108#include <stdio.h>
109int main()
110{
111 char str[1000], ch;
112 int i, frequency = 0;
113 printf("Enter a string: ");
114 gets(str);
115 printf("Enter a character to find the frequency: ");
116 scanf("%c",&ch);
117 for(i = 0; str[i] != '\0'; ++i)
118 {
119 if(ch == str[i])
120 ++frequency;
121 }
122 printf("Frequency of %c = %d", ch, frequency);
123 return 0;
124}
125
126////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127Experiment No.3
128
129
130Aim: Write an application that draw basic graphical primitives on the screen.
131
132
133Description:
1341. Open eclipse or android studio and select new android project
135 2. Give project name and select next
136 3. Choose the android version. Choose the lowest android version(Android 2.2) and select
137 next
1384. Enter the package name. package name must be two word separated by comma and click finish
1395. Go to package explorer in the left hand side. select our project.
1406. Go to res folder and select layout. Double click the main.xml file. Don't change anything in layout. Leave as default.
1417. Now select mainactivity.java file and type the following code.
142
143Source code:
144
145package Basic.primitive;
146import android.app.Activity;
147import android.content.Context;
148import android.graphics.Canvas;
149import android.graphics.Color;
150import android.graphics.Paint;
151import android.os.Bundle;
152import android.view.View;
153public class BasicprimitiveActivity extends Activity {
154/** Called when the activity is first created. */
155@Override
156public void onCreate(Bundle savedInstanceState) {
157super.onCreate(savedInstanceState);
158setContentView(new myview(this));
159}
160private class myview extends View
161{
162public myview(Context context)
163{
164super(context);
165}
166@Override
167protected void onDraw(Canvas canvas)
168{
169super.onDraw(canvas);
170Paint paint=new Paint();
171paint.setTextSize(40);
172paint.setColor(Color.GREEN);
173canvas.drawText("Circle", 55, 30, paint);
174paint.setColor(Color.RED);
175canvas.drawCircle(100, 150,100, paint);
176paint.setColor(Color.GREEN);
177canvas.drawText("Rectangle", 255, 30, paint);
178paint.setColor(Color.YELLOW);
179canvas.drawRect(250, 50,400,350, paint);
180paint.setColor(Color.GREEN);
181canvas.drawText("SQUARE", 55, 430, paint);
182paint.setColor(Color.BLUE);
183canvas.drawRect(50, 450,150,550, paint);
184paint.setColor(Color.GREEN);
185canvas.drawText("LINE", 255, 430, paint);
186paint.setColor(Color.CYAN);
187canvas.drawLine(250, 500, 350, 500, paint);
188}
189}
190}
191
192////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
193Experiment No.4
194
195
196Aim: Implement an application that creates an alert upon receiving a message in Android.
197
198Description:
1991)Open eclipse or android studio and select new android project
2002)Give project name and select next
2013) Choose the android version.Choose the lowest android version(Android 2.2) and select next
2024) Enter the package name.package name must be two word seprated by comma and click finish
2035)Go to package explorer in the left hand side.select our project.
2046)Go to res folder and select layout.Double click the main.xml file.Add the code
205
206Source code:
207
208<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
209android:layout_width="fill_parent"
210android:layout_height="wrap_content"
211android:scrollbars="vertical" >
212<TableLayout
213android:layout_width="match_parent"
214android:layout_height="wrap_content"
215android:shrinkColumns="*" android:stretchColumns="*"
216android:background="#000000">
217<TableRow
218android:layout_height="wrap_content"
219android:layout_width="match_parent"
220android:gravity="center_horizontal">
221<TextView
222android:id="@+id/Title"
223android:layout_width="fill_parent"
224android:layout_height="wrap_content"
225android:layout_margin="5px"
226android:focusable="false"
227android:focusableInTouchMode="false"
228android:gravity="center_vertical|center_horizontal"
229android:text="QUIZ"
230android:textSize="25sp"
231android:textStyle="bold" />
232<View
233android:layout_height="2px"
234android:layout_marginTop="5dip"
235android:layout_marginBottom="5dip"
236android:background="#DDFFDD"/>
237</TableRow>
238<TableRow
239android:layout_height="wrap_content"
240android:layout_width="match_parent"
241android:gravity="center_horizontal">
242<TextView
243android:layout_width="match_parent"
244android:layout_height="wrap_content"
245android:textSize="18sp"
246android:text="1.CAPTIAL OF INDIA"
247android:layout_span="4"
248android:padding="18dip"
249android:textColor="#ffffff"/>
250</TableRow>
251<TableRow
252android:id="@+id/tableRow1"
253android:layout_height="wrap_content"
254android:layout_width="match_parent">
255<RadioGroup
256android:id="@+id/answer1"
257android:layout_width="match_parent"
258android:layout_height="wrap_content"
259android:layout_weight="0.4" >
260<RadioButton
261android:id="@+id/answer1A"
262android:layout_width="match_parent"
263android:layout_height="wrap_content"
264android:textColor="#ffffff"
265android:text="CHENNAI" />
266<RadioButton
267android:id="@+id/answer1B"
268android:layout_width="match_parent"
269android:layout_height="wrap_content"
270android:textColor="#ffffff"
271android:text="NEW DELHI" />
272<RadioButton
273android:id="@+id/answer1C"
274android:layout_width="match_parent"
275android:layout_height="wrap_content"
276android:textColor="#ffffff"
277android:text="MUMBAI" />
278<RadioButton
279android:id="@+id/answer1D"
280android:layout_width="match_parent"
281android:layout_height="wrap_content"
282android:textColor="#ffffff"
283android:text="HYDERBAD" />
284</RadioGroup>
285</TableRow>
286<TableRow
287android:layout_height="wrap_content"
288android:layout_width="match_parent"
289android:gravity="center_horizontal">
290<TextView
291android:layout_width="match_parent" android:layout_height="wrap_content"
292android:textSize="18sp"
293android:text="2. CAPTIAL OF RUSSIA?" android:layout_span="4"
294android:padding="18dip"
295android:textColor="#ffffff"/>
296</TableRow>
297<TableRow
298android:id="@+id/tableRow2"
299android:layout_height="wrap_content"
300android:layout_width="match_parent">
301<RadioGroup
302android:id="@+id/answer2"
303android:layout_width="match_parent"
304android:layout_height="wrap_content"
305android:layout_weight="0.4" >
306<RadioButton
307android:id="@+id/answer2A"
308android:layout_width="match_parent"
309android:layout_height="wrap_content"
310android:textColor="#ffffff"
311android:text="WARSAW " />
312<RadioButton
313android:id="@+id/answer2B"
314android:layout_width="match_parent"
315android:layout_height="wrap_content"
316android:textColor="#ffffff"
317android:text="BERLIN" />
318<RadioButton
319android:id="@+id/answer2C"
320android:layout_width="match_parent"
321android:layout_height="wrap_content"
322android:textColor="#ffffff"
323android:text="MASCOW " />
324<RadioButton
325android:id="@+id/answer2D"
326android:layout_width="match_parent"
327android:layout_height="wrap_content"
328android:textColor="#ffffff"
329android:text="CANEBRA " />
330</RadioGroup>
331</TableRow>
332<TableRow
333<Button
334android:id="@+id/submit"
335android:layout_width="wrap_content"
336android:layout_height="wrap_content"
337android:gravity="center"
338android:text="Submit" />
339</TableRow>
340</TableLayout>
341</ScrollView>
342
343////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
344Experiment No.5
345
346Aim: Develop an application that makes use of database.
347
348Description:
349
3501)Open eclipse or android studio and select new android project
3512)Give project name and select next
3523) Choose the android version. Choose the lowest android version(Android 2.2) and select next
3534) Enter the package name. package name must be two word separated by comma and click finish
3545)Go to package explorer in the left hand side. select our project.
3556)Go to res folder and select layout. Double click the main.xml file.
356
357Source code :
358<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
359android:id="@+id/myLayout"
360android:stretchColumns="0"
361android:layout_width="fill_parent"
362android:layout_height="fill_parent">
363<TextView android:text="@string/title"
364android:layout_x="110dp"
365android:layout_y="10dp"
366android:layout_width="wrap_content"
367android:layout_height="wrap_content"/>
368<TextView android:text="@string/empid"
369android:layout_x="30dp"
370android:layout_y="50dp"
371android:layout_width="wrap_content"
372android:layout_height="wrap_content"/>
373<EditText android:id="@+id/editEmpid"
374android:inputType="number"
375android:layout_x="150dp"
376android:layout_y="50dp"
377android:layout_width="150dp"
378android:layout_height="40dp"/>
379<TextView android:text="@string/name"
380android:layout_x="30dp"
381android:layout_y="100dp"
382android:layout_width="wrap_content"
383android:layout_height="wrap_content"/>
384<EditText android:id="@+id/editName"
385android:inputType="text"
386android:layout_x="150dp"
387android:layout_y="100dp"
388android:layout_width="150dp"
389android:layout_height="40dp"/>
390<TextView android:text="@string/salary"
391android:layout_x="30dp"
392android:layout_y="150dp"
393android:layout_width="wrap_content"
394android:layout_height="wrap_content"/>
395<EditText android:id="@+id/editsalary"
396android:inputType="number"
397android:layout_x="150dp"
398android:layout_y="150dp"
399android:layout_width="150dp"
400android:layout_height="40dp"/>
401<Button android:id="@+id/btnAdd"
402android:text="@string/add"
403android:layout_x="30dp"
404android:layout_y="200dp"
405android:layout_width="130dp"
406android:layout_height="40dp"/>
407<Button android:id="@+id/btnDelete"
408android:text="@string/delete"
409android:layout_x="160dp"
410android:layout_y="200dp"
411android:layout_width="130dp"
412android:layout_height="40dp"/>n
413<Button android:id="@+id/btnModify"
414android:text="@string/modify"
415android:layout_x="30dp"
416android:layout_y="250dp"
417android:layout_width="130dp"
418android:layout_height="40dp"/>
419<Button android:id="@+id/btnView"
420android:text="@string/view"
421android:layout_x="160dp"
422android:layout_y="250dp"
423android:layout_width="130dp"
424android:layout_height="40dp"/>
425<Button android:id="@+id/btnViewAll"
426android:text="@string/view_all"
427android:layout_x="85dp"
428android:layout_y="300dp"
429android:layout_width="150dp"
430android:layout_height="40dp"/>
431</AbsoluteLayout>
432
433
434Go to values folder and select string.xml file.Replace the code below
435<?xml version="1.0" encoding="utf-8"?>
436<resources>
437<string name="app_name">Employee detail1</string>
438<string name="hello">Hello World, Employee detail Activity!</string>
439<string name="title">Employee Details</string>
440<string name="empid">Enter Employee ID: </string>
441<string name="name">Enter Name: </string>
442<string name="salary">Enter salary: </string>
443<string name="add">Add Employee</string>
444<string name="delete">Delete Employee</string>
445<string name="modify">Modify Employee</string>
446<string name="view">View Employee</string>
447<string name="view_all">View All Employee</string>
448</resources>
4498) Now select mainactivity.java file and type the following code.In my coding maniactivity name
450is EmployeedetailActivity.
451package employee.detail;
452//import android.R;
453import android.app.Activity;
454import android.app.AlertDialog.Builder;
455import android.content.Context;
456import android.database.Cursor;
457import android.database.sqlite.SQLiteDatabase;
458import android.os.Bundle;
459import android.view.View;
460import android.view.View.OnClickListener;
461import android.widget.Button;
462import android.widget.EditText;
463public class EmployeedetailActivity extends Activity implements OnClickListener {
464EditText editEmpid,editName,editsalary;
465Button btnAdd,btnDelete,btnModify,btnView,btnViewAll;
466SQLiteDatabase db;
467/** Called when the activity is first created. */
468@Override
469public void onCreate(Bundle savedInstanceState)
470{
471super.onCreate(savedInstanceState);
472setContentView(R.layout.main);
473editEmpid=(EditText)findViewById(R.id.editEmpid);
474editName=(EditText)findViewById(R.id.editName);
475editsalary=(EditText)findViewById(R.id.editsalary);
476btnAdd=(Button)findViewById(R.id.btnAdd);
477btnDelete=(Button)findViewById(R.id.btnDelete);
478btnModify=(Button)findViewById(R.id.btnModify);
479btnView=(Button)findViewById(R.id.btnView);
480btnViewAll=(Button)findViewById(R.id.btnViewAll);
481btnAdd.setOnClickListener(this);
482btnDelete.setOnClickListener(this);
483btnModify.setOnClickListener(this);
484btnView.setOnClickListener(this);
485btnViewAll.setOnClickListener(this);
486db=openOrCreateDatabase("EmployeeDB", Context.MODE_PRIVATE, null);
487db.execSQL("CREATE TABLE IF NOT EXISTS employee(empid VARCHAR,name
488VARCHAR,salary VARCHAR);");
489}
490public void onClick(View view)
491{
492if(view==btnAdd)
493{
494if(editEmpid.getText().toString().trim().length()==0||
495editName.getText().toString().trim().length()==0||
496editsalary.getText().toString().trim().length()==0)
497{
498showMessage("Error", "Please enter all values");
499return;
500}
501db.execSQL("INSERT INTO employee
502VALUES('"+editEmpid.getText()+"','"+editName.getText()+
503"','"+editsalary.getText()+"');");
504showMessage("Success", "Record added");
505clearText();
506}
507if(view==btnDelete)
508{
509if(editEmpid.getText().toString().trim().length()==0)
510{
511showMessage("Error", "Please enter Employee id");
512return;
513}
514Cursor c=db.rawQuery("SELECT * FROM employee WHERE
515empid='"+editEmpid.getText()+"'", null);
516if(c.moveToFirst())
517{
518db.execSQL("DELETE FROM employee WHERE
519empid='"+editEmpid.getText()+"'");
520showMessage("Success", "Record Deleted");
521}
522else
523{
524showMessage("Error", "Invalid Employee id");
525}
526clearText();
527}
528if(view==btnModify)
529{
530if(editEmpid.getText().toString().trim().length()==0)
531{
532showMessage("Error", "Please enter Employee id");
533return;
534}
535Cursor c=db.rawQuery("SELECT * FROM employee WHERE
536empid='"+editEmpid.getText()+"'", null);
537if(c.moveToFirst())
538{
539db.execSQL("UPDATE employee SET
540name='"+editName.getText()+"',salary='"+editsalary.getText()+
541"' WHERE empid='"+editEmpid.getText()+"'");
542showMessage("Success", "Record Modified");
543}
544else
545{
546showMessage("Error", "Invalid Rollno");
547}
548clearText();
549}
550if(view==btnView)
551{
552if(editEmpid.getText().toString().trim().length()==0)
553{
554showMessage("Error", "Please enter Employee id");
555return;
556}
557Cursor c=db.rawQuery("SELECT * FROM employee WHERE
558empid='"+editEmpid.getText()+"'", null);
559if(c.moveToFirst())
560{
561editName.setText(c.getString(1));
562editsalary.setText(c.getString(2));
563}
564else
565{
566showMessage("Error", "Invalid Employee id");
567clearText();
568}
569}
570if(view==btnViewAll)
571{
572Cursor c=db.rawQuery("SELECT * FROM employee", null);
573if(c.getCount()==0)
574{
575showMessage("Error", "No records found");
576return;
577}
578StringBuffer buffer=new StringBuffer();
579while(c.moveToNext())
580{
581buffer.append("Employee id: "+c.getString(0)+"\n");
582buffer.append("Name: "+c.getString(1)+"\n");
583buffer.append("salary: "+c.getString(2)+"\n\n");
584}
585showMessage("Employee details Details", buffer.toString());
586}
587}
588public void showMessage(String title,String message)
589{
590Builder builder=new Builder(this);
591builder.setCancelable(true);
592builder.setTitle(title);
593builder.setMessage(message);
594builder.show();
595}
596public void clearText()
597{
598editEmpid.setText("");
599editName.setText("");
600editsalary.setText("");
601editEmpid.requestFocus();
602}
603}
604
605
606////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
607Experiment No.6
608
609
610Aim: Develop a native application that uses GPS location information.
611
612Description:
613
6141)Open eclipse or android studio and select new android project
6152)Give project name and select next
6163) Choose the android version. Choose the lowest android version(Android 2.2) and select next
6174) Enter the package name. package name must be two word separated by comma and click finish
6185)Go to package explorer in the left hand side. select our project.
6196)Go to res folder and select layout. Double click the main.xml file. Add the code
620
621Source code:
622
623<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
624android:id="@+id/relativeLayout1"
625android:layout_width="match_parent"
626android:layout_height="match_parent" >
627<Button
628android:id="@+id/show_Location"
629android:layout_width="wrap_content"
630android:layout_height="wrap_content
631android:text="Show_Location"
632android:layout_centerVertical="true"
633android:layout_centerHorizontal="true"
634/>
635</RelativeLayout>
636
637
638package gps.location;
639//import android.R;
640import android.app.Activity;
641import android.os.Bundle;
642import android.view.View;
643import android.widget.Button;
644import android.widget.Toast;
645public class GPSlocationActivity extends Activity {
646/** Called when the activity is first created. */
647Button btnShowLocation;
648GPStrace gps;
649@Override
650public void onCreate(Bundle savedInstanceState) {
651super.onCreate(savedInstanceState);
652setContentView(R.layout.main);
653btnShowLocation=(Button)findViewById(R.id.show_Location);
654btnShowLocation.setOnClickListener(new View.OnClickListener() {
655@Override
656public void onClick(View v) {
657// TODO Auto-generated method stub
658gps=new GPStrace(GPSlocationActivity.this);
659if(gps.canGetLocation()){
660double latitude=gps.getLatitude();
661double longitude=gps.getLongtiude();
662Toast.makeText(getApplicationContext(),"Your Location is
663\nLat:"+latitude+"\nLong:"+longitude, Toast.LENGTH_LONG).show();
664}
665else
666{
667gps.showSettingAlert();
668}
669} }); } }
670
671
672)Go to src folder and Right Click on your package folder and choose new class and give the
673class nams as GPStrace
674
675
676
6779)Select the GPStrace.java file and paste the following code.
678package gps.location;
679import android.app.AlertDialog;
680import android.app.Service;
681import android.content.Context;
682import android.content.DialogInterface;
683import android.content.Intent;
684import android.location.Location;
685import android.location.LocationListener;
686import android.location.LocationManager;
687import android.os.Bundle;
688import android.os.IBinder;
689import android.provider.Settings;
690public class GPStrace extends Service implements LocationListener{
691private final Context context;
692boolean isGPSEnabled=false;
693boolean canGetLocation=false;
694boolean isNetworkEnabled=false;
695Location location;
696double latitude;
697double longtitude;
698private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10;
699private static final long MIN_TIME_BW_UPDATES=1000*60*1;
700protected LocationManager locationManager;
701public GPStrace(Context context)
702{
703this.context=context;
704getLocation();
705}
706public Location getLocation()
707{
708try{
709locationManager=(LocationManager) context.getSystemService(LOCATION_SERVICE);
710isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
711isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVI
712DER);
713if(!isGPSEnabled && !isNetworkEnabled){
714}else{
715this.canGetLocation=true;
716if(isNetworkEnabled){
717locationManager.requestLocationUpdates(
718LocationManager.NETWORK_PROVIDER,
719MIN_TIME_BW_UPDATES,
720MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
721}
722if(locationManager!=null){
723location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
724;
725if(location !=null){
726latitude=location.getLatitude();
727longtitude=location.getLongitude();
728}
729}
730}
731if(isGPSEnabled){
732if(location==null){
733locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_B
734W_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
735if(locationManager!=null){
736location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
737if(location!=null){
738latitude=location.getLatitude();
739longtitude=location.getLongitude();
740}
741}
742}
743}
744}
745catch(Exception e)
746{
747e.printStackTrace();
748}
749return location;
750}
751public void stopUsingGPS(){
752if(locationManager!=null){
753locationManager.removeUpdates(GPStrace.this);
754}
755}
756public double getLatitude(){
757if(location!=null){
758latitude=location.getLatitude();
759}
760return latitude;
761}
762public double getLongtiude(){
763if(location!=null){
764longtitude=location.getLatitude();
765}
766return longtitude;
767}
768public boolean canGetLocation(){
769return this.canGetLocation;
770}
771public void showSettingAlert(){
772AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
773alertDialog.setTitle("GPS is settings");
774alertDialog.setMessage("GPS is not enabled.Do you want to go to setting menu?");
775alertDialog.setPositiveButton("settings", new DialogInterface.OnClickListener() {
776@Override
777public void onClick(DialogInterface dialog,int which){
778Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
779context.startActivity(intent);
780}
781});
782alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
783@Override
784public void onClick(DialogInterface dialog, int which) {
785// TODO Auto-generated method stub
786dialog.cancel();
787}
788});
789alertDialog.show();
790}
791@Override
792public void onLocationChanged(Location location) {
793// TODO Auto-generated method stub
794}
795@Override
796public void onProviderDisabled(String provider) {
797// TODO Auto-generated method stub
798}
799@Override
800public void onProviderEnabled(String provider) {
801// TODO Auto-generated method stub
802}
803@Override
804public void onStatusChanged(String provider, int status, Bundle extras) {
805// TODO Auto-generated method stub
806}
807@Override
808public IBinder onBind(Intent intent) {
809// TODO Auto-generated method stub
810return null;
811}
812}
81310)Go to manifest.xml file and add the code below
814<uses-permission
815android:name="android.permission.ACCESS_FINE_LOCATION"/>
816<uses-permission
817android:name="android.permission.INTERNET"/>
818• Now go to main.xml and right click .select run as option and select run configuration
819
820
821////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
822Experiment No.7
823
824Aim: Implement an application that writes data to the SD card.
825
826Description:
8271)Open eclipse or android studio and select new android project
8282)Give project name and select next
8293) Choose the android version. Choose the lowest android version(Android 2.2) and select next
8304) Enter the package name. package name must be two word separated by comma and click finish
8315)Go to package explorer in the left hand side. select our project.
8326)Go to res folder and select layout. Double click the main.xml file. Add the code .
833
834Source code:
835
836
837<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
838android:layout_width="fill_parent"
839android:layout_height="fill_parent"
840android:background="#ff0000ff"
841android:orientation="vertical" >
842<EditText
843android:id="@+id/editText1"
844android:layout_width="match_parent"
845android:layout_height="wrap_content" >
846<requestFocus />
847</EditText>
848<Button
849android:id="@+id/button1"
850android:layout_width="match_parent"
851android:layout_height="wrap_content"
852android:text="SAVE DATA" />
853<Button
854android:id="@+id/button2"
855android:layout_width="match_parent"
856android:layout_height="wrap_content"
857android:text="SHOW DATA" />
858<TextView
859android:id="@+id/textView1"
860android:layout_width="wrap_content"
861android:layout_height="wrap_content"
862/>
863</LinearLayout>
8647) Now select mainactivity.java file and type the following code.
865package save.sd;
866import java.io.File;
867import java.io.FileInputStream;
868import java.io.FileNotFoundException;
869import java.io.FileOutputStream;
870import java.io.IOException;
871import java.io.InputStreamReader;
872import java.io.OutputStreamWriter;
873import android.app.Activity;
874import android.os.Bundle;
875import android.os.Environment;
876import android.view.View;
877import android.widget.Button;
878import android.widget.EditText;
879import android.widget.TextView;
880import android.widget.Toast;
881public class SavedatasdcardActivity extends Activity {
882/** Called when the activity is first created. */
883Button save,load;
884EditText message;
885TextView t1;
886String Message1;
887@Override
888public void onCreate(Bundle savedInstanceState) {
889super.onCreate(savedInstanceState);
890setContentView(R.layout.main);
891save=(Button) findViewById(R.id.button1);
892load=(Button) findViewById(R.id.button2);
893message=(EditText) findViewById(R.id.editText1);
894t1=(TextView) findViewById(R.id.textView1);
895save.setOnClickListener(new View.OnClickListener(){
896public void onClick(View v){
897//Get message from user store in message1 variable
898Message1 =message.getText().toString();
899try{
900//Create a new folder called MyDirectory in SDCard
901File sdcard=Environment.getExternalStorageDirectory();
902File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");
903directory.mkdirs();
904//Create a new file name textfile.txt inside MyDirectory
905File file=new File(directory,"textfile.txt");
906//Create File Outputstream to read the file
907FileOutputStream fou=new FileOutputStream(file);
908OutputStreamWriter osw=new OutputStreamWriter(fou);
909try{
910//write a user data to file
911osw.append(Message1);
912osw.flush();
913osw.close();
914Toast.makeText(getBaseContext(),"Data
915Saved",Toast.LENGTH_LONG).show();
916}catch(IOException e){
917e.printStackTrace();
918}
919}catch (FileNotFoundException e){
920e.printStackTrace();
921}
922}
923});
924load.setOnClickListener(new View.OnClickListener(){
925public void onClick(View v){
926try{
927File sdcard=Environment.getExternalStorageDirectory();
928File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");
929File file=new File(directory,"textfile.txt");
930FileInputStream fis=new FileInputStream(file);
931InputStreamReader isr=new InputStreamReader(fis);
932char[] data=new char[100];
933String final_data="";
934int size;
935try{
936while((size=isr.read(data))>0)
937{
938//read a data from file
939String read_data=String.copyValueOf(data,0,size);
940final_data+=read_data;
941data=new char[100];
942}
943//display the data in output
944Toast.makeText(getBaseContext(),"Message:"+final_data,Toast.LENGTH_LONG).show();
945}catch(IOException e){
946e.printStackTrace();
947}
948}catch (FileNotFoundException e){
949e.printStackTrace();
950}
951}
952}); } }
953
954
955////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
956Experiment No.8
957
958
959Aim: Implementation of GSM security algorithms(A3/A5/A8).
960
961Source Code:
962
963import java.util.Arrays;
964
965
966public class Comp128 {
967
968 private static final int[] comp128v1_t0 = {
969 102, 177, 186, 162, 2, 156, 112, 75, 55, 25, 8, 12, 251, 193, 246, 188,
970 109, 213, 151, 53, 42, 79, 191, 115, 233, 242, 164, 223, 209, 148, 108, 161,
971 252, 37, 244, 47, 64, 211, 6, 237, 185, 160, 139, 113, 76, 138, 59, 70,
972 67, 26, 13, 157, 63, 179, 221, 30, 214, 36, 166, 69, 152, 124, 207, 116,
973 247, 194, 41, 84, 71, 1, 49, 14, 95, 35, 169, 21, 96, 78, 215, 225,
974 182, 243, 28, 92, 201, 118, 4, 74, 248, 128, 17, 11, 146, 132, 245, 48,
975 149, 90, 120, 39, 87, 230, 106, 232, 175, 19, 126, 190, 202, 141, 137, 176,
976 250, 27, 101, 40, 219, 227, 58, 20, 51, 178, 98, 216, 140, 22, 32, 121,
977 61, 103, 203, 72, 29, 110, 85, 212, 180, 204, 150, 183, 15, 66, 172, 196,
978 56, 197, 158, 0, 100, 45, 153, 7, 144, 222, 163, 167, 60, 135, 210, 231,
979 174, 165, 38, 249, 224, 34, 220, 229, 217, 208, 241, 68, 206, 189, 125, 255,
980 239, 54, 168, 89, 123, 122, 73, 145, 117, 234, 143, 99, 129, 200, 192, 82,
981 104, 170, 136, 235, 93, 81, 205, 173, 236, 94, 105, 52, 46, 228, 198, 5,
982 57, 254, 97, 155, 142, 133, 199, 171, 187, 50, 65, 181, 127, 107, 147, 226,
983 184, 218, 131, 33, 77, 86, 31, 44, 88, 62, 238, 18, 24, 43, 154, 23,
984 80, 159, 134, 111, 9, 114, 3, 91, 16, 130, 83, 10, 195, 240, 253, 119,
985 177, 102, 162, 186, 156, 2, 75, 112, 25, 55, 12, 8, 193, 251, 188, 246,
986 213, 109, 53, 151, 79, 42, 115, 191, 242, 233, 223, 164, 148, 209, 161, 108,
987 37, 252, 47, 244, 211, 64, 237, 6, 160, 185, 113, 139, 138, 76, 70, 59,
988 26, 67, 157, 13, 179, 63, 30, 221, 36, 214, 69, 166, 124, 152, 116, 207,
989 194, 247, 84, 41, 1, 71, 14, 49, 35, 95, 21, 169, 78, 96, 225, 215,
990 243, 182, 92, 28, 118, 201, 74, 4, 128, 248, 11, 17, 132, 146, 48, 245,
991 90, 149, 39, 120, 230, 87, 232, 106, 19, 175, 190, 126, 141, 202, 176, 137,
992 27, 250, 40, 101, 227, 219, 20, 58, 178, 51, 216, 98, 22, 140, 121, 32,
993 103, 61, 72, 203, 110, 29, 212, 85, 204, 180, 183, 150, 66, 15, 196, 172,
994 197, 56, 0, 158, 45, 100, 7, 153, 222, 144, 167, 163, 135, 60, 231, 210,
995 165, 174, 249, 38, 34, 224, 229, 220, 208, 217, 68, 241, 189, 206, 255, 125,
996 54, 239, 89, 168, 122, 123, 145, 73, 234, 117, 99, 143, 200, 129, 82, 192,
997 170, 104, 235, 136, 81, 93, 173, 205, 94, 236, 52, 105, 228, 46, 5, 198,
998 254, 57, 155, 97, 133, 142, 171, 199, 50, 187, 181, 65, 107, 127, 226, 147,
999 218, 184, 33, 131, 86, 77, 44, 31, 62, 88, 18, 238, 43, 24, 23, 154,
1000 159, 80, 111, 134, 114, 9, 91, 3, 130, 16, 10, 83, 240, 195, 119, 253 };
1001
1002 /* 256 bytes */
1003 private static final int[] comp128v1_t1 = {
1004 19, 11, 80, 114, 43, 1, 69, 94, 39, 18, 127, 117, 97, 3, 85, 43,
1005 27, 124, 70, 83, 47, 71, 63, 10, 47, 89, 79, 4, 14, 59, 11, 5,
1006 35, 107, 103, 68, 21, 86, 36, 91, 85, 126, 32, 50, 109, 94, 120, 6,
1007 53, 79, 28, 45, 99, 95, 41, 34, 88, 68, 93, 55, 110, 125, 105, 20,
1008 90, 80, 76, 96, 23, 60, 89, 64, 121, 56, 14, 74, 101, 8, 19, 78,
1009 76, 66, 104, 46, 111, 50, 32, 3, 39, 0, 58, 25, 92, 22, 18, 51,
1010 57, 65, 119, 116, 22, 109, 7, 86, 59, 93, 62, 110, 78, 99, 77, 67,
1011 12, 113, 87, 98, 102, 5, 88, 33, 38, 56, 23, 8, 75, 45, 13, 75,
1012 95, 63, 28, 49, 123, 120, 20, 112, 44, 30, 15, 98, 106, 2, 103, 29,
1013 82, 107, 42, 124, 24, 30, 41, 16, 108, 100, 117, 40, 73, 40, 7, 114,
1014 82, 115, 36, 112, 12, 102, 100, 84, 92, 48, 72, 97, 9, 54, 55, 74,
1015 113, 123, 17, 26, 53, 58, 4, 9, 69, 122, 21, 118, 42, 60, 27, 73,
1016 118, 125, 34, 15, 65, 115, 84, 64, 62, 81, 70, 1, 24, 111, 121, 83,
1017 104, 81, 49, 127, 48, 105, 31, 10, 6, 91, 87, 37, 16, 54, 116, 126,
1018 31, 38, 13, 0, 72, 106, 77, 61, 26, 67, 46, 29, 96, 37, 61, 52,
1019 101, 17, 44, 108, 71, 52, 66, 57, 33, 51, 25, 90, 2, 119, 122, 35 };
1020
1021
1022 /* 128 bytes */
1023 private static final int[] comp128v1_t2 = {
1024 52, 50, 44, 6, 21, 49, 41, 59, 39, 51, 25, 32, 51, 47, 52, 43,
1025 37, 4, 40, 34, 61, 12, 28, 4, 58, 23, 8, 15, 12, 22, 9, 18,
1026 55, 10, 33, 35, 50, 1, 43, 3, 57, 13, 62, 14, 7, 42, 44, 59,
1027 62, 57, 27, 6, 8, 31, 26, 54, 41, 22, 45, 20, 39, 3, 16, 56,
1028 48, 2, 21, 28, 36, 42, 60, 33, 34, 18, 0, 11, 24, 10, 17, 61,
1029 29, 14, 45, 26, 55, 46, 11, 17, 54, 46, 9, 24, 30, 60, 32, 0,
1030 20, 38, 2, 30, 58, 35, 1, 16, 56, 40, 23, 48, 13, 19, 19, 27,
1031 31, 53, 47, 38, 63, 15, 49, 5, 37, 53, 25, 36, 63, 29, 5, 7 };
1032
1033 /* 64 bytes */
1034 private static final int[] comp128v1_t3 = {
1035 1, 5, 29, 6, 25, 1, 18, 23, 17, 19, 0, 9, 24, 25, 6, 31,
1036 28, 20, 24, 30, 4, 27, 3, 13, 15, 16, 14, 18, 4, 3, 8, 9,
1037 20, 0, 12, 26, 21, 8, 28, 2, 29, 2, 15, 7, 11, 22, 14, 10,
1038 17, 21, 12, 30, 26, 27, 16, 31, 11, 7, 13, 23, 10, 5, 22, 19 };
1039
1040 /* 32 bytes */
1041 private static final int[] comp128v1_t4 = {
1042 15, 12, 10, 4, 1, 14, 11, 7, 5, 0, 14, 7, 1, 2, 13, 8,
1043 10, 3, 4, 9, 6, 0, 3, 2, 5, 6, 8, 9, 11, 13, 15, 12 };
1044
1045 private static final int[][] comp128_table = { comp128v1_t0, comp128v1_t1, comp128v1_t2, comp128v1_t3, comp128v1_t4 };
1046
1047 /* 256 bytes */
1048 private static final int[] comp128v23_t0 = {
1049 197, 235, 60, 151, 98, 96, 3, 100, 248, 118, 42, 117, 172, 211, 181, 203,
1050 61, 126, 156, 87, 149, 224, 55, 132, 186, 63, 238, 255, 85, 83, 152, 33,
1051 160, 184, 210, 219, 159, 11, 180, 194, 130, 212, 147, 5, 215, 92, 27, 46,
1052 113, 187, 52, 25, 185, 79, 221, 48, 70, 31, 101, 15, 195, 201, 50, 222,
1053 137, 233, 229, 106, 122, 183, 178, 177, 144, 207, 234, 182, 37, 254, 227, 231,
1054 54, 209, 133, 65, 202, 69, 237, 220, 189, 146, 120, 68, 21, 125, 38, 30,
1055 2, 155, 53, 196, 174, 176, 51, 246, 167, 76, 110, 20, 82, 121, 103, 112,
1056 56, 173, 49, 217, 252, 0, 114, 228, 123, 12, 93, 161, 253, 232, 240, 175,
1057 67, 128, 22, 158, 89, 18, 77, 109, 190, 17, 62, 4, 153, 163, 59, 145,
1058 138, 7, 74, 205, 10, 162, 80, 45, 104, 111, 150, 214, 154, 28, 191, 169,
1059 213, 88, 193, 198, 200, 245, 39, 164, 124, 84, 78, 1, 188, 170, 23, 86,
1060 226, 141, 32, 6, 131, 127, 199, 40, 135, 16, 57, 71, 91, 225, 168, 242,
1061 206, 97, 166, 44, 14, 90, 236, 239, 230, 244, 223, 108, 102, 119, 148, 251,
1062 29, 216, 8, 9, 249, 208, 24, 105, 94, 34, 64, 95, 115, 72, 134, 204,
1063 43, 247, 243, 218, 47, 58, 73, 107, 241, 179, 116, 66, 36, 143, 81, 250,
1064 139, 19, 13, 142, 140, 129, 192, 99, 171, 157, 136, 41, 75, 35, 165, 26
1065 };
1066
1067 /* 256 bytes */
1068 private static final int[] comp128v23_t1 = {
1069 170, 42, 95, 141, 109, 30, 71, 89, 26, 147, 231, 205, 239, 212, 124, 129,
1070 216, 79, 15, 185, 153, 14, 251, 162, 0, 241, 172, 197, 43, 10, 194, 235,
1071 6, 20, 72, 45, 143, 104, 161, 119, 41, 136, 38, 189, 135, 25, 93, 18,
1072 224, 171, 252, 195, 63, 19, 58, 165, 23, 55, 133, 254, 214, 144, 220, 178,
1073 156, 52, 110, 225, 97, 183, 140, 39, 53, 88, 219, 167, 16, 198, 62, 222,
1074 76, 139, 175, 94, 51, 134, 115, 22, 67, 1, 249, 217, 3, 5, 232, 138,
1075 31, 56, 116, 163, 70, 128, 234, 132, 229, 184, 244, 13, 34, 73, 233, 154,
1076 179, 131, 215, 236, 142, 223, 27, 57, 246, 108, 211, 8, 253, 85, 66, 245,
1077 193, 78, 190, 4, 17, 7, 150, 127, 152, 213, 37, 186, 2, 243, 46, 169,
1078 68, 101, 60, 174, 208, 158, 176, 69, 238, 191, 90, 83, 166, 125, 77, 59,
1079 21, 92, 49, 151, 168, 99, 9, 50, 146, 113, 117, 228, 65, 230, 40, 82,
1080 54, 237, 227, 102, 28, 36, 107, 24, 44, 126, 206, 201, 61, 114, 164, 207,
1081 181, 29, 91, 64, 221, 255, 48, 155, 192, 111, 180, 210, 182, 247, 203, 148,
1082 209, 98, 173, 11, 75, 123, 250, 118, 32, 47, 240, 202, 74, 177, 100, 80,
1083 196, 33, 248, 86, 157, 137, 120, 130, 84, 204, 122, 81, 242, 188, 200, 149,
1084 226, 218, 160, 187, 106, 35, 87, 105, 96, 145, 199, 159, 12, 121, 103, 112
1085 };
1086
1087 public static void _comp128_compression_round(final int[] x, final int n, final int[] tbl) {
1088 final int m = 4 - n;
1089 for (int i = 0; i < (1 << n); i++) {
1090 for (int j = 0; j < (1 << m); j++) {
1091 final int a = j + i * (2 << m);
1092 final int b = a + (1 << m);
1093 final int y = (x[a] + (x[b] << 1)) & ((32 << m) - 1);
1094 final int z = ((x[a] << 1) + x[b]) & ((32 << m) - 1);
1095 x[a] = tbl[y];
1096 x[b] = tbl[z];
1097 }
1098 }
1099 }
1100
1101 static void _comp128_compression(final int[] x) {
1102 for (int n = 0; n < 5; n++) {
1103 _comp128_compression_round(x, n, comp128_table[n]);
1104 }
1105 }
1106
1107 static void _comp128_bitsfrombytes(final int[] x, final int[] bits) {
1108 Arrays.fill(bits, 0, 128, 0);
1109 for (int i = 0; i < 128; i++) {
1110 if ((x[i >> 2] & (1 << (3 - (i & 3)))) != 0) {
1111 bits[i] = 1;
1112 }
1113 }
1114 }
1115
1116 static void _comp128_permutation(final int[] x, final int[] bits) {
1117 Arrays.fill(x, 16, 32, 0);
1118 for (int i = 0; i < 128; i++) {
1119 x[(i >> 3) + 16] |= bits[(i * 17) & 127] << (7 - (i & 7));
1120 }
1121 }
1122
1123 static void _comp128v23(final int[] rand, final int[] kxor) {
1124 final int[] temp = new int[16];
1125 final int[] km_rm = new int[32];
1126
1127 // Java bytes are initialised at 0, so redundant
1128 // Arrays.fill(temp, 0);
1129 System.arraycopy(rand, 0, km_rm, 0, 16);
1130 System.arraycopy(kxor, 0, km_rm, 16, 16);
1131 Arrays.fill(rand, 0, 16, 0);
1132
1133 for (int i = 0; i < 5; i++) {
1134 int j = 0;
1135
1136 for (int z = 0; z < 16; z++) {
1137 temp[z] = comp128v23_t0[
1138 comp128v23_t1[
1139 km_rm[16 + z]
1140 ] ^ km_rm[z]
1141 ] & 0xff;
1142 }
1143
1144 while ((1 << i) > j) {
1145 int k = 0;
1146 while ((1 << (4 - i)) > k) {
1147 km_rm[(((2 * k) + 1) << i) + j] =
1148 comp128v23_t0[comp128v23_t1[temp[(k << i) + j]] ^ (km_rm[(k << i) + 16 + j])];
1149 km_rm[(k << (i + 1)) + j] = temp[(k << i) + j];
1150 k++;
1151 }
1152 j++;
1153 }
1154 }
1155
1156 for (int i = 0; i < 16; i++) {
1157 for (int j = 0; j < 8; j++) {
1158 rand[i] = ((rand[i] ^ (((km_rm[(19 * (j + 8 * i) + 19) % 256 / 8] >> (3 * j + 3) % 8) & 1) << j)) & 0xff);
1159 }
1160 }
1161 }
1162
1163 /**
1164 * Calculate comp128v2 or comp128v3 sres and kc from ki and rand
1165 */
1166 private static byte[] comp128v23(final byte[] ki, final byte[] rand, final boolean v2) {
1167 validateInput(ki, rand);
1168 final int[] k_mix = new int[16];
1169 final int[] rand_mix = new int[16];
1170 final int[] katyvasz = new int[16];
1171 final int[] buffer = new int[16];
1172
1173 /* Every day IM suffling... */
1174 for (int i = 0; i < 8; i++) {
1175 k_mix[i] = ki[15 - i] & 0xff;
1176 k_mix[15 - i] = ki[i] & 0xff;
1177 }
1178
1179 for (int i = 0; i < 8; i++) {
1180 rand_mix[i] = rand[15 - i] & 0xff;
1181 rand_mix[15 - i] = rand[i] & 0xff;
1182 }
1183
1184 for (int i = 0; i < 16; i++) {
1185 katyvasz[i] = k_mix[i] ^ rand_mix[i];
1186 }
1187
1188 for (int i = 0; i < 8; i++) {
1189 _comp128v23(rand_mix, katyvasz);
1190 }
1191
1192 for (int i = 0; i < 16; i++) {
1193 buffer[i] = rand_mix[15 - i];
1194 }
1195
1196 if (v2) {
1197 buffer[15] = 0;
1198 buffer[14] = 4 * (buffer[14] >> 2);
1199 }
1200
1201 for (int i = 0; i < 4; i++) {
1202 buffer[8 + i - 4] = buffer[8 + i];
1203 buffer[8 + i] = buffer[8 + i + 4];
1204 }
1205
1206 /*
1207 * The algorithm uses 16 bytes until this point, but only 12 bytes are effective
1208 * also 12 bytes coming out from the SIM card.
1209 */
1210 final byte[] sresKc = new byte[12];
1211 for (int i = 0; i < 12; i++) {
1212 sresKc[i] = (byte) (buffer[i] & 0xff);
1213 }
1214 return sresKc;
1215 }
1216
1217 private static void validateInput(final byte[] ki, final byte[] rand) {
1218 if (ki == null || ki.length != 16) {
1219 throw new IllegalArgumentException("The Ki must be 16 bytes");
1220 }
1221 if (rand == null || rand.length != 16) {
1222 throw new IllegalArgumentException("The RAND must be 16 bytes");
1223 }
1224 }
1225
1226
1227 public static byte[] v1(final byte[] ki, final byte[] rand) {
1228 validateInput(ki, rand);
1229 final int[] x = new int[32];
1230 final int[] bits = new int[128];
1231 final byte[] sres_kc = new byte[12];
1232
1233 /* x[16-31] = RAND */
1234 for (int u = 0; u < 16; u++) {
1235 x[u + 16] = (rand[u] & 0xff);
1236 }
1237
1238 /*
1239 * Round 1-7
1240 */
1241 for (int i = 0; i < 7; i++) {
1242 /* x[0-15] = Ki */
1243 for (int u = 0; u < 16; u++) {
1244 x[u] = (ki[u] & 0xff);
1245 }
1246
1247 /* Compression */
1248 _comp128_compression(x);
1249
1250 /* FormBitFromBytes */
1251 _comp128_bitsfrombytes(x, bits);
1252
1253 /* Permutation */
1254 _comp128_permutation(x, bits);
1255 }
1256
1257 /*
1258 * Round 8 (final)
1259 * x[0-15] = Ki
1260 */
1261 for (int u = 0; u < 16; u++) {
1262 x[u] = (ki[u] & 0xff);
1263 }
1264
1265 /* Compression */
1266 _comp128_compression(x);
1267
1268 /* Output stage */
1269 for (int i = 0; i < 8; i += 2) {
1270 sres_kc[i >> 1] = (byte) (x[i] << 4 | x[i + 1]);
1271 }
1272
1273 for (int i = 0; i < 12; i += 2) {
1274 sres_kc[4 + (i >> 1)] = (byte) ((x[i + 18] << 6) |
1275 (x[i + 19] << 2) |
1276 (x[i + 20] >> 2));
1277 }
1278// for (int i = 8; i < 20; i += 2) {
1279// sres_kc[i >> 1] = (byte) ((x[i + 10] << 6) |
1280// (x[i + 11] << 2) |
1281// (x[i + 12] >> 2));
1282// }
1283
1284 sres_kc[10] = (byte) ((x[30] << 6) | (x[31] << 2));
1285 sres_kc[11] = 0;
1286 return sres_kc;
1287 }
1288
1289 public static byte[] v2(final byte[] ki, final byte[] rand) {
1290 return comp128v23(ki, rand, true);
1291 }
1292
1293 public static byte[] v3(final byte[] ki, final byte[] rand) {
1294 return comp128v23(ki, rand, false);
1295 }
1296}
1297
1298////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1299Experiment No.9
1300
1301Aim: To implement Mobile node discovery.
1302
1303Source Code:
1304Program No 1:Blue.java
1305
1306package mypackage;
1307
1308import java.io.*;
1309import javax.microedition.midlet.*;
1310import javax.microedition.lcdui.*;
1311import javax.microedition.io.*;
1312import javax.bluetooth.*;
1313import java.util.*;
1314public class Blue extends MIDlet implements CommandListener,DiscoveryListener
1315{
1316 private List activeDevices;
1317 private Command select,exit;
1318 private Display display;
1319 private LocalDevice local=null;
1320 private DiscoveryAgent agent = null;
1321 private Vector devicesFound = null;
1322 private ServiceRecord[] servicesFound = null;
1323 private String connectionURL = null;
1324 public void startApp() {
1325 display = Display.getDisplay(this);
1326 activeDevices = new List("Active Devices", List.IMPLICIT);
1327 select = new Command("Search Again", Command.OK, 0);
1328 exit = new Command("Exit", Command.EXIT, 0);
1329 activeDevices.addCommand(exit);
1330 activeDevices.setCommandListener(this);
1331 try {
1332 local = LocalDevice.getLocalDevice(); }
1333 catch (Exception e) {}
1334 doDeviceDiscovery();
1335 display.setCurrent(activeDevices);
1336 }
1337
1338 public void pauseApp() {}
1339 public void destroyApp(boolean unconditional) { notifyDestroyed(); }
1340 public void commandAction(Command cmd, Displayable disp) {
1341 if (cmd == select && disp == activeDevices) {
1342 activeDevices.deleteAll();
1343 doDeviceDiscovery();
1344 }
1345 if (cmd == exit) { destroyApp(false); }
1346 }
1347 public void inquiryCompleted(int param) {
1348 try {
1349 switch (param) {
1350 case DiscoveryListener.INQUIRY_COMPLETED:
1351
1352 if (devicesFound.size() > 0) {
1353 activeDevices.addCommand(select);
1354 activeDevices.setSelectCommand(select);
1355 }
1356 else { activeDevices.append("No Devices Found", null); }
1357 break; }
1358 }
1359 catch (Exception e) {}
1360 }
1361 public void serviceSearchCompleted(int transID, int respCode) {}
1362 public void servicesDiscovered(int transID, ServiceRecord[] serviceRecord) {}
1363 public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {
1364 String str = null;
1365 try {
1366 str = remoteDevice.getBluetoothAddress() + " - ";
1367 str += remoteDevice.getFriendlyName(true);
1368 } catch (Exception e) {}
1369 activeDevices.append(str, null);
1370 devicesFound.addElement(remoteDevice);
1371try {
1372 if (!agent.startInquiry(DiscoveryAgent.GIAC, this)) {}
1373} catch (BluetoothStateException e) {
1374 // TODO Auto-generated catch block
1375 e.printStackTrace();
1376}
1377 }
1378 private void doDeviceDiscovery() {
1379 try {
1380 local = LocalDevice.getLocalDevice();
1381 agent = local.getDiscoveryAgent();
1382 devicesFound = new Vector();
1383 } catch (Exception e) {}
1384 }
1385 }
1386
1387
1388
1389
1390
1391Program No 2:discover_device.java
1392
1393package mypackage;
1394
1395import java.io.IOException;
1396import javax.bluetooth.*;
1397import javax.bluetooth.DiscoveryListener;
1398import javax.microedition.lcdui.*;
1399import javax.microedition.midlet.*;
1400
1401public class discover_device extends MIDlet implements CommandListener,DiscoveryListener {
1402
1403private final List deviceList;
1404private final Command Exit,Refresh;
1405private String deviceName;
1406private DiscoveryAgent agent;
1407private Alert dialog;
1408
1409public discover_device()
1410{
1411 deviceList = new List("List of Devices",List.IMPLICIT);
1412 Exit= new Command("Exit",Command.EXIT, 0);
1413 Refresh = new Command("Refresh",Command.SCREEN, 1);
1414 deviceList.addCommand(Exit);
1415 deviceList.addCommand(Refresh);
1416 deviceList.setCommandListener(this);
1417 Display.getDisplay(this).setCurrent(deviceList);
1418}
1419
1420public void startApp() {
1421try {
1422deviceList.deleteAll();
1423LocalDevice local = LocalDevice.getLocalDevice();
1424local.setDiscoverable(DiscoveryAgent.GIAC);
1425deviceName = local.getFriendlyName();
1426agent = local.getDiscoveryAgent();
1427}
1428catch (BluetoothStateException ex) {
1429ex.printStackTrace();
1430}
1431try {
1432 agent.startInquiry(DiscoveryAgent.GIAC, this);
1433}
1434catch (BluetoothStateException ex) {
1435ex.printStackTrace();
1436}
1437}
1438
1439public void pauseApp() {
1440}
1441public void destroyApp(boolean unconditional) {
1442}
1443public void commandAction(Command c, Displayable d) {
1444if(c==Exit)
1445{
1446this.destroyApp(true);
1447notifyDestroyed();
1448}
1449if(c==Refresh){
1450this.startApp();
1451}
1452}
1453
1454public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
1455String deviceaddress = null;
1456try {
1457deviceaddress = btDevice.getBluetoothAddress();//btDevice.getFriendlyName(true);
1458} catch (Exception ex) {
1459ex.printStackTrace();
1460}
1461deviceList.insert(0, deviceaddress , null);
1462}
1463
1464public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
1465 throw new UnsupportedOperationException("Not supported yet.");
1466}
1467
1468 public void serviceSearchCompleted(int transID, int respCode) {
1469 throw new UnsupportedOperationException("Not supported yet.");
1470 }
1471
1472
1473
1474
1475public void inquiryCompleted(int discType) {
1476 Alert dialog = null;
1477 if (discType != DiscoveryListener.INQUIRY_COMPLETED) {
1478 dialog = new Alert("Bluetooth Error","The inquiry failed to complete normally",null, AlertType.ERROR);
1479 }
1480 else {
1481 dialog = new Alert("Inquiry Completed","The inquiry completed normally", null,AlertType.INFO);
1482 }
1483 dialog.setTimeout(500);
1484 Display.getDisplay(this).setCurrent(dialog);
1485
1486 }
1487}
1488Mobile Node Discovery
1489
1490Step 1: First run discover_device.java. You will see this as the output. (Hereafter referred as Device 0)
1491
1492
1493
1494
1495
1496Step 2: Run Blue.java file (You can run this file any number of times). For example, I have run this file for two times. So here I can see that two devices are running. (Hereafter referred as Device 1 and Device 2 respectively).
1497
1498
1499
1500
1501
1502Step 3: Click in Device 0 to the button pointed by the arrow. After clicking you will be able to see the Device number of Device 1 & Device 2 as shown below.
1503
1504////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1505 Experiment No.10
1506
1507Aim: To implement basic function of CDMA.
1508
1509Source Code:
1510
1511
1512package mypackage;
1513
1514//Java code illustrating a simple implementation of CDMA
1515
1516import java.util.*;
1517
1518public class CDMACODE {
1519
1520 private int[][] wtable;
1521 private int[][] copy;
1522 private int[] channel_sequence;
1523
1524 public void setUp(int[] data, int num_stations)
1525 {
1526
1527 wtable = new int[num_stations][num_stations];
1528 copy = new int[num_stations][num_stations];
1529
1530 buildWalshTable(num_stations, 0, num_stations - 1, 0,
1531 num_stations - 1, false);
1532
1533 showWalshTable(num_stations);
1534
1535 for (int i = 0; i < num_stations; i++) {
1536
1537 for (int j = 0; j < num_stations; j++) {
1538
1539 // Making a copy of walsh table
1540 // to be used later
1541 copy[i][j] = wtable[i][j];
1542
1543 // each row in table is code for one station.
1544 // So we multiply each row with station data
1545 wtable[i][j] *= data[i];
1546 }
1547 }
1548
1549 channel_sequence = new int[num_stations];
1550
1551 for (int i = 0; i < num_stations; i++) {
1552
1553 for (int j = 0; j < num_stations; j++) {
1554 // Adding all sequences to get channel sequence
1555 channel_sequence[i] += wtable[j][i];
1556 }
1557 }
1558 }
1559
1560 public void listenTo(int sourceStation, int num_stations)
1561 {
1562 int innerProduct = 0;
1563
1564 for (int i = 0; i < num_stations; i++) {
1565
1566 // multiply channel sequence and source station code
1567 innerProduct += copy[sourceStation][i] * channel_sequence[i];
1568 }
1569
1570 System.out.println("The data received is: " +
1571 (innerProduct / num_stations));
1572 }
1573
1574 public int buildWalshTable(int len, int i1, int i2, int j1,
1575 int j2, boolean isBar)
1576 {
1577 // len = size of matrix. (i1, j1), (i2, j2) are
1578 // starting and ending indices of wtable.
1579
1580 // isBar represents whether we want to add simple entry
1581 // or complement(southeast submatrix) to wtable.
1582
1583 if (len == 2) {
1584
1585 if (!isBar) {
1586
1587 wtable[i1][j1] = 1;
1588 wtable[i1][j2] = 1;
1589 wtable[i2][j1] = 1;
1590 wtable[i2][j2] = -1;
1591 }
1592 else {
1593
1594 wtable[i1][j1] = -1;
1595 wtable[i1][j2] = -1;
1596 wtable[i2][j1] = -1;
1597 wtable[i2][j2] = +1;
1598 }
1599
1600 return 0;
1601 }
1602
1603 int midi = (i1 + i2) / 2;
1604 int midj = (j1 + j2) / 2;
1605
1606 buildWalshTable(len / 2, i1, midi, j1, midj, isBar);
1607 buildWalshTable(len / 2, i1, midi, midj + 1, j2, isBar);
1608 buildWalshTable(len / 2, midi + 1, i2, j1, midj, isBar);
1609 buildWalshTable(len / 2, midi + 1, i2, midj + 1, j2, !isBar);
1610
1611 return 0;
1612 }
1613
1614 public void showWalshTable(int num_stations)
1615 {
1616
1617 System.out.print("\n");
1618
1619 for (int i = 0; i < num_stations; i++) {
1620 for (int j = 0; j < num_stations; j++) {
1621 System.out.print(wtable[i][j] + " ");
1622 }
1623 System.out.print("\n");
1624 }
1625 System.out.println("-------------------------");
1626 System.out.print("\n");
1627 }
1628
1629 // Driver Code
1630 public static void main(String[] args)
1631 {
1632 int num_stations = 4;
1633
1634 int[] data = new int[num_stations];
1635
1636 //data bits corresponding to each station
1637 data[0] = -1;
1638 data[1] = -1;
1639 data[2] = 0;
1640 data[3] = 1;
1641
1642 CDMACODE channel = new CDMACODE();
1643
1644 channel.setUp(data, num_stations);
1645
1646 // station you want to listen to
1647 int sourceStation = 3;
1648
1649 channel.listenTo(sourceStation, num_stations);
1650 }
1651}