Java Code Examples for android.bluetooth.BluetoothManager#getConnectedDevices()

The following examples show how to use android.bluetooth.BluetoothManager#getConnectedDevices() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BTDeviceTracker.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
BTDeviceTracker(Context context, BTDeviceUSourceData data,
                @NonNull PendingIntent event_positive,
                @NonNull PendingIntent event_negative) {
    super(context, data, event_positive, event_negative);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        for (int profile : new int[]{BluetoothProfile.GATT, BluetoothProfile.GATT_SERVER}) {
            for (BluetoothDevice btDevice : bluetoothManager.getConnectedDevices(profile)) {
                if (is_target(btDevice)) {
                    matched_devices++;
                }
            }
        }
    }
    determine_satisfied();
}
 
Example 2
Source File: BtReconnect.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isConnectedToDevice(final String mac) {
    UserError.Log.d(TAG, "isConnected to device: " + mac);
    if (JoH.emptyString(mac)) {
        return false;
    }
    final BluetoothManager bluetoothManager = (BluetoothManager) xdrip.getAppContext().getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        return false;
    }
    boolean foundConnectedDevice = false;
    UserError.Log.d(TAG, "isConnected to device iterate: " + mac);
    for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
        //UserError.Log.d(TAG, "Connected device: " + bluetoothDevice.getAddress() + " " + bluetoothDevice.getName());
        if (bluetoothDevice.getAddress().equalsIgnoreCase(mac)) {
            foundConnectedDevice = true;
            break;
        }
    }
    return foundConnectedDevice;
}
 
Example 3
Source File: JamBaseBluetoothSequencer.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isConnectedToDevice(final String mac) {
    if (JoH.emptyString(mac)) {
        return false;
    }
    final BluetoothManager bluetoothManager = (BluetoothManager) xdrip.getAppContext().getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        return false;
    }
    boolean foundConnectedDevice = false;
    for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
        if (bluetoothDevice.getAddress().equalsIgnoreCase(mac)) {
            foundConnectedDevice = true;
            break;
        }
    }
    return foundConnectedDevice;
}
 
Example 4
Source File: JamBaseBluetoothSequencer.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isConnectedToDevice(final String mac) {
    if (JoH.emptyString(mac)) {
        return false;
    }
    final BluetoothManager bluetoothManager = (BluetoothManager) xdrip.getAppContext().getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        return false;
    }
    boolean foundConnectedDevice = false;
    for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
        if (bluetoothDevice.getAddress().equalsIgnoreCase(mac)) {
            foundConnectedDevice = true;
            break;
        }
    }
    return foundConnectedDevice;
}
 
Example 5
Source File: BtReconnect.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isConnectedToDevice(final String mac) {
    UserError.Log.d(TAG, "isConnected to device: " + mac);
    if (JoH.emptyString(mac)) {
        return false;
    }
    final BluetoothManager bluetoothManager = (BluetoothManager) xdrip.getAppContext().getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        return false;
    }
    boolean foundConnectedDevice = false;
    UserError.Log.d(TAG, "isConnected to device iterate: " + mac);
    for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
        //UserError.Log.d(TAG, "Connected device: " + bluetoothDevice.getAddress() + " " + bluetoothDevice.getName());
        if (bluetoothDevice.getAddress().equalsIgnoreCase(mac)) {
            foundConnectedDevice = true;
            break;
        }
    }
    return foundConnectedDevice;
}
 
Example 6
Source File: BleRequestImpl.java    From Android-BLE with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @return 已经连接的设备集合
 */
public List<BluetoothDevice> getConnectedDevices() {
    BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager != null){
        return bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
    }
    return null;
}
 
Example 7
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public void attemptConnection() {
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (mBluetoothManager != null) {
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        if (mBluetoothAdapter != null) {
            if (device != null) {
                mConnectionState = STATE_DISCONNECTED;
                for (BluetoothDevice bluetoothDevice : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
                    if (bluetoothDevice.getAddress().compareTo(device.getAddress()) == 0) {
                        mConnectionState = STATE_CONNECTED;
                    }
                }
            }

            Log.w(TAG, "Connection state: " + mConnectionState);
            if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
                ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
                if (btDevice != null) {
                    mDeviceName = btDevice.name;
                    mDeviceAddress = btDevice.address;
                    if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(mDeviceAddress) != null) {
                        connect(mDeviceAddress);
                        return;
                    }
                }
            } else if (mConnectionState == STATE_CONNECTED) { //WOOO, we are good to go, nothing to do here!
                Log.w(TAG, "Looks like we are already connected, going to read!");
                return;
            }
        }
    }
    setRetryTimer();
}
 
Example 8
Source File: DexCollectionService.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
public void attemptConnection() {
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        setRetryTimer();
        return;
    }

    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        setRetryTimer();
        return;
    }

    if (device != null) {
        mConnectionState = STATE_DISCONNECTED;
        for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
            if (bluetoothDevice.getAddress().compareTo(device.getAddress()) == 0) {
                mConnectionState = STATE_CONNECTED;
            }
        }
    }

    Log.i(TAG, "attemptConnection: Connection state: " + getStateStr(mConnectionState));
    if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
        ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
        if (btDevice != null) {
            String deviceAddress = btDevice.address;
            if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(deviceAddress) != null) {
                connect(deviceAddress);
                return;
            }
        }
    } else if (mConnectionState == STATE_CONNECTED) { //WOOO, we are good to go, nothing to do here!
        Log.i(TAG, "attemptConnection: Looks like we are already connected, going to read!");
        return;
    }

    setRetryTimer();
}
 
Example 9
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
synchronized void checkConnection() {
    status("Attempting connection");
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        status("No bluetooth manager");
        setRetryTimer();
        return;
    }

    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        status("No bluetooth adapter");
        setRetryTimer();
        return;
    }

    if (!mBluetoothAdapter.isEnabled()) {
        mConnectionState = STATE_DISCONNECTED; // can't be connected if BT is disabled
        if (Pref.getBoolean("automatically_turn_bluetooth_on", true)) {
            Log.i(TAG, "Turning bluetooth on as appears disabled");
            status("Turning bluetooth on");
            JoH.setBluetoothEnabled(getApplicationContext(), true);
        } else {
            Log.d(TAG, "Not automatically turning on bluetooth due to preferences");
        }
    }

    if (device != null) {
        boolean found = false;
        for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
            if (bluetoothDevice.getAddress().equals(device.getAddress())) {
                found = true;
                if (mConnectionState != STATE_CONNECTED) {
                    UserError.Log.d(TAG, "Detected state change by checking connected devices");
                    handleConnectedStateChange();
                }
                break;
            }
        }
        if (!found) {
            if (mConnectionState == STATE_CONNECTED) {
                UserError.Log.d(TAG, "Marking disconnected as not in list of connected devices");
                mConnectionState = STATE_DISCONNECTED; // not in connected list so should be disconnected we think
            }

        }
    } else {
        UserError.Log.d(TAG, "Device is null in checkConnection");
        mConnectionState = STATE_DISCONNECTED; // can't be connected if we don't know the device
    }

    Log.i(TAG, "checkConnection: Connection state: " + getStateStr(mConnectionState));
    if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
        final ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
        if (btDevice != null) {
            final String deviceAddress = btDevice.address;
            mDeviceAddress = deviceAddress;
            try {
                if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(deviceAddress) != null) {
                    if (useScanning()) {
                        status(gs(R.string.scanning) + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        scanMeister.setAddress(deviceAddress).addCallBack(this, TAG).scan();
                    } else {
                        status("Connecting" + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        connect(deviceAddress);
                    }
                    mStaticState = mConnectionState;
                    return;
                }
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "IllegalArgumentException: " + e);
            }
        }
    } else if (mConnectionState == STATE_CONNECTING) {
        mStaticState = mConnectionState;
        if (JoH.msSince(last_connect_request) > (getTrustAutoConnect() ? Constants.SECOND_IN_MS * 3600 : Constants.SECOND_IN_MS * 30)) {
            Log.i(TAG, "Connecting for too long, shutting down");
            retry_backoff = 0;
            close();
        }
    } else if (mConnectionState == STATE_CONNECTED) { //WOOO, we are good to go, nothing to do here!
        status("Last Connected");
        Log.i(TAG, "checkConnection: Looks like we are already connected, ready to receive");
        retry_backoff = 0;
        mStaticState = mConnectionState;
        if (use_polling && (JoH.msSince(lastPacketTime) >= POLLING_PERIOD)) {
            pollForData();
        }

        return;
    }
    setRetryTimer();
}
 
Example 10
Source File: DexCollectionService.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
synchronized void checkConnection() {
    status("Attempting connection");
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        status("No bluetooth manager");
        setRetryTimer();
        return;
    }

    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        status("No bluetooth adapter");
        setRetryTimer();
        return;
    }

    if (!mBluetoothAdapter.isEnabled()) {
        mConnectionState = STATE_DISCONNECTED; // can't be connected if BT is disabled
        if (Pref.getBoolean("automatically_turn_bluetooth_on", true)) {
            Log.i(TAG, "Turning bluetooth on as appears disabled");
            status("Turning bluetooth on");
            JoH.setBluetoothEnabled(getApplicationContext(), true);
        } else {
            Log.d(TAG, "Not automatically turning on bluetooth due to preferences");
        }
    }

    if (device != null) {
        boolean found = false;
        for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
            if (bluetoothDevice.getAddress().equals(device.getAddress())) {
                found = true;
                if (mConnectionState != STATE_CONNECTED) {
                    UserError.Log.d(TAG, "Detected state change by checking connected devices");
                    handleConnectedStateChange();
                }
                break;
            }
        }
        if (!found) {
            if (mConnectionState == STATE_CONNECTED) {
                UserError.Log.d(TAG, "Marking disconnected as not in list of connected devices");
                mConnectionState = STATE_DISCONNECTED; // not in connected list so should be disconnected we think
            }

        }
    } else {
        UserError.Log.d(TAG, "Device is null in checkConnection");
        mConnectionState = STATE_DISCONNECTED; // can't be connected if we don't know the device
    }

    Log.i(TAG, "checkConnection: Connection state: " + getStateStr(mConnectionState));
    if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
        final ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
        if (btDevice != null) {
            final String deviceAddress = btDevice.address;
            mDeviceAddress = deviceAddress;
            try {
                if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(deviceAddress) != null) {
                    if (useScanning()) {
                        status(gs(R.string.scanning) + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        scanMeister.setAddress(deviceAddress).addCallBack(this, TAG).scan();
                    } else {
                        status("Connecting" + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        connect(deviceAddress);
                    }
                    mStaticState = mConnectionState;
                    return;
                }
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "IllegalArgumentException: " + e);
            }
        }
    } else if (mConnectionState == STATE_CONNECTING) {
        mStaticState = mConnectionState;
        if (JoH.msSince(last_connect_request) > (getTrustAutoConnect() ? Constants.SECOND_IN_MS * 3600 : Constants.SECOND_IN_MS * 30)) {
            Log.i(TAG, "Connecting for too long, shutting down");
            retry_backoff = 0;
            close();
        }
    } else if (mConnectionState == STATE_CONNECTED) { //WOOO, we are good to go, nothing to do here!
        status("Last Connected");
        Log.i(TAG, "checkConnection: Looks like we are already connected, ready to receive");
        retry_backoff = 0;
        mStaticState = mConnectionState;
        if (use_polling && (JoH.msSince(lastPacketTime) >= POLLING_PERIOD)) {
            pollForData();
        }

        return;
    }
    setRetryTimer();
}
 
Example 11
Source File: DexShareCollectionService.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public void attemptConnection() {
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (mBluetoothManager != null) {
        if (device != null) {
            mConnectionState = STATE_DISCONNECTED;
            for (BluetoothDevice bluetoothDevice : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
                if (bluetoothDevice.getAddress().compareTo(device.getAddress()) == 0) {
                    mConnectionState = STATE_CONNECTED;
                }
            }
        }
        Log.i(TAG, "Connection state: " + mConnectionState);
        if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
            ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
            if (btDevice != null) {
                mDeviceName = btDevice.name;
                mDeviceAddress = btDevice.address;
                mBluetoothAdapter = mBluetoothManager.getAdapter();
                try {
                    if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(mDeviceAddress) != null) {
                        connect(mDeviceAddress);
                        return;
                    } else {
                        Log.w(TAG, "Bluetooth is disabled or BT device cant be found");
                        setRetryTimer();
                        return;
                    }
                } catch (IllegalArgumentException e) {
                    if (JoH.ratelimit("dex-share-error-log", 180)) {
                        Log.wtf(TAG, "Error connecting: " + e);
                    }
                }
            } else {
                Log.w(TAG, "No bluetooth device to try and connect to");
                setRetryTimer();
                return;
            }
        } else if (mConnectionState == STATE_CONNECTED) {
            Log.i(TAG, "Looks like we are already connected, going to read!");
            attemptRead();
            return;
        } else {
            setRetryTimer();
            return;
        }
    } else {
        setRetryTimer();
        return;
    }
}
 
Example 12
Source File: DexCollectionService.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
synchronized void checkConnection() {
    status("Attempting connection");
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        status("No bluetooth manager");
        setRetryTimer();
        return;
    }

    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        status("No bluetooth adapter");
        setRetryTimer();
        return;
    }

    if (!mBluetoothAdapter.isEnabled()) {
        mConnectionState = STATE_DISCONNECTED; // can't be connected if BT is disabled
        if (Pref.getBoolean("automatically_turn_bluetooth_on", true)) {
            Log.i(TAG, "Turning bluetooth on as appears disabled");
            status("Turning bluetooth on");
            JoH.setBluetoothEnabled(getApplicationContext(), true);
        } else {
            Log.d(TAG, "Not automatically turning on bluetooth due to preferences");
        }
    }

    if (device != null) {
        boolean found = false;
        for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
            if (bluetoothDevice.getAddress().equals(device.getAddress())) {
                found = true;
                if (mConnectionState != STATE_CONNECTED) {
                    UserError.Log.d(TAG, "Detected state change by checking connected devices");
                    handleConnectedStateChange();
                }
                break;
            }
        }
        if (!found) {
            if (mConnectionState == STATE_CONNECTED) {
                UserError.Log.d(TAG, "Marking disconnected as not in list of connected devices");
                mConnectionState = STATE_DISCONNECTED; // not in connected list so should be disconnected we think
            }

        }
    } else {
        UserError.Log.d(TAG, "Device is null in checkConnection");
        mConnectionState = STATE_DISCONNECTED; // can't be connected if we don't know the device
    }

    Log.i(TAG, "checkConnection: Connection state: " + getStateStr(mConnectionState));
    if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
        final ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
        if (btDevice != null) {
            final String deviceAddress = btDevice.address;
            mDeviceAddress = deviceAddress;
            try {
                if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(deviceAddress) != null) {
                    if (useScanning()) {
                        status(gs(R.string.scanning) + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        scanMeister.setAddress(deviceAddress).addCallBack(this, TAG).scan();
                    } else {
                        status("Connecting" + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        connect(deviceAddress);
                    }
                    mStaticState = mConnectionState;
                    return;
                }
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "IllegalArgumentException: " + e);
            }
        }
    } else if (mConnectionState == STATE_CONNECTING) {
        mStaticState = mConnectionState;
        if (JoH.msSince(last_connect_request) > (getTrustAutoConnect() ? Constants.SECOND_IN_MS * 3600 : Constants.SECOND_IN_MS * 30)) {
            Log.i(TAG, "Connecting for too long, shutting down");
            retry_backoff = 0;
            close();
        }
    } else if (mConnectionState == STATE_CONNECTED) { //WOOO, we are good to go, nothing to do here!
        status("Last Connected");
        Log.i(TAG, "checkConnection: Looks like we are already connected, ready to receive");
        retry_backoff = 0;
        mStaticState = mConnectionState;
        if (use_polling && (JoH.msSince(lastPacketTime) >= POLLING_PERIOD)) {
            pollForData();
        }

        return;
    }
    setRetryTimer();
}
 
Example 13
Source File: DexShareCollectionService.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public void attemptConnection() {
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (mBluetoothManager != null) {
        if (device != null) {
            mConnectionState = STATE_DISCONNECTED;
            for (BluetoothDevice bluetoothDevice : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
                if (bluetoothDevice.getAddress().compareTo(device.getAddress()) == 0) {
                    mConnectionState = STATE_CONNECTED;
                }
            }
        }
        Log.i(TAG, "Connection state: " + mConnectionState);
        if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
            ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
            if (btDevice != null) {
                mDeviceName = btDevice.name;
                mDeviceAddress = btDevice.address;
                mBluetoothAdapter = mBluetoothManager.getAdapter();
                try {
                    if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(mDeviceAddress) != null) {
                        connect(mDeviceAddress);
                        return;
                    } else {
                        Log.w(TAG, "Bluetooth is disabled or BT device cant be found");
                        setRetryTimer();
                        return;
                    }
                } catch (IllegalArgumentException e) {
                    if (JoH.ratelimit("dex-share-error-log", 180)) {
                        Log.wtf(TAG, "Error connecting: " + e);
                    }
                }
            } else {
                Log.w(TAG, "No bluetooth device to try and connect to");
                setRetryTimer();
                return;
            }
        } else if (mConnectionState == STATE_CONNECTED) {
            Log.i(TAG, "Looks like we are already connected, going to read!");
            attemptRead();
            return;
        } else {
            setRetryTimer();
            return;
        }
    } else {
        setRetryTimer();
        return;
    }
}
 
Example 14
Source File: DexShareCollectionService.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public void attemptConnection() {
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (mBluetoothManager != null) {
        if (device != null) {
            mConnectionState = STATE_DISCONNECTED;
            for (BluetoothDevice bluetoothDevice : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
                if (bluetoothDevice.getAddress().compareTo(device.getAddress()) == 0) {
                    mConnectionState = STATE_CONNECTED;
                }
            }
        }
        Log.i(TAG, "Connection state: " + mConnectionState);
        if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
            ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
            if (btDevice != null) {
                mDeviceName = btDevice.name;
                mDeviceAddress = btDevice.address;
                mBluetoothAdapter = mBluetoothManager.getAdapter();
                if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(mDeviceAddress) != null) {
                    connect(mDeviceAddress);
                    return;
                } else {
                    Log.w(TAG, "Bluetooth is disabled or BT device cant be found");
                    setRetryTimer();
                    return;
                }
            } else {
                Log.w(TAG, "No bluetooth device to try and connect to");
                setRetryTimer();
                return;
            }
        } else if (mConnectionState == STATE_CONNECTED) {
            Log.i(TAG, "Looks like we are already connected, going to read!");
            attemptRead();
            return;
        } else {
            setRetryTimer();
            return;
        }
    } else {
        setRetryTimer();
        return;
    }
}
 
Example 15
Source File: DexShareCollectionService.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public void attemptConnection() {
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (mBluetoothManager != null) {
        if (device != null) {
            mConnectionState = STATE_DISCONNECTED;
            for (BluetoothDevice bluetoothDevice : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
                if (bluetoothDevice.getAddress().compareTo(device.getAddress()) == 0) {
                    mConnectionState = STATE_CONNECTED;
                }
            }
        }
        Log.i(TAG, "Connection state: " + mConnectionState);
        if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
            ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
            if (btDevice != null) {
                mDeviceName = btDevice.name;
                mDeviceAddress = btDevice.address;
                mBluetoothAdapter = mBluetoothManager.getAdapter();
                try {
                    if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(mDeviceAddress) != null) {
                        connect(mDeviceAddress);
                        return;
                    } else {
                        Log.w(TAG, "Bluetooth is disabled or BT device cant be found");
                        setRetryTimer();
                        return;
                    }
                } catch (IllegalArgumentException e) {
                    if (JoH.ratelimit("dex-share-error-log", 180)) {
                        Log.wtf(TAG, "Error connecting: " + e);
                    }
                }
            } else {
                Log.w(TAG, "No bluetooth device to try and connect to");
                setRetryTimer();
                return;
            }
        } else if (mConnectionState == STATE_CONNECTED) {
            Log.i(TAG, "Looks like we are already connected, going to read!");
            attemptRead();
            return;
        } else {
            setRetryTimer();
            return;
        }
    } else {
        setRetryTimer();
        return;
    }
}
 
Example 16
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
synchronized void checkConnection() {
    status("Attempting connection");
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        status("No bluetooth manager");
        setRetryTimer();
        return;
    }

    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        status("No bluetooth adapter");
        setRetryTimer();
        return;
    }

    if (!mBluetoothAdapter.isEnabled()) {
        mConnectionState = STATE_DISCONNECTED; // can't be connected if BT is disabled
        if (Pref.getBoolean("automatically_turn_bluetooth_on", true)) {
            Log.i(TAG, "Turning bluetooth on as appears disabled");
            status("Turning bluetooth on");
            JoH.setBluetoothEnabled(getApplicationContext(), true);
        } else {
            Log.d(TAG, "Not automatically turning on bluetooth due to preferences");
        }
    }

    if (device != null) {
        boolean found = false;
        for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
            if (bluetoothDevice.getAddress().equals(device.getAddress())) {
                found = true;
                if (mConnectionState != STATE_CONNECTED) {
                    UserError.Log.d(TAG, "Detected state change by checking connected devices");
                    handleConnectedStateChange();
                }
                break;
            }
        }
        if (!found) {
            if (mConnectionState == STATE_CONNECTED) {
                UserError.Log.d(TAG, "Marking disconnected as not in list of connected devices");
                mConnectionState = STATE_DISCONNECTED; // not in connected list so should be disconnected we think
            }

        }
    } else {
        UserError.Log.d(TAG, "Device is null in checkConnection");
        mConnectionState = STATE_DISCONNECTED; // can't be connected if we don't know the device
    }

    Log.i(TAG, "checkConnection: Connection state: " + getStateStr(mConnectionState));
    if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
        final ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
        if (btDevice != null) {
            final String deviceAddress = btDevice.address;
            mDeviceAddress = deviceAddress;
            try {
                if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(deviceAddress) != null) {
                    if (useScanning()) {
                        status(gs(R.string.scanning) + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        scanMeister.setAddress(deviceAddress).addCallBack(this, TAG).scan();
                    } else {
                        status("Connecting" + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        connect(deviceAddress);
                    }
                    mStaticState = mConnectionState;
                    return;
                }
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "IllegalArgumentException: " + e);
            }
        }
    } else if (mConnectionState == STATE_CONNECTING) {
        mStaticState = mConnectionState;
        if (JoH.msSince(last_connect_request) > (getTrustAutoConnect() ? Constants.SECOND_IN_MS * 3600 : Constants.SECOND_IN_MS * 30)) {
            Log.i(TAG, "Connecting for too long, shutting down");
            retry_backoff = 0;
            close();
        }
    } else if (mConnectionState == STATE_CONNECTED) { //WOOO, we are good to go, nothing to do here!
        status("Last Connected");
        Log.i(TAG, "checkConnection: Looks like we are already connected, ready to receive");
        retry_backoff = 0;
        mStaticState = mConnectionState;
        if (use_polling && (JoH.msSince(lastPacketTime) >= POLLING_PERIOD)) {
            pollForData();
        }

        return;
    }
    setRetryTimer();
}
 
Example 17
Source File: DexShareCollectionService.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public void attemptConnection() {
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (mBluetoothManager != null) {
        if (device != null) {
            mConnectionState = STATE_DISCONNECTED;
            for (BluetoothDevice bluetoothDevice : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
                if (bluetoothDevice.getAddress().compareTo(device.getAddress()) == 0) {
                    mConnectionState = STATE_CONNECTED;
                }
            }
        }
        Log.w(TAG, "Connection state: " + mConnectionState);
        if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
            ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
            if (btDevice != null) {
                mDeviceName = btDevice.name;
                mDeviceAddress = btDevice.address;
                mBluetoothAdapter = mBluetoothManager.getAdapter();
                if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(mDeviceAddress) != null) {
                    connect(mDeviceAddress);
                    return;
                } else {
                    Log.w(TAG, "Bluetooth is disabled or BT device cant be found");
                    setRetryTimer();
                    return;
                }
            } else {
                Log.w(TAG, "No bluetooth device to try and connect to");
                setRetryTimer();
                return;
            }
        } else if (mConnectionState == STATE_CONNECTED) {
            Log.w(TAG, "Looks like we are already connected, going to read!");
            attemptRead();
            return;
        } else {
            setRetryTimer();
            return;
        }
    } else {
        setRetryTimer();
        return;
    }
}
 
Example 18
Source File: DeviceServicesActivity.java    From EFRConnect-android with Apache License 2.0 4 votes vote down vote up
private List<BluetoothDevice> getConnectedBluetoothDevices() {
    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    return bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
}
 
Example 19
Source File: BrowserActivity.java    From EFRConnect-android with Apache License 2.0 4 votes vote down vote up
private List<BluetoothDevice> getConnectedBluetoothDevices() {
    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    return bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
}
 
Example 20
Source File: DebugModeDeviceAdapter.java    From EFRConnect-android with Apache License 2.0 4 votes vote down vote up
private List<BluetoothDevice> getConnectedDevices() {
    BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    return bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
}