How to get the address/name of connected Bluetooth device - Android - Android Studio

I need to get the CONNECTED device name and address.
This is what I have tried so far and with it I'm able to get the paired devices list and addresses.
'''
public class MainActivity extends AppCompatActivity {
TextView mBlue;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBlue = (TextView) findViewById(R.id.Bluetooth);
String s = " ";
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
Set <BluetoothDevice> devices = ba.getBondedDevices();
if (ba != null) {
for (BluetoothDevice device: devices)
s += device.getName() + "-" + device.getAddress() + "\n";
} else
s += "No address";
mBlue.setText(s.toString());
}
}
'''
How to get the device address/name of connected Bluetooth device?
Please help me out with a clear code (both manifest and class code). I have seen other posts on SO but I can't make it out. Please help to solve this.

Related

Help with grid View

Hello!
i need some help, i am creating a Grid View with this java code:
MainActivity
public class MainActivity extends AppCompatActivity {
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new GridAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Click to expand...
Click to collapse
Product
public class Product {
private static String nombrePizza;
private static String ingredientesPizza;
private static int imagenPizza;
private static int idThumbnail;
public Product(String nombrePizza, String ingredientesPizza, int imagenPizza){
this.nombrePizza = nombrePizza;
this.ingredientesPizza = ingredientesPizza;
this.imagenPizza = imagenPizza;
}
public String getNombrePizza(){return nombrePizza;}
public String getIngredientesPizza(){return ingredientesPizza;}
public int getImagenPizza(){return imagenPizza;}
public int getId(){return nombrePizza.hashCode();}
public static Product[] Pizzas={
new Product(
"Proscuito",
"Jamon York y queso",
R.drawable.imagenprueba),
new Product(
"Tropical",
"Jamon York, queso y piña",
R.drawable.imagenprueba),
new Product(
"Barbacoa",
"Carne picada, queso y salsa Barbacoa",
R.drawable.imagenprueba),
new Product(
"Romana",
"Jamon York, champiñones y queso",
R.drawable.cubo),
};
public static Product getItem(int id) {
for (Product item : Pizzas) {
if (item.getId() == id) {
return item;
}
}
return null;
}
}
Click to expand...
Click to collapse
Grid Adapter
public class GridAdapter extends BaseAdapter {
private final Context mContext;
public GridAdapter(Context c){
this.mContext = c;
}
@Override
public int getCount(){return Product.Pizzas.length;}
@Override
public Product getItem(int position){return Product.Pizzas[position];}
@Override
public long getItemId(int position){return 0;}
@Override
public View getView(int position, View ConvertView, ViewGroup viewGroup){
if(ConvertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ConvertView = inflater.inflate(R.layout.grid_item, viewGroup, false);
}
TextView name = (TextView)ConvertView.findViewById(R.id.grid_mainText);
ImageView image = (ImageView) ConvertView.findViewById(R.id.grid_image);
TextView descripcion = (TextView) ConvertView.findViewById(R.id.grid_subText);
final Product item = getItem(position);
image.setImageResource(item.getImagenPizza());
name.setText(item.getNombrePizza());
descripcion.setText(item.getIngredientesPizza());
return ConvertView;
}
}
Click to expand...
Click to collapse
It work fine except for one thing, the app only shows the last product of the java code (Pizza romana)four times
I dont know why my app do this, can somebody help me??
Thank you! :fingers-crossed:

Trouble with implementing In App Billing into my app...

So I was programming in some In app Billing code for my Android app using Android Studio. I was following an Android tutorial called 'Preparing your In-app Billing Application' at the Android Developer website (not allowed to give a link because I'm a new user). All was going well, until I get to the part where I have to paste their code into my code. Check out their code put into mine (I left the base64EncodedPublicKey empty so nobody would steal it):
Code:
public class MainActivity extends ActionBarActivity {
//Add other java files to the main class
private JokeBook mJokeBook = new JokeBook();
private ColorWheel mColorWheel = new ColorWheel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare View variables
final TextView jokeLabel = (TextView) findViewById(R.id.jokeTextView);
final Button showJokeButton = (Button) findViewById(R.id.showJokeButton);
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
final Button moreJokes = (Button) findViewById(R.id.moreJokes);
//On Click
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String joke = mJokeBook.getJoke();
//Update label with fact
jokeLabel.setText(joke);
int color = mColorWheel.getColor();
relativeLayout.setBackgroundColor(color);
showJokeButton.setTextColor(color);
moreJokes.setTextColor(color);
}
};
showJokeButton.setOnClickListener(listener);
goToTwitter();
goToFacebook();
goToExxellerate();
IabHelper mHelper;
@Override
public void [U]onCreate(Bundle savedInstanceState)[/U] {
// ...
String base64EncodedPublicKey = "";
// compute your public key and store it in base64EncodedPublicKey
[B]mHelper = new IabHelper(this, base64EncodedPublicKey);[/B]
}
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
// Oh noes, there was a problem.
[I]Log.d(TAG, "Problem setting up In-app Billing: " + result);[/I]
}
// Hooray, IAB is fully set up!
}
});
@Override
public void [U]onDestroy()[/U] {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
}
The code that they gave me had many errors when pasted into Android Studio, which were:
TAG has private access in 'android.support.v4.app.FragmentActivity' (Italicized text)
Annotations are not allowed here (@Override)
; expected (Underlined text)
IabHelper (android.view.View.onClickListener, String) in IabHelper cannot be applied to (com.exxellerate.joketeller.MainActivity, String) (Bolded Text)
Any help with fixing there errors? I'm a beginner at code and very confused that the code they gave me was wrong...

Executing code in 2end activity

Hi .
I'm newbie so I need your help in some issue
I'm coding an app that has 2 activity .
The problem is I want Some codes to be execute right after user goes to 2end activity .
So I write the codes in Oncreate function . but in test when user goes to 2end activity he will see just empty design of activity . like I didn't write any code for that :/
Please help me what should I do .
p.n : I want 2end activity to show the inbox content to user and I'm sure that my codes is correct .
sry for my bad english .
You need to display all your codes.
hiphop12ism said:
You need to display all your codes.
Click to expand...
Click to collapse
Activity A
Code:
public class A extends Activity implements View.OnClickListener {
Button inbox,draft,sent,call;
TextView lblmsg,lblNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
inbox = (Button) findViewById(R.id.btn1);
sent = (Button) findViewById(R.id.btn2);
draft = (Button) findViewById(R.id.btn3);
call = (Button) findViewById(R.id.btn4);
inbox.setOnClickListener(this);
draft.setOnClickListener(this);
call.setOnClickListener(this);
sent.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == inbox) {
Intent i;
i = new Intent(this, B.class);
startActivity(i);
}
}
Activity B
Code:
public class B extends AppCompatActivity {
ListView lv1;
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
lv1=(ListView)findViewById(R.id.lv);
Uri InboxUri= Uri.parse("Content://sms/inbox");
String [] C= new String[] { "_id", "address", "body" };
ContentResolver cr=getContentResolver();
Cursor D = cr.query(InboxUri, C, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.row,
D,
new String[]{"body","address"},
new int[]{R.id.lblmsg,R.id.lblNumber});
lv1.setAdapter(adapter);
}
and I forgot to say Android studio detecting no error

Handler android

Dear People,
I´m working on a app to control my robot with bluetooth.
So i made a thread to handle the bluetooth device, and a handler to read the information in the Gui thread.
(See code below)
The problem is that i´m not able to print the information to the textview in the handler.
When i print somthing to the textview in de onCreate function, everythings works fine, but when i print something to the textview in the handler, nothing happens.
There are also no errors or something like that.
I know that the handler function is called, because the receive messages are printed well in the log.d.
I hope that there is anyone who can help me.
thanks in advance.
Tom
public class InteractionActivity extends Activity {
private ConnectThread mConnectThread;
private interface MessageConstants
{
public static final int MESSAGE_READ = 0;
public static final int MESSAGE_WRITE = 1;
public static final int MESSAGE_TOAST = 2;
}
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interaction);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
BluetoothDevice device = getIntent().getExtras().getParcelable("btdevice");
((TextView) findViewById(R.id.textView5)).setText("Hallo"); // works fine, print correct to the screen
mConnectThread = new ConnectThread(device);
mConnectThread.start();
}
private final Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;
String writeMessage = new String(writeBuf);
writeMessage = writeMessage.substring(begin, end);
((TextView) findViewById(R.id.textView5)).setText("Hallo1111"); //Nothing happens
Log.d(TAG, writeMessage); //is printed fine in the log
}
};
private class ConnectThread extends Thread {
etc ............

Runtime error when logging off android stuido mobile app

I have a java mobile app that is connected with Firebase, login and registration works perfect. I only get an error when I try to logout from the Home Page/Activity. This is the error i get and I don't know what to do.
Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference at com.example.platformapp.HomeActivity.onCreate(HomeActivity.java:37)
This is my code for the Home Activity/Page
public class HomeActivity extends AppCompatActivity {
Button btnLogout;
FirebaseAuth myFireBaseAuth;
TextView email;
FirebaseFirestore myfStore;
String userID;
private FirebaseAuth.AuthStateListener myAuthStateListener;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
email = findViewById(R.id.displayEmail);
myFireBaseAuth = FirebaseAuth.getInstance();
myfStore = FirebaseFirestore.getInstance();
userID = myFireBaseAuth.getCurrentUser().getUid(); // This is line 37
DocumentReference documentReference = myfStore.collection("users").document(userID);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@override
public void onEvent @nullable DocumentSnapshot documentSnapshot, @nullable FirebaseFirestoreException e) {
email.setText(documentSnapshot.getString("email"));
}
});
}
public void logout(View view)
{
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}

Categories

Resources