Example usage for android.bluetooth BluetoothProfile STATE_CONNECTED

List of usage examples for android.bluetooth BluetoothProfile STATE_CONNECTED

Introduction

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

Prototype

int STATE_CONNECTED

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

Click Source Link

Document

The profile is in connected state

Usage

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

private void setBleServiceListener() {
    mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() {
        @Override/* w  w  w  . j  a v  a2  s.  c  om*/
        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.google.android.apps.forscience.ble.MyBleService.java

boolean connect(String address) {
    BluetoothDevice device = btAdapter.getRemoteDevice(address);
    if (device == null) {
        return false;
    }//  www  .  j  a  v  a2 s  . co m
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    int connectionState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
    if (bluetoothGatt != null && connectionState != BluetoothProfile.STATE_CONNECTED) {
        return bluetoothGatt.connect();
    }
    if (bluetoothGatt != null && connectionState == BluetoothProfile.STATE_CONNECTED) {
        sendGattBroadcast(address, BleEvents.GATT_CONNECT, null);
        return true;
    }

    if (bluetoothGatt != null) {
        bluetoothGatt.close();
    }
    bluetoothGatt = device.connectGatt(this, false, // autoConnect = false
            gattCallbacks);
    addressToGattClient.put(address, bluetoothGatt);
    return true;
}

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

private void setBleServiceListener() {
    mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() {
        @Override//from w  w w.  j  a  v  a2  s.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:com.ferdi2005.secondgram.voip.VoIPService.java

@Override
public void onCreate() {
    super.onCreate();
    FileLog.d("=============== VoIPService STARTING ===============");
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1
            && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER) != null) {
        int outFramesPerBuffer = Integer
                .parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
        VoIPController.setNativeBufferSize(outFramesPerBuffer);
    } else {/*from   ww  w .ja v a  2 s.  co  m*/
        VoIPController.setNativeBufferSize(
                AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT)
                        / 2);
    }
    final SharedPreferences preferences = getSharedPreferences("mainconfig", MODE_PRIVATE);
    VoIPServerConfig.setConfig(preferences.getString("voip_server_config", "{}"));
    if (System.currentTimeMillis() - preferences.getLong("voip_server_config_updated", 0) > 24 * 3600000) {
        ConnectionsManager.getInstance().sendRequest(new TLRPC.TL_phone_getCallConfig(), new RequestDelegate() {
            @Override
            public void run(TLObject response, TLRPC.TL_error error) {
                if (error == null) {
                    String data = ((TLRPC.TL_dataJSON) response).data;
                    VoIPServerConfig.setConfig(data);
                    preferences.edit().putString("voip_server_config", data)
                            .putLong("voip_server_config_updated",
                                    BuildConfig.DEBUG ? 0 : System.currentTimeMillis())
                            .apply();
                }
            }
        });
    }
    try {
        controller = new VoIPController();
        controller.setConnectionStateListener(this);
        controller.setConfig(MessagesController.getInstance().callPacketTimeout / 1000.0,
                MessagesController.getInstance().callConnectTimeout / 1000.0,
                preferences.getInt("VoipDataSaving", VoIPController.DATA_SAVING_NEVER));

        cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE))
                .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
        cpuWakelock.acquire();

        btAdapter = am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;

        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.addAction(ACTION_HEADSET_PLUG);
        if (btAdapter != null) {
            filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
            filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
        }
        filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        filter.addAction(getPackageName() + ".END_CALL");
        filter.addAction(getPackageName() + ".DECLINE_CALL");
        filter.addAction(getPackageName() + ".ANSWER_CALL");
        registerReceiver(receiver, filter);

        ConnectionsManager.getInstance().setAppPaused(false, false);

        soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
        spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
        spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
        spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
        spEndId = soundPool.load(this, R.raw.voip_end, 1);
        spBusyId = soundPool.load(this, R.raw.voip_busy, 1);

        am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));

        if (btAdapter != null && btAdapter.isEnabled()) {
            int headsetState = btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
            updateBluetoothHeadsetState(headsetState == BluetoothProfile.STATE_CONNECTED);
            if (headsetState == BluetoothProfile.STATE_CONNECTED)
                am.setBluetoothScoOn(true);
            for (StateListener l : stateListeners)
                l.onAudioSettingsChanged();
        }

        NotificationCenter.getInstance().addObserver(this, NotificationCenter.appDidLogout);
    } catch (Exception x) {
        FileLog.e("error initializing voip controller", x);
        callFailed();
    }
}

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

private void toggleConnectionState(final Button bv, final MooshimeterDevice m) {
    bv.setEnabled(false);/*from   w  w w  .j ava 2  s . co  m*/
    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();
}

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

private void conncetManage(BluetoothGatt gatt, int newState) {
    String deviceAddress = getDeviceAddress(gatt);
    JSONObject obj = new JSONObject();
    if (connectCC.get(deviceAddress) != null) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Tools.addProperty(obj, Tools.DEVICE_ADDRESS, deviceAddress);
            connectCC.get(deviceAddress).success(obj);
            connectCC.remove(deviceAddress);
            connectedDevice.put(deviceAddress, true);
        } else {/*from w w w.j a v  a  2s.  c  o m*/
            Tools.addProperty(obj, Tools.DEVICE_ADDRESS, deviceAddress);
            connectCC.get(deviceAddress).error(obj);
            connectCC.remove(deviceAddress);
        }
    }
}

From source file:org.bcsphere.bluetooth.BluetoothSam42.java

private boolean isConnected(BluetoothDevice device) {
    if (device == null) {
        return false;
    }/*from w  w w  . j a v a2 s  .c o m*/
    return bluetoothGatt.getConnectedDevices().contains(device)
            && bluetoothGatt.getConnectionState(device) == BluetoothProfile.STATE_CONNECTED;
}