我正在使用来自 Android 开发者网站的代码来检测范围内的蓝牙设备,并将它们添加到 ArrayAdapter。问题是,每个设备都被添加到 ArrayAdapter 5-6 次。现在,我只是使用这里的代码:http: //developer.android.com/guide/topics/connectivity/bluetooth.html#DiscoveringDevices
这是我所拥有的:
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
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);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
知道是什么原因造成的吗?我该怎么做才能让设备只添加一次到 ArrayAdapter,而不是 5 次?