Android bluetooth device name

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

Источник

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.

Читайте также:  Bluetooth не работает gps

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()String name = mBluetoothAdapter.getName();Feedback

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 …

Читайте также:  Kenwood kmm 125 bluetooth

Источник

Display Android Bluetooth Device Name

Solution 1: You can use Android BluetoothManager following functions you can make use of : Example : Solution 2: If you have the of the connected device then you can get the device by And to get the name of the connected device Question: I want to scan and bond with nearby android smartwatches which are running WearOS. Question: I am trying get the connected Bluetooth device name in android.

Display Android Bluetooth Device Name

How to display a Bluetooth device name in android which uses Java? Any codes for me to refer to?

The below code will get u the bluetooth name, here mBluetoothAdapter is of type BluetoothAdapter .

 public String getLocalBluetoothName() < if(mBluetoothAdapter == null)< mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); >String name = mBluetoothAdapter.getName(); if(name == null) < System.out.println("Name is null!"); name = mBluetoothAdapter.getAddress(); >return name; > 

Android: How to get more information of a bluetooth device?, Not for «classic Bluetooth devices», as opposed to Bluetooth Low Energy (BLE) devices. Information such as model number, manufacturer name,

How to change the Bluetooth name on your Android 11 phone

How to rename your Bluetooth device name used for pairing. To do this go to settings Then tap Duration: 0:55

How to Pair Bluetooth with Android

Check the instructions that came with your device to find out how to do this. Once you’ve done Duration: 2:30

How to get only present connected Bluetooth device name in android

I am trying get the connected Bluetooth device name in android.

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); String name = mBluetoothAdapter.getName(); Log.d(TAG,"name--->"+name); 

but I am getting my device name.

You can use Android BluetoothManager

following functions you can make use of :

getConnectedDevices (int profile) //get fetch connected device’s info

BluetoothManager btManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE); List devices = btManager.getConnectedDevices(BluetoothProfile.GATT); for(BluetoothDevice device : devices) < // you will get each device's info here. >

If you have the mac-address of the connected device then you can get the device by

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mac-address); 

And to get the name of the connected device

String connectedDeviceName = device.getName() 

Update nearby unpaired bluetooth devices list in listview [Android, I am trying to scan all (paired and unpaired) bluetooth devices and display a list of these devices on application screen. I have been

How to filter nearby bluetooth devices by type

I want to scan and bond with nearby android smartwatches which are running WearOS. I followed the instruction here using CompanionDeviceManager: https://developer.android.com/guide/topics/connectivity/companion-device-pairing

The CompanionDeviceManager always shows a list of all device types including other mobile phones, laptops, bluetooth speakers, and watches (Both bonded and unpaired). Comparing to Google Wear OS app, it also use CompanionDeviceManager (since I saw the same popup UI) and it always show the Android Watch only.

How does it filter the smartwatch among plenty of other bluetooth devices?

private fun scanLeDevice(enable: Boolean) < val deviceFilter: BluetoothLeDeviceFilter = BluetoothLeDeviceFilter.Builder() .build() // The argument provided in setSingleDevice() determines whether a single // device name or a list of device names is presented to the user as // pairing options. val pairingRequest: AssociationRequest = AssociationRequest.Builder() .setSingleDevice(false) .addDeviceFilter(deviceFilter) .build() // When the app tries to pair with the Bluetooth device, show the // appropriate pairing request dialog to the user. deviceManager.associate(pairingRequest, object : CompanionDeviceManager.Callback() < override fun onDeviceFound(chooserLauncher: IntentSender) < startIntentSenderForResult(chooserLauncher, Companion.SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0) >override fun onFailure(error: CharSequence?) < // Handle failure >>, null) > 

Remark: This isn’t completed answer since I don’t know what’s exactly filter-mask for each device type, however at least it can help to filter the Android smartwatch

Читайте также:  Using your bluetooth device

I decompiled the Google WearOS app and looking at source file FindDeviceControllerSdk26.java. Here are interested things:

 private static final byte[] BLE_WEAR_MATCH_DATA = < 0, 20 >; private static final byte[] BLE_WEAR_MATCH_DATA_LEGACY = < 0, 19 >; private static final byte[] BLE_WEAR_MATCH_MASK = < 0, -1 >; private final void showAssociationDialog() < Object localObject1 = new ArrayList(); Object localObject3 = new FindDeviceDeviceFilterBuilder(); medium = 1; Object localObject4 = new ScanFilter.Builder(); byte[] arrayOfByte = BLE_WEAR_MATCH_DATA; Object localObject2 = BLE_WEAR_MATCH_MASK; scanFilter = ((ScanFilter.Builder)localObject4).setManufacturerData(224, arrayOfByte, (byte[])localObject2).build(); FindDeviceAssociationRequestBuilder.unbox_addDeviceFilter$ar$ds((FindDeviceDeviceFilterBuilder)localObject3, (List)localObject1); localObject3 = new FindDeviceDeviceFilterBuilder(); medium = 1; scanFilter = new ScanFilter.Builder().setManufacturerData(224, BLE_WEAR_MATCH_DATA_LEGACY, (byte[])localObject2).build(); FindDeviceAssociationRequestBuilder.unbox_addDeviceFilter$ar$ds((FindDeviceDeviceFilterBuilder)localObject3, (List)localObject1); if (!deviceWhitelist.shouldShowAllDevices()) < localObject2 = (GServicesFindDeviceWhitelist)deviceWhitelist; ((GServicesFindDeviceWhitelist)localObject2).initializeIfNecessary(); localObject2 = cachedDeviceWhitelistRegularExpression; if (!TextUtils.isEmpty((CharSequence)localObject2)) < localObject3 = new FindDeviceDeviceFilterBuilder(); medium = 0; namePatternString = ((String)localObject2); FindDeviceAssociationRequestBuilder.unbox_addDeviceFilter$ar$ds((FindDeviceDeviceFilterBuilder)localObject3, (List)localObject1); >> else < localObject2 = new FindDeviceDeviceFilterBuilder(); medium = 0; namePatternString = ".*"; FindDeviceAssociationRequestBuilder.unbox_addDeviceFilter$ar$ds((FindDeviceDeviceFilterBuilder)localObject2, (List)localObject1); >localObject2 = new AssociationRequest.Builder().setSingleDevice(false); localObject3 = ((List)localObject1).iterator(); while (((Iterator)localObject3).hasNext()) < localObject1 = (FindDeviceDeviceFilterBuilder)((Iterator)localObject3).next(); if (medium != 0) < localObject4 = new BluetoothLeDeviceFilter.Builder(); localObject1 = scanFilter; if (localObject1 != null) < ((BluetoothLeDeviceFilter.Builder)localObject4).setScanFilter((ScanFilter)localObject1); >localObject1 = ((BluetoothLeDeviceFilter.Builder)localObject4).build(); > else < localObject4 = new BluetoothDeviceFilter.Builder(); if (!Platform.stringIsNullOrEmpty(namePatternString)) < ((BluetoothDeviceFilter.Builder)localObject4).setNamePattern(Pattern.compile(namePatternString)); >localObject1 = ((BluetoothDeviceFilter.Builder)localObject4).build(); > ((AssociationRequest.Builder)localObject2).addDeviceFilter((DeviceFilter)localObject1); > localObject1 = ((AssociationRequest.Builder)localObject2).build(); viewClient.hideRefreshButton(); cwEventLogger.incrementCounter(Counter.COMPANION_PAIR_CDM_ASSOCIATE_REQUEST); companionDeviceManager.associate((AssociationRequest)localObject1, deviceManagerCallback, null); > 

I then rewrite my scan function as following

 private val BLE_WEAR_MATCH_DATA = byteArrayOf(0, 20) private val BLE_WEAR_MATCH_DATA_LEGACY = byteArrayOf(0, 19) private val BLE_WEAR_MATCH_MASK = byteArrayOf(0, -1) private fun scanLeDevice(enable: Boolean) < val scanFilter = ScanFilter.Builder().setManufacturerData(224,BLE_WEAR_MATCH_DATA_LEGACY,BLE_WEAR_MATCH_MASK).build() val deviceFilter: BluetoothLeDeviceFilter = BluetoothLeDeviceFilter.Builder() .setScanFilter(scanFilter) .setNamePattern(Pattern.compile(".*")) .build() // The argument provided in setSingleDevice() determines whether a single // device name or a list of device names is presented to the user as // pairing options. val pairingRequest: AssociationRequest = AssociationRequest.Builder() .setSingleDevice(false) .addDeviceFilter(deviceFilter) .build() // When the app tries to pair with the Bluetooth device, show the // appropriate pairing request dialog to the user. deviceManager.associate(pairingRequest, object : CompanionDeviceManager.Callback() < override fun onDeviceFound(chooserLauncher: IntentSender) < startIntentSenderForResult(chooserLauncher, Companion.SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0) >override fun onFailure(error: CharSequence?) < // Handle failure >>, null) > 

I would hope someone can help to describe what do these maskes stand for, and then I will help to update to make this answer completely.

private val BLE_WEAR_MATCH_DATA = byteArrayOf(0, 20)

private val BLE_WEAR_MATCH_DATA_LEGACY = byteArrayOf(0, 19)

private val BLE_WEAR_MATCH_MASK = byteArrayOf(0, -1)

Please look at that article: https://medium.com/@martijn.van.welie/making-android-ble-work-part-1-a736dcd53b02

String[] names = new String[]; List filters = null; if(names != null) < filters = new ArrayList<>(); for (String name : names) < ScanFilter filter = new ScanFilter.Builder() .setDeviceName(name) .build(); filters.add(filter); >> scanner.startScan(filters, scanSettings, scanCallback); 

Try restarting your device. Just the primary time, as once it is at the SmartWatch application list, you might not have the identical trouble.

Android — Scanning for Bluetooth devices with startDiscovery but no, You have declared the permissions in the Manifest but have you asked user to allow the ACCESS_FINE_LOCATION & ACCESS_COARSE_LOCATION

Источник

Оцените статью
Adblock
detector