我终于解决了这个问题 . 以前我从“BluetoothAdpter”类获得了所有绑定设备的“getBondedDevices()”方法 . 但我通过使用“BluetoothProfile”类中的“getConnectedDevices”方法解决了这个问题 .
我的新代码如下,其中仅显示连接的蓝牙耳机设备名称,该名称仅连接到HEADSET配置文件 .
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.getProfileProxy(this, listener, BluetoothProfile.HEADSET);
public final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int i, final BluetoothProfile bluetoothProfile) {
final TextView txt = (TextView) findViewById(R.id.textView);
List b = bluetoothProfile.getConnectedDevices();
StringBuilder stringBuilder = new StringBuilder();
for(BluetoothDevice getConnectedDevice : b){
stringBuilder.append(getConnectedDevice.getName());
}
txt.setText(stringBuilder);
}
@Override
public void onServiceDisconnected(int i) {
final TextView txt = (TextView) findViewById(R.id.textView);
txt.setText(String.valueOf(i));
}
};