I am developing an application where I have to connect to Bluetooth device on Android 4.3.
And I want to use getConnectedDevices()
like the following code to get the connected Device after connect BLE device using by mBluetoothGatt.connect()
, but is not working.
@Override
public void onServiceDisconnected(int profile) {
// TODO Auto-generated method stub
Log.v(TAG, "profile1" + profile);
Log.v(TAG, "BluetoothProfile.HEADSET1 = " + BluetoothProfile.HEADSET);
if(profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
// TODO Auto-generated method stub
Log.v(TAG, "profile2 " + profile);
Log.v(TAG, "BluetoothProfile.HEADSET2 = " + BluetoothProfile.HEADSET);
if(profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
Log.v(TAG, "mBluetoothHeadset = " + mBluetoothHeadset);
List<BluetoothDevice> devicelist = mBluetoothHeadset.getConnectedDevices();
Log.v(TAG, "mBluetoothHeadset.getConnectedDevices = " + mBluetoothHeadset.getConnectedDevices());
Log.v(TAG, "devicelist = " + devicelist);
for(BluetoothDevice dev : devicelist) {
Log.v(TAG, "connectedDevices = " + dev);
}
}
}
};
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
setIntent(intent);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.getProfileProxy(Main.this, mProfileListener, BluetoothProfile.HEADSET);
}
and seen from the log , it didn't call the function getConnectedDevices(); The Log is:
D/BluetoothHeadset( 3454): Proxy object connected
V/Main ( 3454): profile2 = 1
V/Main ( 3454): BluetoothProfile.HEADSET2 = 1
V/Main ( 3454): mBluetoothHeadset = android.bluetooth.BluetoothHeadset@4a99cddc
V/Main ( 3454): mBluetoothHeadset.getConnectedDevices = []
V/Main ( 3454): devicelist = []
The list of connected is empty , and I'm sure I have one connected BLE device. Does it something wrong in my code ??