Problem while disconnecting BLE device

0 votes

I'm running Android Things on my Pi and I connected it with a Bluetooth Low-Energy device using the method 'connectGatt()'. But I just can't seem to disconnect it!
I'm finally forced to kill the application and when I reopen it to enable the broadcasting mode for visibility, it tells me that the app was not disconnected properly.

I'm sharing my code snippet for the disconnectivity of the device if anybody could tell me if I've missed out on something or done some mistakes in it:

private void disconnectDevice() {
    gatt.disconnect();
}
//I've closed the connection here

private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                Log.d("BLED-GATT", "STATE_CONNECTED");
                setUIConnectionStatus("Discover services on device...", Color.YELLOW);
                checkEnableAddButton(simpleTrackEditText.getText().toString());
                gatt.discoverServices();
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                Log.d("BLED-GATT", "STATE_DISCONNECTED");
                setUIConnectionStatus("Not Connected!", Color.RED);
                gatt.close();
                break;
            default:
                Log.d("BLED-GATT", "STATE_OTHER");
        }
    }
}

The entire things executes but neither does my broadcasting mode change and nor does my device disconnect. 

Please Help! 

Thanks in Advance. :)  

Jul 18, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
3,710 views

1 answer to this question.

0 votes

I think that's because of the way they've paired. And, everytime you reopen, the broadcasting mode simultaneously attempts connecting to that same device, again and again, making your application have too many connections open at the same time and delaying the search for new devices. 
I have a similar code running for mine. I've just added a "!isConnected()". Try this: 

/**
 * Connects to the device. Does nothing if already connected.
 * @param macAddress the address of the device.
 */
private void connectDevice(String macAddress) {
    if (!isConnected()) {
        device = bluetoothAdapter.getRemoteDevice(macAddress);

        if (device == null) {
            this.sendToast("Device Not Available");
        } else {
            Log.d("BLED", "Connecting...");
            gatt = device.connectGatt(this, true, gattCallback);
        }
    }
}
Lemme know if it solved your problem!

answered Jul 18, 2018 by nirvana
• 3,130 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Customize menu based on BLE device Authentication

You may dynamically change contents of the ...READ MORE

answered Aug 7, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
639 views
0 votes
1 answer
0 votes
1 answer

Publishing commands to device in IBM IoT using MQTT in Java

If you are publishing from an application, ...READ MORE

answered Jul 27, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,506 views
0 votes
1 answer
0 votes
1 answer

Android Ble GATT_ERROR 133 getting often with samsung devices

I tried the following code #!/usr/bin/python import ibmiotf.device from time ...READ MORE

answered Jul 18, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
2,456 views
0 votes
1 answer

Java No Class Def Found error

I solved this issue by updating my ...READ MORE

answered Oct 8, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,496 views
0 votes
1 answer

Use of .driver-metadata file in Android Things?

If I'm not wrong, it could be ...READ MORE

answered Oct 12, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
632 views
0 votes
1 answer

Detecting BLE beacons that are not ibeacons on IOS

You'll have to understand that CoreLocation is ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
875 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP