In our previous example we have seen method to scan for nearby devices. In this blog we will extend the same example to remove the device explicitly.
In our previous example while exploring “StartDiscovery” method, we have seen “InterfacesRemoved” signal which will be emitted when any object path or interfaces inside a object path is removed.
We have also seen, bluetoothd will automatically remove the devices after 180 seconds which are populated during scanning process if the devices are not used. This does not apply for the devices which are paired or connected. Apart from this automatic removal, one can remove the device using “RemoveDevice” method. This method can be used any devices, i.e devices which are
- appeared during scanning but not used
- paired but not connected
- paired and connected
RemoveDevice takes an argument to the object path i.e /org/bluez/hciX/dev_XX_YY_ZZ_AA_BB_CC. In the above extended example, we are removing the device immediately after it’s apperance during the scanning process. So while handling “InterfacesAdded” we are printing the device information and removing it.
As we are also watching on “InterfacesRemoved” signal, device removal triggers this signal callback handler and which it turn prints the device MAC address which is removed and finally quits the main loop.
This means, we exiting our program once we find at least one device in range. If no devices are found, this program remains in indefinite scanning mode.