- Set Bluetooth device name in Android Source Code?
- Set Bluetooth device name in Android Source Code?
- How to get list of connected bluetooth devices on android
- android get connected bluetooth devices
- Android Bluetooth List Paired Devices Example
- How to get information ( specially headset device name ) of bluetooth headset device in android
- How to get name of the connected Bluetooth device on android
- 2 Answers 2
- how to get Bluetooth name of peer device in Android?
- 2 Answers 2
- How to get current Bluetooth name in android?
Set Bluetooth device name in Android Source Code?
I’m going to try setting it to the device name manually, but I’d prefer that this fix be usable across several devices. My question is very similar to this one, I need to know how to change to display the actual device name, i.e. reading it from the build.prop, as opposed to displaying «BlueZ» I found the in (Might be slightly off) and it contains a line regarding the device name, with the variable set to «BlueZ».
Set Bluetooth device name in Android Source Code?
My question is very similar to this one, I need to know how to change BlueZ to display the actual device name, i.e. reading it from the build.prop, as opposed to displaying «BlueZ»
I found the main.conf in external/bluetooth/bluez/src/main.conf (Might be slightly off) and it contains a line regarding the device name, with the variable set to «BlueZ». I tried changing it to both %d and %h , neither of which made any change. I’m going to try setting it to the device name manually, but I’d prefer that this fix be usable across several devices.
# Default adaper name # %h - substituted for hostname # %d - substituted for adapter id Name = "Bluez"
I’ve tried substituting the above two variables, but neither seem to have any effect.
What about from an outside app? You could just make an Android build and, at the last state, run an app to change the Android device name?
You can use IBluetooth.aidl -> setName to change the Bluetooth name.
Tutorials can be found here, which further references this.
In short, in src you make a package android.bluetooth, Inside it you copy paste IBluetooth.aidl and IBluetoothCallback.aidl (you can find them in the previous link).
In your code , import the package: import android.bluetooth.IBluetooth;
Then implement this method to get the Bluetooth object:
@SuppressWarnings("rawtypes") private IBluetooth getIBluetooth() < IBluetooth ibt = null; try < Class c2 = Class.forName("android.os.ServiceManager"); Method m2 = c2.getDeclaredMethod("getService", String.class); IBinder b = (IBinder) m2.invoke(null, "bluetooth"); Class c3 = Class.forName("android.bluetooth.IBluetooth"); Class[] s2 = c3.getDeclaredClasses(); Class c = s2[0]; Method m = c.getDeclaredMethod("asInterface", IBinder.class); m.setAccessible(true); ibt = (IBluetooth) m.invoke(null, b); >catch (Exception e) < Log.e("flowlab", "Erroraco. " + e.getMessage()); >return ibt; >
Then instance this object: IBluetooth ib =getIBluetooth();
and probably use ib.setName(«something»);
Android how to get the name of connected bluetooth, i want to know how to get the name of the connected bluetooth device in android here is the code NetworkInfo bluetooth = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH); if
How to get list of connected bluetooth devices on android
android get connected bluetooth devices
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); List connected = manager.getConnectedDevices(GATT); Log.i("Connected Devices: ", connected.size()+"");
Java — Display Android Bluetooth Device Name, The below code will get u the bluetooth name, here mBluetoothAdapter is of type BluetoothAdapter. String android_id = … Code samplepublic String getLocalBluetoothName()
Android Bluetooth List Paired Devices Example
The getBoundedDevices() method of BluetoothAdapter class provides a set containing list of all paired or bounded bluetooth devices.
In this example, we are checking if the bluetooth is turned off, if yes then turn it on and list all the paired devices.
activity_main.xml
Drag one textview from the pallete, now the activity_main.xml file will like this:
Provide Permission
You need to provide following permissions in AndroidManifest.xml file.
The full code of AndroidManifest.xml file is given below.
Activity class
Let’s write the code to provide the list of paired (bounded) bluetooth devices.
package com.example.bluetoothshowpaired; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import java.util.Set; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.widget.TextView; public class MainActivity extends Activity < TextView textview1; private static final int REQUEST_ENABLE_BT = 1; BluetoothAdapter btAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview1 = (TextView) findViewById(R.id.textView1); // Getting the Bluetooth adapter btAdapter = BluetoothAdapter.getDefaultAdapter(); textview1.append("\nAdapter: " + btAdapter); CheckBluetoothState(); >/* It is called when an activity completes.*/ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) < super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_ENABLE_BT) < CheckBluetoothState(); >> @Override protected void onDestroy() < super.onDestroy(); >private void CheckBluetoothState() < // Checks for the Bluetooth support and then makes sure it is turned on // If it isn't turned on, request to turn it on // List paired devices if(btAdapter==null) < textview1.append("\nBluetooth NOT supported. Aborting."); return; >else < if (btAdapter.isEnabled()) < textview1.append("\nBluetooth is enabled. "); // Listing paired devices textview1.append("\nPaired Devices are:"); Setdevices = btAdapter.getBondedDevices(); for (BluetoothDevice device : devices) < textview1.append("\n Device: " + device.getName() + ", " + device); >> else < //Prompt user to turn on Bluetooth Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); >> > @Override public boolean onCreateOptionsMenu(Menu menu) < // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; >>
You need to run it on the real device (e.g. mobile) to test the application.
How to get current Bluetooth name in android?, This example demonstrate about How to get current Bluetooth name in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill …
How to get information ( specially headset device name ) of bluetooth headset device in android
I am creating an app where I need to display the name of connected bluetooth headset . My headset is switched on and connected to android device . I am routing phone audio to headset , but I am unable to display the connected headset name . I tried with » getName () » method , but it returns another paired bluetooth mobile device which is not currently connected and switched off . Need suggestions so badly . UPDATE I used this code . But unfortunately it returns an android bluetooth device name which is not currently connected , where my headset is still connected and I am able to route phone audio
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.cancelDiscovery(); Set getDevices = bluetoothAdapter.getBondedDevices(); String remoteDevice = null; for (BluetoothDevice getDevice : getDevices) < //stringBuilder.append(getDevice); remoteDevice = getDevice.toString(); >blueToothDevice = bluetoothAdapter.getRemoteDevice(remoteDevice); StringBuilder stringBuilder1 = new StringBuilder(); stringBuilder1.append(blueToothDevice.getName());
As you’re iterating through the list of devices, you need to look for the device you are interested in — if you assign remoteDevice over and over, only the last device in the list will be represented. Further, it’s not trivial to see whether a paired device is currently connected; the only thing you can do is to try to connect and listen for connectivity state.
Actually I am quite new to android . As being newer to android developing I don’t have clear idea about listening to bluetooth connectivity state . I think it could be done with broadcast receiver . But as far I know broadcast receiver receives an intent when some event occurred . But suppose my app is not started yet , though bluetooth headset is paired and connected . If I launch my app later my app could not receive the broadcast intent , coz at the launching time no pairing or connection was made , as a result not broadcast was sent . What should I do at this moment ?
How to get name of the connected Bluetooth device on android
I can get information about the device that is previously paired and trying to make a connection or a device trying to pair to the device. what I want is the name or the connection state of the currently paired and connected device.
2 Answers 2
String name; String address; String threadName; public void checkConnected() < BluetoothAdapter.getDefaultAdapter().getProfileProxy(this, serviceListener, BluetoothProfile.HEADSET); >private BluetoothProfile.ServiceListener serviceListener = new BluetoothProfile.ServiceListener() < @Override public void onServiceDisconnected(int profile) < >@Override public void onServiceConnected(int profile, BluetoothProfile proxy) < for (BluetoothDevice device : proxy.getConnectedDevices()) < name = device.getName(); address = device.getAddress(); threadName = Thread.currentThread().getName(); Toast.makeText(MainActivity.this, name+" " + address+ threadName, Toast.LENGTH_SHORT).show(); txtName.setText(name + " " + address); Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = " + BluetoothProfile.STATE_CONNECTED + ")"); >BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy); > >;
BluetoothServerSocket bluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("abc", uuid); BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept(); BluetoothDevice device = bluetoothSocket.getRemoteDevice(); String deviceName = device.getName();
looks like the code stock when it get to this line BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept();
Have you checked the documentation? developer.android.com/guide/topics/connectivity/… Check out the sections «Querying paired devices» and «Discovering devices». Especially the call to device.getName()
Thanks for your reply. But what I want is to get the connection status of the currently paired and connected Bluetooth device . No the ones that are trying to connect. For that situation I preferred to use broadcast Listers
And about the link you mentioned, BluetoothDevice class doesn’t provide a function to make a query of paired devices connection state
how to get Bluetooth name of peer device in Android?
how is it possible to get the Bluetooth information (name, Mac address. ) of the peer device that the android device is connected to that via Bluetooth ?
2 Answers 2
You can use the following code to get all remote devices 🙂
private void discoveryDevice() < // Register the BroadcastReceiver IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy mBluetoothAdapter.startDiscovery(); >// Create a BroadcastReceiver for ACTION_FOUND 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)) < // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d(TAG, "device " + device.getName() + "\n" + device.getAddress()); >> >;
It’s really good for you to get the name of remote device throught the code:
Log.d(TAG, "device " + device.getName() + "\n" + device.getAddress());
I can’t test this code at this moment but maybe can give an idea to how you can get the name and others parameters of bluetooth.
static BluetoothSocket socket ; public static boolean btCon(Context ctx)< Method m; try < BluetoothDevice devc = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MAC_ADRS); m = devc.getClass().getMethod("createRfcommSocket", new Class[]); socket = (BluetoothSocket)m.invoke(devc, Integer.valueOf(1)); socket.connect(); String name = devc.getName();/* or */ String name2 = devc.getName().toString(); Log.i("Test","Name: "+name); Log.i("Test","Name 2: "+name2); return true; >catch(Exception e) < return false; >>
How to get current Bluetooth name in android?
This example demonstrate about How to get current Bluetooth name in android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
In the above code, we have taken text view to show Bluetooth name.
Step 3 − Add the following code to src/MainActivity.java
package com.example.myapplication; import android.bluetooth.BluetoothManager; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.os.Build; import android.os.Bundle; import android.os.health.SystemHealthManager; import android.provider.Telephony; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.telephony.SmsManager; import android.view.View; import android.view.WindowManager; import android.widget.TextView; public class MainActivity extends AppCompatActivity < TextView textView; @RequiresApi(api = Build.VERSION_CODES.N) @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text); final BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); textView.setText(manager.getAdapter().getName()); >@Override protected void onStop() < super.onStop(); >@Override protected void onResume() < super.onResume(); >>
Step 4 − Add the following code to androidManifest.xml
Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
Click here to download the project code