You can follow the sequence of steps and create an asynchronous task to check the connection when searching and sharing files between devices.
The deviceDiscovered() and inquiryCompleted() functions are those that execute when the device is found and when the request is complete.
If the device is disconnected, you will receive a notification from the inqueryCompleted method and process it in the line:
try { synchronized(lock){ lock.wait(); } } catch (InterruptedException e) { e.printStackTrace(); }
In InterruptedException you can handle the error.
package bt; import java.io.OutputStream; import java.util.ArrayList; import javax.bluetooth.DataElement; import javax.bluetooth.DeviceClass; import javax.bluetooth.DiscoveryAgent; import javax.bluetooth.DiscoveryListener; import javax.bluetooth.LocalDevice; import javax.bluetooth.RemoteDevice; import javax.bluetooth.ServiceRecord; import javax.bluetooth.UUID; import javax.microedition.io.Connector; import javax.obex.ClientSession; import javax.obex.HeaderSet; import javax.obex.Operation; import javax.obex.ResponseCodes; import bt.BTListener.MyDeviceListenerFilter; public class MyDiscoveryListener implements DiscoveryListener{ private static Object lock=new Object(); public ArrayList<RemoteDevice> devices; public MyDiscoveryListener() { devices = new ArrayList<RemoteDevice>(); } public static void main(String[] args) { MyDiscoveryListener listener = new MyDiscoveryListener(); try{ LocalDevice localDevice = LocalDevice.getLocalDevice(); DiscoveryAgent agent = localDevice.getDiscoveryAgent(); agent.startInquiry(DiscoveryAgent.GIAC, listener); try { synchronized(lock){ lock.wait(); } } catch (InterruptedException e) { e.printStackTrace(); return; } System.out.println("Device Inquiry Completed. "); UUID[] uuidSet = new UUID[1]; uuidSet[0]=new UUID(0x1105);
source share