Connect laptop and android via bluetooth - android

Hi I want to connect bluetooth and laptop
What I want to do is, send numeric value from android application to Labview program installed in Laptop.
The android program returns value which changes according to the button click.
(When I press up button, value +1).
And I want to send this value to the laptop via bluetooth!.
I was looking for google, stackoverflow and other many communities., I couldn't find any hints or solutions.
package com.u2ring.control;
import com.u2ring.control.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.webkit.*;
public class MainActivity extends Activity implements OnClickListener
{
Button Plus, Minus;
TextView Value;
TextView url;
int score = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Plus = (Button) findViewById(R.id.up);
Minus = (Button) findViewById(R.id.down);
Value = (TextView) findViewById(R.id.number);
String host = getString(R.string.host);
Plus.setOnClickListener(this);
Minus.setOnClickListener(this);
Button bt1 = (Button) findViewById(R.id.button2);
bt1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent in = new Intent(MainActivity.this,Secondpage.class);
startActivity(in);
}
});};
public void onClick(View v)
{
boolean showText = false;
int id = v.getId();
if (id == R.id.up) {
score++;
showText = true;
} else if (id == R.id.down) {
score--;
showText = true;
} else if (id == R.id.number) {
showText = true;
}
if(showText)
Value.setText(String.valueOf(score));
WebView wv= (WebView) findViewById(R.id.web);
wv.loadUrl("http://10.16.27.184:8080/admin/speed/"+Integer.toString(score));
}
}

You will need to connect the laptop and the phone over bluetooth first either manually or create a service. Android provides access to your phone's Bluetooth through BluetoothAdapter. To access the BluetoothAdapter you will need to add the appropriate permission(for bluetooth) in the manifest file.
You can then manage connections through the different methods of the BluetoothAdapter.For details visit here

Related

How to assign a value to radio button in eclipse

I am developing an application that contains 10 set of questions. One questions belong to one page, thus i have 10 different pages. Each answer using radio button. I want to ask, how to assign value on radio button and bring along the value through the 10 pages and displays the result in the result page?
package com.project.logicalthinking;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RadioGroup;
public class question1 extends Activity
{
private Button Button2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question1);
addListenerRadioButton() ;
//Button2
Button2 = (Button) findViewById(R.id.button1);
Button2.setOnClickListener((new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(v.getContext(),question2.class);
startActivityForResult(intent,0);
}
}
));
}
private void addListenerRadioButton() {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnTouchListener(new OnTouchListener()
{
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
;
}
I'm not an android guru (someone correct me if I'm wrong), but could you declare static variables for each activity that define which option was selected and then in the Results activity check the static variable values?
public static int QUESTION1_NO_ANSWER = 0;
public static int QUESTION1_ANSWER1 = 1;
public static int QUESTION1_ANSWER2 = 2;
public static int QUESTION1_ANSWER3 = 3;
public static int selectedAnswer;
Then in your results....
if(QUESTION1.selectedAnswer = QUESTION1_ANSWER2)
//YAY got question right!
} else {
//uh oh, it was wrong
}
You will need to populate the questions, one by one, as the user selects Yes/No using the radio button and pressing "Next". Yes/No could be a radio button and "Next" is just a Button. A new question is loaded in the OnClickListener of the "Next" button.

Connect android bluetooth and laptop

Hi I want to connect bluetooth and laptop
What I want to do is, send numeric value from android application to Labview program installed in Laptop.
The android program returns value which changes according to the button click.
(When I press up button, value +1).
And I want to send this value to the laptop via bluetooth!.
I was looking for google, stackoverflow and other many communities., I couldn't find any hints or solutions.
package com.u2ring.control;
import com.u2ring.control.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.webkit.*;
public class MainActivity extends Activity implements OnClickListener
{
Button Plus, Minus;
TextView Value;
TextView url;
int score = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Plus = (Button) findViewById(R.id.up);
Minus = (Button) findViewById(R.id.down);
Value = (TextView) findViewById(R.id.number);
String host = getString(R.string.host);
Plus.setOnClickListener(this);
Minus.setOnClickListener(this);
Button bt1 = (Button) findViewById(R.id.button2);
bt1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent in = new Intent(MainActivity.this,Secondpage.class);
startActivity(in);
}
});};
public void onClick(View v)
{
boolean showText = false;
int id = v.getId();
if (id == R.id.up) {
score++;
showText = true;
} else if (id == R.id.down) {
score--;
showText = true;
} else if (id == R.id.number) {
showText = true;
}
if(showText)
Value.setText(String.valueOf(score));
WebView wv= (WebView) findViewById(R.id.web);
wv.loadUrl("http://10.16.27.184:8080/admin/speed/"+Integer.toString(score));
}
}
There is a nice Bluetooth example with code for LabVIEW and Android on LabVIEW Hacker.

Why does Text = null?

I am new to Android and Java and don't know why the Text value is null after I assign Text to it? This is a project for University They didn't teach us Java only gave us Eclipse and Android Sdk and Java need some help please!
package com.example.glossaryapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Syntax extends Activity {
static String Text;
public static TextItem[] Syntax = new TextItem[50];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.syntax);
final TextView show = (TextView) Syntax.this
.findViewById(R.id.textViewSyn);
// Linking the text View widget
int Count1 = 0;
// Using the TextView's shows method set text to display the appropriate
// arrays and indexes
if (Syntax[0] != null) {
Text = Syntax[0].displayText();
show.setText(Text);
}
// The Add button for if a user wants to add a new TextItem to the Array
Button but1 = (Button) findViewById(R.id.butAddSyntax);
but1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Create.strSender = "Syntax1";
startActivity(new Intent(Syntax.this, Create.class));
}// end of on click
});// butAddSyntax
// Home Button
Button but2 = (Button) findViewById(R.id.buttHomeSyntax);
but2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}// end of on click
});// buttonHomeSyntax
}
public void onResume() {
super.onResume();
TextView show = (TextView) Syntax.this.findViewById(R.id.textViewSyn);
if (Syntax[0] != null) {
Text = Syntax[0].displayText().toString();
show.setText(Text);
}
}
}
Using Android's LogCat will help you to solve this kind of problems.
Here you have a nice article:
http://www.vogella.com/tutorials/AndroidLogging/article.html
I think the problem is in here:
if( Syntax[0] != null ){
Text = Syntax[0].displayText();
show.setText(Text);
}
you could begin using LogCat trying this:
if( Syntax[0] != null ){
Log.d("MyApp", "Sintax (0) = " + Syntax[0].displayText());
Text = Syntax[0].displayText();
show.setText(Text);
} else Log.d("MyApp", "Nothing ");
And watch the result in the Logcat pane.

Android Apptitude practice app

I want to develop an aptitude app..
for that in my text view i have to display first question .. On clicking next button i have to display second question.. on again clicking same next button third question have to be displayed.. liked that i want to display some 30 questions ..all questions should be displayed in single java file.I Tried to display two questions . but for multiple questions i could not find the code..
package com.example.asl;
import java.util.Arrays;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Aptitude extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
Button b=(Button) findViewById(R.id.button1);
final TextView tv=(TextView) findViewById(R.id.textView1);
final String Question[]={"what is UR Name","What is ur Age","Whats ur Qualification"};
Button btnNext = (Button) findViewById(R.id.button1);
final TextView cumulos = (TextView) findViewById(R.id.textView1);
//TextView respostas = (TextView)findViewById(R.id.respostas);
Random randPhrase = new Random();
final String[] cum = {"what is UR Name","What is ur Age","Whats ur Qualification"};
//String[] resp = getResources().getStringArray(R.array.resp_cumulos);
String textout = "";
String textresp = "";
//Button btnPrevious = (Button) findViewById(R.id.yourPreviousbutton);
btnNext.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
int i = 0;
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
// respostas.setText(resp[i]);
}
}
});
//btnPrevious.setOnClickListener(new OnClickListener(){
//public void onClick(View arg0) {
//if(i>0){
// i-=1;
// cumulos.setText(cum[i]);
// respostas.setText(resp[i]);
// }
// }
//});
}
}
enter code here
Initializing your counter in your onClick() is always going to reset it
Initialize it outside of onClick() and increment it in onClick() as you are.
public class Aptitude extends Activity {
int i = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
Button b=(Button) findViewById(R.id.button1);
...
}
public void onClick(View arg0) { // rename arg0 to something meaningful
// like v or view for readibility
// int i = 0; remove this guy
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
If this doesn't fix your problem then please explain what the problem is but I'm sure this part was causing you trouble.

How can I keep my Java Android code short?

Hi I am developing an Android app using an EditText, and a add en minus button to control it's value.
I got it working now, but I wanna repeat it to multiple values. I know I can just repeat the code with different variables but the code would big really large.
Anyone an idea how to repeat the same code for multiple values?
This is my current code:
package com.lars.MyApp;
import com.lars.MyApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAppActivity extends Activity {
int currentValue = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText Value = (EditText) findViewById(R.id.Value);
Button addButton = (Button) findViewById(R.id.addButton);
Button minusButton = (Button) findViewById(R.id.minusButton);
addButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
addValue();
Value.setText("" + currentValue);
}
});
minusButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
minusValue();
Value.setText("" + currentValue);
}
});
}
public void addValue(){
if(currentValue <= 999){
currentValue = currentValue + 1;
}
}
public void minusValue(){
if(currentValue >= 1){
currentValue = currentValue - 1;
}
}
}
You should refactor your OnClickListeners so that they are generic. Probably the easiest way to do this is to change your addValue() method to be addValue(View v), and minusValue() to minusValue(View v). Then, in the layout xml, you add a property android:onClick=addValue or android:onClick=minusValue. You'll have to update the method bodies so that they check the view's id and do the right thing based on that, but you won't have to create and set a whole bunch of OnClickListeners in the onCreate method - you'll get that for free.

Categories

Resources