Итак, я создаю приложение, которое сканирует ближайшие устройства bluetooth. Теперь эта часть работает отлично. Дело в том, что я хочу, чтобы он остановил процесс обнаружения через 30 секунд. Как мне этого добиться?
Вот мой код:
Широковещательный приемник:
private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d(TAG,"onReceive: Action Found");
if(action.equals(BluetoothDevice.ACTION_FOUND))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "onReceive: " + device.getName() + ": " + device.getAddress());
if(device.getName() != null)
{
//System.out.println("JUNGE ER FINDET DOCH SOGAR DAS DEVICE WIESO ZEIGT ER DIE SCHEIßE NICHT AN");
//Jetzt wo keine nullpointer Exception mehr ent
if(device.getName().contains("HC")) {
mBTDevices.add(device);
mDeviceListAdapter = new DeviceListAdapter(context, R.layout.device_adapter_view, mBTDevices);
lvNewDevices.setAdapter((mDeviceListAdapter));
}
}
}
/*else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.v(TAG, "Entered the Finished ");
System.out.println("Entered the Finished");
}
*/
}
} ;
Метод обнаружения (щелчок, когда я нажимаю кнопку обнаружения)
public void btnDiscover(View view)
{
Log.d(TAG,"btnDiscover: Looking for unpaired Devices");
if(mBluetoothAdapter.isDiscovering())
{
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG,"btnDiscover:Cancelling discovery.");
//Checkt ob die Berechtigungen im Manifest für BT vorliegen
checkBTPermissions();
//start discovery again
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3,discoverDevicesIntent);
}
if(!(mBluetoothAdapter.isDiscovering()))
{
//another check
checkBTPermissions();
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3,discoverDevicesIntent);
}
}