Android: Issue getting Bluetooth name - android

I have this method to retrieve the Android device bluetooth name:
public String getDeviceName() {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) return "Unknown device";
String deviceName = mBluetoothAdapter.getName();
return deviceName;
}
It is working properly in most devices, but in a few others I get the device model number, like GT-P1000 (Samsung Galaxy Tab), instead. However, in all devices where I have installed the app, I have changed the device name (Settings -> About device). Any idea? Does this method work whether bluetooth is off?
Thanks!

Related

How to get only present connected Bluetooth device name in android

I am trying get the connected Bluetooth device name in android.
Done like below ,
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 :
getConnectionState (BluetoothDevice device, int profile)
getConnectedDevices (int profile) //get fetch connected device's info
Example :
BluetoothManager btManager = (BluetoothManager)
mContext.getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> 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()

Android OS 9.0 , BLE 5.0 specific issue fetching list of services from BluetoothGatt

Developed the following source code for sending message from one Android device to another Android device using BLE:
public static String SERVICE_STRING = "6exxxxxx-xxxx-xxxx-xxxx-xxxxxdcca9e";
public static String CHARACTERISTIC_ECHO_STRING = "6eyyyyyy-yyyy-yyyy-yyyy-yyyyycca9e";
List<BluetoothGattService> serviceList = bluetoothGatt.getServices();
On checking the list of services , on Realme 5 Pro(Android OS 9.0 , BLE v5.0) after a successful connection is established with another device, the list of services is not returning the specific service with service string as mentioned above , hence the following code(BluetoothGattService) is returned as null , so unable to send data to another device via BLE.
BluetoothGattService service = findService(serviceList);
if (service == null) {
Log.i("Null service", "Null service.");
return matchingCharacteristics;
}
private static BluetoothGattService findService(List<BluetoothGattService> serviceList) {
for (BluetoothGattService service : serviceList) {
String serviceIdString = service.getUuid()
.toString();
if (matchesServiceUuidString(serviceIdString)) {
return service;
}
}
return null;
}
private static boolean matchesServiceUuidString(String serviceIdString) {
return uuidMatches(serviceIdString, SERVICE_STRING);
}
The same source code is returning the list of services with the mentioned service string for other devices like Mi 4a(Android OS 7.0 , BLE 4.0) , Realme 3i(Android OS 9.0, BLE 4.2) and for them , the BluetoothGattService instance is returning the details which is not null and hence able to send messages to other devices.
Did anyone face the same issue with device with Android OS v9.0 and BLE v5.0 ?

Not able to get bluetooth paired android devices name, which is changed programatically from another device

I am trying to connect 2 android devices via blue-tooth, I m changing Bluetooth name by bluetoothDevice.setName() method, but the updated name is not showing on another device. The old name is coming, when I click on old name for connecting, connection get to fail and when I again come on the screen, the New name gets reflected.
Please help, trying for a long time.
For getting bonded devices:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
System.out.println("BT not available");
}
else {
if(!bluetoothAdapter.isEnabled()) {
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
}
Set<BluetoothDevice> all_devices = bluetoothAdapter.getBondedDevices();
if (all_devices.size() > 0) {
for (BluetoothDevice currentDevice : all_devices) {
log.i("Device Name " + currentDevice.getName());
}
}
}
For changing name of bluetooth device name:
bluetoothDevice.setName()

Get RSSI Value of Android Explicitly Paired Devices

I have paired my device to the Android phone explicitly, via an app. This device is a MI-Band 2, and it is paired using the MI-FIT app. I am currently connecting to bluetooth devices using this code:
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
However, the problem is that if the MI-band is connected via the mi-fit app, I cannot get the RSSI value from the MI-Band.
Is it possible to get the RSSI value of explicitly paired devices in Android? If so, what am I doing wrong?
Thanks
Edit: It seems like I can't get RSSI values of any connected devices. For example, if Android phone A is connected to Android Phone B, then I can't read the RSSI values when trying to read via the above code from Phone A.
If you know for sure the bluetooth name of device to be connected, maybe you can try using BluetoothAdapter.LeScanCallback to get noticed when it's scanned. Or do you really need to tell scanned or connected apart?
BluetoothManager bleManager = (BluetoothManager) getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bleAdapter = bleManager.getAdapter();
bleAdapter.startLeScan(leScanCallback);// find match device
BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
if (device.getName() == null || !device.getName().equals("your bluetooth device name")) {
return;
}
// here you can get rssi of specified bluetooth device if it's available.
// and try to connect it programmatically.
connect(device.getAddress());
}
};

How to detect hands free garniture on Android 2.2+?

I want to know when user has connected hands free accessories and hasn't blocked calls\sms. Is it possible to know when it is connected via hardware ports or bluetooth?
Try this in your onCreate or onResume.
BluetoothAdapter myLocalAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice garniture;
Set<BluetoothDevice> connectedDevices = myLocalAdapter.getBondedDevices();
for (BluetoothDevice device : connectedDevices){
String name = device.getName();
//... check for the name you want
if( name.equals("whatnameisit"){
garniture = device
}
}
if (garniture != null){
// yay we found it, lets do our work with the device here
}

Categories

Resources