I\'ve got a Bluetooth device that has connected on all versions of Android that I have tried prior to 4.4.2. Now, it\'s not connecting on the Galaxy Tab 4 or the S3. The Tab
Try this code, this is working android 4.4.2 on nexus 7
private boolean refreshDeviceCache(BluetoothGatt gatt){
try {
BluetoothGatt localBluetoothGatt = gatt;
Method localMethod = localBluetoothGatt.getClass().getMethod("refresh", new Class[0]);
if (localMethod != null) {
boolean bool = ((Boolean) localMethod.invoke(localBluetoothGatt, new Object[0])).booleanValue();
return bool;
}
}
catch (Exception localException) {
Log.e(TAG, "An exception occured while refreshing device");
}
return false;
}
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG,"BluetoothAdapter not initialized or unspecified address.");
return false;
}
// Previously connected device. Try to reconnect.
if (mBluetoothGatt != null) {
Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGatt.connect()) {
return true;
} else {
return false;
}
}
final BluetoothDevice device = mBluetoothAdapter
.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to directly connect to the device, so we are setting the
// autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(MyApp.getContext(), false, mGattCallback));
refreshDeviceCache(mBluetoothGatt);
Log.d(TAG, "Trying to create a new connection.");
return true;
}
I have also used connection steps using the same code but that could work only for Smart Android device.
you can get the BluetoothDevice object of the particular device you want to connect. after that you can use BluetoothHeadset's connect and disconnect method via reflection to connect to the perticular BluetoothDevice.
Class<?> BTHeadset = Class.forName("android.bluetooth.BluetoothHeadset");
Method conn = BTHeadset.getMethod("connect", new Class[] {BluetoothDevice.class});
conn.invoke(BTHeadset, new Object[] {btDev});
same code you can use for disconneting a device using disconnect method via reflection.