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
}
Related
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()
I'm working with device Texas, for management of the LED, in my custom Android app. The device has enabled the pairing with a default passkey 000000. In my code for app, I have this part of code for reading the paired of device.
private void getpaireddevices(){
Set<BluetoothDevice> devicesArray = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
if(devicesArray.size() > 0) {
for(BluetoothDevice device : devicesArray) {
device.getName();
device.getAddress();
}
}
}
In this moment, when I enable the BLE the app found the device, it connect but not works. For this to work I should exit and reconnect with my device. Why?
This is possible if the device is already bonded. Call removeBond() method to clear previous bonding state.
device.removeBond();
For check bonding state of BluetoothDevice, use getBondState().
Ble gatt connection success rate is different per device by device. You may need disconnect ble by hidden method, if your connection fails continuously.
Please read this:
BLE Device Bonding Remove Automatically in Android
The method unpairDevice() will unpair bluetooth connection.
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice bt : pairedDevices) {
if (bt.getName().contains("String you know has to be in device name")) {
unpairDevice(bt);
}
}
// Function to unpair from passed in device
private void unpairDevice(BluetoothDevice device) {
try {
Method m = device.getClass().getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
} catch (Exception e) { Log.e(TAG, e.getMessage()); }
}
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()
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());
}
};
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!