Android 特定时间后刷新蓝牙设备列表
Android refresh Bluetooth device list after specific time
我使用 Android developer guide for Bluetooth 列出所有附近的蓝牙设备,我使用以下代码。
// Create a BroadcastReceiver for ACTION_FOUND
private 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());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
更新视图。
ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
newDevicesListView.setAdapter(mArrayAdapter);
现在我想每 10 秒更新一次列表,我该怎么做。
任何帮助将不胜感激。
使用 Timer
和 TimerTask
(来自 Java)
首先你必须注册接收器
//THIS IS YOUR CODE
private 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());
//IMPORTANT: Do not remove lines below
mBluetoothAdapter.cancelDiscovery();
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
之后,执行此操作:
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Timer mTimer = new Timer();
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
//After cancel discovery, start discovery instead of wait another 10 seconds to re-execute this piece of code
mBluetoothAdapter.startDiscovery();
} else {
mBluetoothAdapter.startDiscovery();
}
}
});
//Timer delays 0 seconds (it will start immediately)
//Timer repeats every 10 seconds, as you said
//10000ms equals to 10s
mTimer.schedule(mTimerTask, 0, 10000);
}
};
我使用 Android developer guide for Bluetooth 列出所有附近的蓝牙设备,我使用以下代码。
// Create a BroadcastReceiver for ACTION_FOUND
private 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());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
更新视图。
ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
newDevicesListView.setAdapter(mArrayAdapter);
现在我想每 10 秒更新一次列表,我该怎么做。
任何帮助将不胜感激。
使用 Timer
和 TimerTask
(来自 Java)
首先你必须注册接收器
//THIS IS YOUR CODE
private 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());
//IMPORTANT: Do not remove lines below
mBluetoothAdapter.cancelDiscovery();
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
之后,执行此操作:
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Timer mTimer = new Timer();
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
//After cancel discovery, start discovery instead of wait another 10 seconds to re-execute this piece of code
mBluetoothAdapter.startDiscovery();
} else {
mBluetoothAdapter.startDiscovery();
}
}
});
//Timer delays 0 seconds (it will start immediately)
//Timer repeats every 10 seconds, as you said
//10000ms equals to 10s
mTimer.schedule(mTimerTask, 0, 10000);
}
};