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: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:/*  ww  w. j a  v a 2 s  .c  om*/
        return "STATE_UNKNOWN: " + status;
    }
}

From source file:fr.bmartel.android.bluetooth.connection.BluetoothDeviceConn.java

/**
 * Build Bluetooth device connection//from ww w.  j a  v a  2s  . co  m
 *
 * @param address
 */
@SuppressLint("NewApi")
public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) {
    this.deviceAddr = address;
    this.deviceName = deviceName;
    this.manager = manager;

    gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            String intentAction;

            if (newState == BluetoothProfile.STATE_CONNECTED) {

                intentAction = ActionFilterGatt.ACTION_GATT_CONNECTED;
                Log.i(TAG, "Connected to GATT server.");
                Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                connected = false;
                intentAction = ActionFilterGatt.ACTION_GATT_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server.");
                manager.broadcastUpdate(ActionFilterGatt.ACTION_GATT_DISCONNECTED);

                if (BluetoothDeviceConn.this.gatt != null) {
                    BluetoothDeviceConn.this.gatt.close();
                }
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {

                Runnable test = new Runnable() {
                    @Override
                    public void run() {

                        //you can improve this by using reflection
                        device = new DottiDevice(BluetoothDeviceConn.this);

                        device.addInitListener(new IDeviceInitListener() {
                            @Override
                            public void onInit() {
                                try {

                                    String name = "";

                                    JSONObject object = new JSONObject();
                                    object.put("address", getAddress());
                                    object.put("deviceName", getDeviceName());

                                    ArrayList<String> values = new ArrayList<String>();
                                    values.add(object.toString());

                                    connected = true;

                                    //when device is fully intitialized broadcast service discovery
                                    manager.broadcastUpdateStringList(
                                            ActionFilterGatt.ACTION_GATT_SERVICES_DISCOVERED, values);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        device.init();
                    }
                };
                Thread testThread = new Thread(test);
                testThread.start();

            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            System.out.println("characteristic write received ");
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicWriteReceived(characteristic);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicReadReceived(characteristic);
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            System.out.println("descriptor write received ");
            manager.getEventManager().set();
        }

        @Override
        // Characteristic notification
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            System.out.println("descriptor change received ");

            if (device != null) {
                device.notifyCharacteristicChangeReceived(characteristic);
            }
        }
    };
}

From source file:fr.bmartel.android.notti.service.bluetooth.connection.BluetoothDeviceConn.java

/**
 * Build Bluetooth device connection//from   www  . j a  va  2  s . c om
 *
 * @param address
 */
@SuppressLint("NewApi")
public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) {
    this.deviceAddr = address;
    this.deviceName = deviceName;
    this.manager = manager;

    gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            if (newState == BluetoothProfile.STATE_CONNECTED) {

                Log.i(TAG, "Connected to GATT server.");
                Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                connected = false;
                Log.i(TAG, "Disconnected from GATT server.");

                try {
                    JSONObject object = new JSONObject();
                    object.put(BluetoothConst.DEVICE_ADDRESS, getAddress());
                    object.put(BluetoothConst.DEVICE_NAME, getDeviceName());

                    ArrayList<String> values = new ArrayList<String>();
                    values.add(object.toString());

                    //when device is fully intitialized broadcast service discovery
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, values);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (BluetoothDeviceConn.this.gatt != null) {
                    BluetoothDeviceConn.this.gatt.close();
                }
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {

                Runnable test = new Runnable() {
                    @Override
                    public void run() {

                        //you can improve this by using reflection
                        device = new NottiDevice(BluetoothDeviceConn.this);

                        device.addInitListener(new IDeviceInitListener() {
                            @Override
                            public void onInit() {
                                try {
                                    JSONObject object = new JSONObject();
                                    object.put(BluetoothConst.DEVICE_ADDRESS, getAddress());
                                    object.put(BluetoothConst.DEVICE_NAME, getDeviceName());

                                    ArrayList<String> values = new ArrayList<String>();
                                    values.add(object.toString());

                                    connected = true;
                                    //when device is fully intitialized broadcast service discovery
                                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED,
                                            values);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        device.init();
                    }
                };
                Thread testThread = new Thread(test);
                testThread.start();

            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicWriteReceived(characteristic);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicReadReceived(characteristic);
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            manager.getEventManager().set();
        }

        @Override
        // Characteristic notification
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            if (device != null) {
                device.notifyCharacteristicChangeReceived(characteristic);
            }
        }
    };
}

From source file:com.github.akinaru.bleanalyzer.bluetooth.connection.BluetoothDeviceConn.java

/**
 * Build Bluetooth device connection/* w ww.  j a va 2  s  .  c  om*/
 *
 * @param address
 */
@SuppressLint("NewApi")
public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) {
    this.deviceAddr = address;
    this.deviceName = deviceName;
    this.manager = manager;

    gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            if (newState == BluetoothProfile.STATE_CONNECTED) {

                Log.i(TAG, "Connected to GATT server.");
                Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                connected = false;
                Log.i(TAG, "Disconnected from GATT server.");

                try {
                    JSONObject object = new JSONObject();
                    object.put(JsonConstants.BT_ADDRESS, getAddress());
                    object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName());

                    ArrayList<String> values = new ArrayList<String>();
                    values.add(object.toString());

                    //when device is fully intitialized broadcast service discovery
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, values);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (manager.getWaitingMap().containsKey(deviceAddr)) {
                    manager.getWaitingMap().get(deviceAddr).cancel(true);
                    manager.getWaitingMap().remove(deviceAddr);
                }

                if (BluetoothDeviceConn.this.gatt != null) {
                    Log.i(TAG, "connection close clean");
                    BluetoothDeviceConn.this.gatt.close();
                }
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            if (status == BluetoothGatt.GATT_SUCCESS) {

                Runnable test = new Runnable() {
                    @Override
                    public void run() {

                        //you can improve this by using reflection
                        device = new RfduinoDevice(BluetoothDeviceConn.this);

                        device.addInitListener(new IDeviceInitListener() {
                            @Override
                            public void onInit() {
                                try {
                                    JSONObject object = new JSONObject();
                                    object.put(JsonConstants.BT_ADDRESS, getAddress());
                                    object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName());

                                    ArrayList<String> values = new ArrayList<String>();
                                    values.add(object.toString());

                                    connected = true;
                                    //when device is fully intitialized broadcast service discovery
                                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED,
                                            values);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        device.init();
                    }
                };
                Thread testThread = new Thread(test);
                testThread.start();

            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            //manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicWriteReceived(characteristic);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                Log.i(TAG, "onCharacteristicRead");
                device.notifyCharacteristicReadReceived(characteristic);
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            manager.getEventManager().set();
        }

        @Override
        // Characteristic notification
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            manager.getEventManager().set();
            if (device != null) {
                Log.i(TAG, "onCharacteristicChanged");
                device.notifyCharacteristicChangeReceived(characteristic);
            }
        }
    };
}

From source file:com.github.akinaru.roboticbuttonpusher.bluetooth.connection.BluetoothDeviceConn.java

/**
 * Build Bluetooth device connection/*from w w w .ja  v a 2 s  . com*/
 *
 * @param address
 */
@SuppressLint("NewApi")
public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) {
    this.deviceAddr = address;
    this.deviceName = deviceName;
    this.manager = manager;

    gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            if (newState == BluetoothProfile.STATE_CONNECTED) {

                Log.v(TAG, "Connected to GATT server.");
                Log.v(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                connected = false;
                Log.v(TAG, "Disconnected from GATT server.");

                if (status != 133) {
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED,
                            new ArrayList<String>());
                } else {
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_RETRY,
                            new ArrayList<String>());
                }

                if (manager.getWaitingMap().containsKey(deviceAddr)) {
                    manager.getWaitingMap().get(deviceAddr).cancel(true);
                    manager.getWaitingMap().remove(deviceAddr);
                }

                if (BluetoothDeviceConn.this.gatt != null) {
                    Log.v(TAG, "connection close clean");
                    BluetoothDeviceConn.this.gatt.close();
                }
                if (remove) {
                    manager.getConnectionList().remove(deviceAddr);
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_REMOVED,
                            new ArrayList<String>());
                }
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            if (status == BluetoothGatt.GATT_SUCCESS) {

                Runnable test = new Runnable() {
                    @Override
                    public void run() {

                        //you can improve this by using reflection
                        device = new RfduinoDevice(BluetoothDeviceConn.this);

                        device.addInitListener(new IDeviceInitListener() {
                            @Override
                            public void onInit() {
                                try {
                                    JSONObject object = new JSONObject();
                                    object.put(JsonConstants.BT_ADDRESS, getAddress());
                                    object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName());

                                    ArrayList<String> values = new ArrayList<String>();
                                    values.add(object.toString());

                                    connected = true;
                                    //when device is fully intitialized broadcast service discovery
                                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED,
                                            values);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        device.init();
                    }
                };
                Thread testThread = new Thread(test);
                testThread.start();

            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicWriteReceived(characteristic);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                Log.v(TAG, "onCharacteristicRead");
                device.notifyCharacteristicReadReceived(characteristic);
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            manager.getEventManager().set();
        }

        @Override
        // Characteristic notification
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            manager.getEventManager().set();
            if (device != null) {
                Log.v(TAG, "onCharacteristicChanged");
                device.notifyCharacteristicChangeReceived(characteristic);
            }
        }
    };
}

From source file:org.physical_web.physicalweb.BluetoothSite.java

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    if (newState == BluetoothProfile.STATE_CONNECTED && status == gatt.GATT_SUCCESS) {
        Log.i(TAG, "Connected to GATT server");
        mBluetoothGatt = gatt;/*from  w ww.  ja v a 2 s .c  o m*/
        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
            gatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH);
            gatt.requestMtu(505);
        } else {
            gatt.discoverServices();
        }
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        Log.i(TAG, "Disconnected to GATT server");
        // ensure progress dialog is removed and running is set false
        close();
    } else if (status != gatt.GATT_SUCCESS) {
        Log.i(TAG, "Status is " + status);
        close();
    }
}

From source file:com.wolkabout.hexiwear.service.BluetoothService.java

private void createGATT(final BluetoothDevice device) {
    bluetoothGatt = device.connectGatt(this, true, new BluetoothGattCallback() {
        @Override//w  w w .ja  v  a2 s.c  o  m
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            isConnected = BluetoothProfile.STATE_CONNECTED == newState;
            if (isConnected) {
                Log.i(TAG, "GATT connected.");
                startForeground(442, getNotification(device));
                gatt.discoverServices();
            } else {
                Log.i(TAG, "GATT disconnected.");
                NotificationService_.intent(BluetoothService.this).stop();
                notificationManager.notify(442, getNotification(device));
                gatt.connect();
            }

            final Intent connectionStateChanged = new Intent(CONNECTION_STATE_CHANGED);
            connectionStateChanged.putExtra(CONNECTION_STATE, isConnected);
            sendBroadcast(connectionStateChanged);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.i(TAG, "Services discovered.");
            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            discoverCharacteristics(gatt);
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            Log.i(TAG, "Characteristic written: " + status);

            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            final byte command = characteristic.getValue()[0];
            switch (command) {
            case WRITE_TIME:
                Log.i(TAG, "Time written.");
                final BluetoothGattCharacteristic batteryCharacteristic = readableCharacteristics
                        .get(Characteristic.BATTERY.getUuid());
                gatt.setCharacteristicNotification(batteryCharacteristic, true);
                for (BluetoothGattDescriptor descriptor : batteryCharacteristic.getDescriptors()) {
                    if (descriptor.getUuid().toString().startsWith("00002904")) {
                        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                        gatt.writeDescriptor(descriptor);
                    }
                }
                break;
            case WRITE_NOTIFICATION:
                Log.i(TAG, "Notification sent.");
                if (notificationsQueue.isEmpty()) {
                    Log.i(TAG, "Reading characteristics...");
                    readNextCharacteristics(gatt);
                } else {
                    Log.i(TAG, "writing next notification...");
                    alertIn.setValue(notificationsQueue.poll());
                    gatt.writeCharacteristic(alertIn);
                }
                break;
            default:
                Log.w(TAG, "No such ALERT IN command: " + command);
                break;
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            readCharacteristic(gatt, Characteristic.MANUFACTURER);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                final BluetoothGattCharacteristic gattCharacteristic, int status) {
            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            final String characteristicUuid = gattCharacteristic.getUuid().toString();
            final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);
            switch (characteristic) {
            case MANUFACTURER:
                manufacturerInfo.manufacturer = gattCharacteristic.getStringValue(0);
                readCharacteristic(gatt, Characteristic.FW_REVISION);
                break;
            case FW_REVISION:
                manufacturerInfo.firmwareRevision = gattCharacteristic.getStringValue(0);
                readCharacteristic(gatt, Characteristic.MODE);
                break;
            default:
                Log.v(TAG, "Characteristic read: " + characteristic.name());
                if (characteristic == Characteristic.MODE) {
                    final Mode newMode = Mode.bySymbol(gattCharacteristic.getValue()[0]);
                    if (mode != newMode) {
                        onModeChanged(newMode);
                    }
                } else {
                    onBluetoothDataReceived(characteristic, gattCharacteristic.getValue());
                }

                if (notificationsQueue.isEmpty()) {
                    readNextCharacteristics(gatt);
                } else {
                    alertIn.setValue(notificationsQueue.poll());
                    gatt.writeCharacteristic(alertIn);
                }

                break;
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                BluetoothGattCharacteristic gattCharacteristic) {
            final String characteristicUuid = gattCharacteristic.getUuid().toString();
            final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);
            Log.d(TAG, "Characteristic changed: " + characteristic);

            if (characteristic == Characteristic.BATTERY) {
                onBluetoothDataReceived(Characteristic.BATTERY, gattCharacteristic.getValue());
            }
        }
    });
}

From source file:net.emilymaier.movebot.HeartFragment.java

@Override
@SuppressWarnings("deprecation")
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
    Log.d("HeartFragment", "Bluetooth LE device found");
    BluetoothGatt bluetoothGatt = device.connectGatt(act, false, new BluetoothGattCallback() {
        @Override/*from   www.ja va 2s .  c  om*/
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.d("HeartFragment", "Connected to LE device");
                gatt.discoverServices();
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                BluetoothGattCharacteristic characteristic = null;
                for (BluetoothGattService service : gatt.getServices()) {
                    characteristic = service
                            .getCharacteristic(UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb"));
                    if (characteristic != null) {
                        break;
                    }
                }
                if (characteristic != null) {
                    Log.d("HeartFragment", "Found device with HRM characteristic");
                    HeartDevice device = new HeartDevice();
                    device.bluetoothGatt = gatt;
                    device.characteristic = characteristic;
                    for (HeartDevice testDevice : heartDevices) {
                        if (testDevice.bluetoothGatt.getDevice().getAddress()
                                .equals(device.bluetoothGatt.getDevice().getAddress())) {
                            heartDevices.remove(testDevice);
                        }
                    }
                    heartDevices.add(device);
                } else {
                    Log.d("HeartFragment", "Device does not have HRM characteristic");
                    gatt.disconnect();
                    gatt.close();
                }
            } else {
                Log.w("HeartFragment", "Failed to discover device services");
                gatt.disconnect();
                gatt.close();
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            int flag = characteristic.getProperties();
            int format = -1;
            if ((flag & 0x01) != 0) {
                format = BluetoothGattCharacteristic.FORMAT_UINT16;
            } else {
                format = BluetoothGattCharacteristic.FORMAT_UINT8;
            }
            final int heartRate = characteristic.getIntValue(format, 1);
            act.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    act.updateHeart(heartRate);
                }
            });
        }
    });
}

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  w  w .  j  a v a 2s  . 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.mooshim.mooshimeter.main.ScanActivity.java

private void refreshMeterTile(final MooshimeterDevice d, final ViewGroup wrapper) {
    if (wrapper.getChildCount() == 0) {
        Log.e(TAG, "Received empty wrapper");
    }//from  w  w w  . j  a  v  a 2 s  .c o m
    if (wrapper.getChildCount() > 0) {
        // Update the title bar
        int rssi = d.mRssi;
        String name;
        String build;
        if (d.mOADMode) {
            name = "Bootloader";
        } else {
            name = d.getBLEDevice().getName();
            if (name == null) {
                name = "Unknown device";
            }
        }

        if (d.mBuildTime == 0) {
            build = "Invalid firmware";
        } else {
            build = "Build: " + d.mBuildTime;
        }

        String descr = name + "\n" + build + "\nRssi: " + rssi + " dBm";
        ((TextView) wrapper.findViewById(R.id.descr)).setText(descr);

        final Button bv = (Button) wrapper.findViewById(R.id.btnConnect);

        int bgid = d.mConnectionState == BluetoothProfile.STATE_CONNECTED ? R.drawable.connected
                : R.drawable.disconnected;
        bv.setBackground(getResources().getDrawable(bgid));

        // Set the click listeners
        bv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggleConnectionState(bv, d);
            }
        });
    }
    if (d.mInitialized && !d.isInOADMode()) {
        // We are representing a connected meter
        if (wrapper.getChildCount() != 2) {
            // We need to create a new value pane
            wrapper.addView(mInflater.inflate(R.layout.element_mm_readingsbar, mDeviceScrollView, false));
        }
        Util.dispatch(new Runnable() {
            @Override
            public void run() {
                if (!d.isNotificationEnabled(d.getChar(MooshimeterDevice.mUUID.METER_SAMPLE))) {
                    // We need to enable notifications
                    d.playSampleStream(new Runnable() {
                        @Override
                        public void run() {
                            TextView ch1 = (TextView) wrapper.findViewById(R.id.ch1_value_label);
                            TextView ch2 = (TextView) wrapper.findViewById(R.id.ch2_value_label);
                            TextView ch1_unit = (TextView) wrapper.findViewById(R.id.ch1_unit_label);
                            TextView ch2_unit = (TextView) wrapper.findViewById(R.id.ch2_unit_label);
                            valueLabelRefresh(0, d, ch1, ch1_unit);
                            valueLabelRefresh(1, d, ch2, ch2_unit);
                        }
                    });
                }
            }
        });
    } else {
        //We are representing a disconnected meter or a meter in OAD mode
        if (wrapper.getChildCount() == 2) {
            // We need to eliminate a pane
            wrapper.removeViewAt(1);
        }
    }
}