こんにちは私は同じ問題を抱えていました。それを私が直した。
onCreate で、以下を追加します。
filter = new IntentFilter(BluetoothDevice.ACTION_NAME_CHANGED);
this.registerReceiver(mReceiver, filter);
次に、以下の BroadcastReceiver を編集します。
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("mReceiver","ACTION_FOUND:"+device.getAddress()+" :"+device.getName());
// If it's already paired, skip it, because it's been listed already
if(device.getName()==null)
{ //when name is null, skip
//But if you want to make lists asap, comment out this block.
}
else
{
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
//mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
boolean listed = false;
for (int i = 0; i < mNewDevicesArrayAdapter.getCount(); i++) {
String info = mNewDevicesArrayAdapter.getItem(i);
String address = info.substring(info.length() - 17);
if (device.getAddress().equals(address)) {
Log.d("mReceiver", "ACTION_FOUND: replace the item of lists");
mNewDevicesArrayAdapter.remove(info);
info = device.getName() + "\n" + device.getAddress();
mNewDevicesArrayAdapter.insert(info, i);
listed = true;
break;
}
}
if(listed==false)
{//if it is new device( not in lists), add it.
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
}
}else if(BluetoothDevice.ACTION_NAME_CHANGED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("mReceiver", "NAME_CHANGED:" + device.getAddress() + " :" + device.getName());
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
boolean listed = false;
for (int i = 0; i < mNewDevicesArrayAdapter.getCount(); i++) {
String info = mNewDevicesArrayAdapter.getItem(i);
String address = info.substring(info.length() - 17);
if (device.getAddress().equals(address)) {
Log.d("mReceiver", "NAME_CHANGED: replace the item of lists");
mNewDevicesArrayAdapter.remove(info);
info = device.getName() + "\n" + device.getAddress();
mNewDevicesArrayAdapter.insert(info, i);
listed = true;
break;
}
}
if(listed==false)
{//if it is new device( not in lists), add it.
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
};