简体   繁体   中英

Monitoring the connection of a paired bluetooth device

I have written the code which returns me a list of all the paired bluetooth devices of a android device.One among the list of paired bluetooth devices is my laptop as well.

Now my question is

Is there a way I can monitoring the bluetooth connectivity status between the laptop and the android device.The output which I need is "Connected" if there is a bluetooth connection present and "Disconnected" if there is no bluetooth connection


The android version is 2.1.Eclair

Before I proceed further , I have some questions.

->Should I test this application when I connect the laptop and device through USB? ->How to run it on a separate thread or from an AsyncTask? ->The above code is not working for me if I connect the device and the laptop through USB and then launch the application. Even though the laptop and the phone are nearby and they are paired, I am not able to see a connected status in the phone.

The code which I have written is as follows

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_);

    out = (TextView) findViewById(R.id.textView1);

    // Getting the Bluetooth adapter
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    // out.append("\nAdapter: " + adapter);

    // Check for Bluetooth support in the first place
    // Emulator doesn't support Bluetooth and will return null
    if (adapter == null) {
        out.append("\nBluetooth NOT supported. Aborting.");
        return;
    }

    // Starting the device discovery
    out.append("\nStarting discovery...");
    adapter.startDiscovery();
    out.append("\nDone with discovery...");

    // Listing paired devices
    out.append("\nDevices Pared:");
    Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter()
            .getBondedDevices();

    for (BluetoothDevice device : devices) {
        out.append("\nFound device: " + device);
        out.append("\n The name is: " + device.getName());
        out.append("\n The type is: "
                + device.getBluetoothClass().getDeviceClass());

        Method m;
        try {
            System.out.println("Trying to connect to " + device.getName());
            m = device.getClass().getMethod("createInsecureRfcommSocket",
                    new Class[] { int.class });
            BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
            socket.connect();
            out.append("Connected");
        } catch (Exception e) {
            e.printStackTrace();
            out.append("\nDisconnected");
        }
    }
}

Which android version you are using?

You must have the permission on your AndroidManifest.xml:

<uses-permission android:name="android.permission.BLUETOOTH" />

You can try the code below, but be sure to run it on a separate thread or from an AsyncTask or your UI can hang up. This will check for your paired devices and try to connect to each one. If it is successful then it prints Connected on logcat or else it prints Disconnected .

private void checkPairedPrinters() throws IOException {
        Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
        for (BluetoothDevice device : devices) {
            Method m;
            try {
                System.out.println("Trying to connect to " + device.getName());
                m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
                BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
                socket.connect();
                System.out.println("Connected");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Disconnected");
            }
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM