Example usage for android.bluetooth BluetoothProfile STATE_CONNECTING

List of usage examples for android.bluetooth BluetoothProfile STATE_CONNECTING

Introduction

In this page you can find the example usage for android.bluetooth BluetoothProfile STATE_CONNECTING.

Prototype

int STATE_CONNECTING

To view the source code for android.bluetooth BluetoothProfile STATE_CONNECTING.

Click Source Link

Document

The profile is in connecting state

Usage

From source file:Main.java

public static String getBleConnectStatus(int status) {
    switch (status) {
    case BluetoothProfile.STATE_DISCONNECTED:
        return "STATE_DISCONNECTED";

    case BluetoothProfile.STATE_CONNECTING:
        return "STATE_CONNECTING";

    case BluetoothProfile.STATE_CONNECTED:
        return "STATE_CONNECTED";

    case BluetoothProfile.STATE_DISCONNECTING:
        return "STATE_DISCONNECTING";

    default:/*from  ww  w.  j  av  a2 s . c  o  m*/
        return "STATE_UNKNOWN: " + status;
    }
}

From source file:com.mooshim.mooshimeter.main.ScanActivity.java

private void addDevice(final MooshimeterDevice d) {
    mEmptyMsg.setVisibility(View.GONE);

    if (mMeterList.contains(d)) {
        // The meter as already been added
        Log.e(TAG, "Tried to add the same meter twice");
        return;//w  ww  .  j av a2  s.c  o m
    }

    mMeterList.add(d);
    mMeterDict.put(d.getAddress(), d);

    final LinearLayout wrapper = new LinearLayout(this);
    wrapper.setOrientation(LinearLayout.VERTICAL);
    wrapper.setLayoutParams(mDeviceScrollView.getLayoutParams());

    wrapper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (d.mConnectionState == BluetoothProfile.STATE_CONNECTED
                    || d.mConnectionState == BluetoothProfile.STATE_CONNECTING) {
                startSingleMeterActivity(d);
            } else {
                final Button bv = (Button) view.findViewById(R.id.btnConnect);
                toggleConnectionState(bv, d);
            }
        }
    });

    wrapper.addView(mInflater.inflate(R.layout.element_mm_titlebar, wrapper, false));
    //wrapper.setTag(Integer.valueOf(R.layout.element_mm_titlebar));

    refreshMeterTile(d, wrapper);
    mDeviceScrollView.addView(wrapper);
    mTileList.add(wrapper);

    if (mMeterList.size() > 1)
        setStatus(mMeterList.size() + " devices");
    else
        setStatus("1 device");
}

From source file:com.junkchen.blelib.sample.MultipleBleActivity.java

private void setBleServiceListener() {
    mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() {
        @Override/*from www  . j  av  a2 s.  com*/
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                for (int i = 0; i < deviceList.size(); i++) {
                    HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i);
                    if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) {
                        ((HashMap) deviceList.get(i)).put("isConnect", false);
                        return;
                    }
                }
            } else if (newState == BluetoothProfile.STATE_CONNECTING) {

            } else if (newState == BluetoothProfile.STATE_CONNECTED) {
                for (int i = 0; i < deviceList.size(); i++) {
                    HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i);
                    if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) {
                        ((HashMap) deviceList.get(i)).put("isConnect", true);
                        return;
                    }
                }
            } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {

            }
        }
    });
    mBleService.setOnDataAvailableListener(new MultipleBleService.OnDataAvailableListener() {
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {

        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {

        }
    });
}

From source file:com.beestar.ble.ble.ui.MultipleBleActivity.java

private void setBleServiceListener() {
    mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() {
        @Override/*from   www .j a  v  a 2s  .  c  o m*/
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                for (int i = 0; i < deviceList.size(); i++) {
                    HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i);
                    if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) {
                        ((HashMap) deviceList.get(i)).put("isConnect", false);
                        return;
                    }
                }
            } else if (newState == BluetoothProfile.STATE_CONNECTING) {

            } else if (newState == BluetoothProfile.STATE_CONNECTED) {
                for (int i = 0; i < deviceList.size(); i++) {
                    HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i);
                    if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) {
                        ((HashMap) deviceList.get(i)).put("isConnect", true);
                        return;
                    }
                }
            } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {

            }
        }
    });
    //??
    mBleService.setOnServicesDiscoveredListener(new MultipleBleService.OnServicesDiscoveredListener() {
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            String characteristics_uuid_read = null;
            String characteristics_uuid_write = null;
            String read_Uuid = null;
            String write_Uuid = null;
            BluetoothGattCharacteristic characteristic;
            if (status == gatt.GATT_SUCCESS) {
                String address = gatt.getDevice().getAddress();
                List<BluetoothGattService> services = gatt.getServices();
                commonUtils = new CommonUtils(getApplicationContext());
                int j, k;
                for (int i = 0; i < services.size(); i++) {

                    if (services.get(i).getUuid().toString().contains("ffe5")) {
                        write_Uuid = services.get(i).getUuid().toString();
                        Log.i("info", "--------------------" + i);

                        characteristics_uuid_write = services.get(i).getCharacteristics().get(0).getUuid()
                                .toString();

                    }
                    if (services.get(i).getUuid().toString().contains("ffe0")) {
                        Log.i("info", "--------------------" + i);
                        characteristic = services.get(i).getCharacteristic(services.get(i).getUuid());
                        read_Uuid = services.get(i).getUuid().toString();
                        characteristics_uuid_read = services.get(i).getCharacteristics().get(0).getUuid()
                                .toString();

                    }
                }
                Equipment_Device equipment_device = new Equipment_Device();
                equipment_device.setAddress(address);
                equipment_device.setRead_uuid(read_Uuid);
                equipment_device.setRead_uuid(write_Uuid);
                equipment_device.setCh_read_uuid(characteristics_uuid_read);
                equipment_device.setCh_write_uuid(characteristics_uuid_write);
                commonUtils.insertStudent(equipment_device);
            }
        }
    });
    mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        }
    });
    mBleService.setOnDataAvailableListener(new MultipleBleService.OnDataAvailableListener() {
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {

        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {

        }
    });
}

From source file:net.kenevans.android.blecardiacmonitor.BCMBleService.java

/**
 * Connects to the GATT server hosted on the Bluetooth LE device.
 *
 * @param address The device address of the destination device.
 * @return Return true if the connection is initiated successfully. The
 * connection result is reported asynchronously through the
 * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth
 * .BluetoothGatt, int, int)}//from w w  w  .java2 s. c  o  m
 * callback.
 */
public boolean connect(final String address) {
    Log.d(TAG, "connect");
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "connect: BluetoothAdapter not initialized or unspecified" + " address");
        return false;
    }

    // Previously connected device. Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
        Log.d(TAG, "connect: Trying to use an existing mBluetoothGatt for " + "connection");
        if (mBluetoothGatt.connect()) {
            mConnectionState = BluetoothProfile.STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect");
        return false;
    }
    // We want to directly connect to the device, so we are setting the
    // autoConnect parameter to false.
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection");
    mBluetoothDeviceAddress = address;
    mConnectionState = BluetoothProfile.STATE_CONNECTING;
    return true;
}

From source file:net.kenevans.android.hxmmonitor.HxMBleService.java

/**
 * Connects to the GATT server hosted on the Bluetooth LE device.
 *
 * @param address//ww w  .  j  a  v  a2 s .  co  m
 *            The device address of the destination device.
 *
 * @return Return true if the connection is initiated successfully. The
 *         connection result is reported asynchronously through the
 *         {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
 *         callback.
 */
public boolean connect(final String address) {
    Log.d(TAG, "connect");
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "connect: BluetoothAdapter not initialized or unspecified address");
        return false;
    }

    // Previously connected device. Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
        Log.d(TAG, "connect: Trying to use an existing mBluetoothGatt for connection");
        if (mBluetoothGatt.connect()) {
            mConnectionState = BluetoothProfile.STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect");
        return false;
    }
    // We want to directly connect to the device, so we are setting the
    // autoConnect parameter to false.
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection");
    mBluetoothDeviceAddress = address;
    mConnectionState = BluetoothProfile.STATE_CONNECTING;
    return true;
}

From source file:com.mooshim.mooshimeter.main.ScanActivity.java

private void toggleConnectionState(final Button bv, final MooshimeterDevice m) {
    bv.setEnabled(false);/*from www  .  j  ava 2s .c om*/
    bv.setAlpha((float) 0.5);
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            if (m.mConnectionState == BluetoothProfile.STATE_CONNECTED
                    || m.mConnectionState == BluetoothProfile.STATE_CONNECTING) {
                m.disconnect();
            } else {
                int rval;
                setStatus("Connecting...");
                rval = m.connect();
                if (BluetoothGatt.GATT_SUCCESS != rval) {
                    setStatus(String.format("Connection failed.  Status: %d", rval));
                    return;
                }
                setStatus("Discovering Services...");
                rval = m.discover();
                if (BluetoothGatt.GATT_SUCCESS != rval) {
                    setStatus(String.format("Discovery failed.  Status: %d", rval));
                    return;
                }
                setStatus("Connected!");
                startSingleMeterActivity(m);
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    bv.setEnabled(true);
                    bv.setAlpha((float) 1.0);
                    int bgid = m.mConnectionState == BluetoothProfile.STATE_CONNECTED ? R.drawable.connected
                            : R.drawable.disconnected;
                    bv.setBackground(getResources().getDrawable(bgid));
                }
            });
        }
    });
    t.start();
}