Example usage for android.bluetooth BluetoothGattService getCharacteristic

List of usage examples for android.bluetooth BluetoothGattService getCharacteristic

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattService getCharacteristic.

Prototype

public BluetoothGattCharacteristic getCharacteristic(UUID uuid) 

Source Link

Document

Returns a characteristic with a given UUID out of the list of characteristics offered by this service.

Usage

From source file:io.puzzlebox.bloom.ui.BloomFragment.java

private void getGattService(BluetoothGattService gattService) {
    if (gattService == null)
        return;//from   w  ww. j av a 2s .  c  o  m

    setButtonEnable();
    startReadRssi();

    BloomSingleton.getInstance().characteristicTx = gattService
            .getCharacteristic(RBLService.UUID_BLE_SHIELD_TX);

    BluetoothGattCharacteristic characteristicRx = gattService.getCharacteristic(RBLService.UUID_BLE_SHIELD_RX);
    BloomSingleton.getInstance().mBluetoothLeService.setCharacteristicNotification(characteristicRx, true);
    BloomSingleton.getInstance().mBluetoothLeService.readCharacteristic(characteristicRx);
}

From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuService.java

/**
 * Disconnects from the device and cleans local variables in case of error. This method is SYNCHRONOUS and wait until the disconnecting process will be completed.
 * //w  w  w  .ja v  a2  s  . c om
 * @param gatt
 *            the GATT device to be disconnected
 * @param error
 *            error number
 */
private void terminateConnection(final BluetoothGatt gatt, final int error) {
    if (mConnectionState != STATE_DISCONNECTED) {
        updateProgressNotification(PROGRESS_DISCONNECTING);

        // disable notifications
        try {
            final BluetoothGattService dfuService = gatt.getService(DFU_SERVICE_UUID);
            if (dfuService != null) {
                final BluetoothGattCharacteristic controlPointCharacteristic = dfuService
                        .getCharacteristic(DFU_CONTROL_POINT_UUID);
                setCharacteristicNotification(gatt, controlPointCharacteristic, false);
                sendLogBroadcast(Level.INFO, "Notifications disabled");
            }
        } catch (final DeviceDisconnectedException e) {
            // do nothing
        } catch (final DfuException e) {
            // do nothing
        } catch (final Exception e) {
            // do nothing
        }

        // Disconnect from the device
        disconnect(gatt);
        sendLogBroadcast(Level.INFO, "Disconnected");
    }

    // Close the device
    refreshDeviceCache(gatt);
    close(gatt);
    updateProgressNotification(error);
}

From source file:com.umundus.service.NCallServiceOld.java

public void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (BATTERY_CHARACTERISTIC.equals(characteristic.getUuid())) {

        int battery = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
        Log.d(TAG, String.format("battery value : %d", battery));

        intent.putExtra(BATTERY_VALUE, battery);

    } else if (NCALL_REMOCON_KEY_NOTI_CHARACTERISTIC.equals(characteristic.getUuid())) {

        int cmd = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);

        if (cmd == 0x00) {

            intent.putExtra(KEY_INPUT_DATA, characteristic.getValue());

        } else if (cmd == 0x22) {

            int status = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
            Log.d(TAG, String.format("scenario status : %d", status));
            intent.putExtra(SCENARIO_STATUS_DATA, status);

        } else if (cmd == 0x32) {
            intent.putExtra(LED_INPUT_DATA, characteristic.getValue());
        } else if (cmd == 0x42) {
            intent.putExtra(VERSION_INPUT_DATA, characteristic.getValue());
        } else if (cmd == 0x52) {
            int status = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);

            Log.i(TAG, "RESPONSE_PATRERN_DATA ++");

            if (status == RESPONSE_PATRERN_DATA_WRITE_ERROR) {
                Log.e(TAG, "PATTERN_DATA_WRITE_ERROR");
            } else if (status == RESPONSE_PATRERN_DATA_WRITE_START) {
                Log.i(TAG, "pattern data response ok!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                if (mBluetoothAdapter == null || mBluetoothGatt == null) {
                    Log.w(TAG, "writeRemoconCharacteristic BluetoothAdapter not initialized");
                    return;
                }/*from w w  w .j  a  v  a2s  . c o m*/

                BluetoothGattService RemoconService = mBluetoothGatt.getService(NCALL_REMOCON_SERVICE);
                if (RemoconService == null) {
                    showMessage("NCALL_REMOCON_SERVICE = NULL");
                    return;
                }

                BluetoothGattCharacteristic RemoconChar = RemoconService
                        .getCharacteristic(NCALL_REMOCON_SEND_PATTERN_CHARACTERISTIC);
                if (RemoconChar == null) {
                    showMessage("NCALL_REMOCON_SEND_PATTERN_CHARACTERISTIC = NULL");
                    return;
                }

                try {
                    final byte[] buffer = mBuffer;
                    final int size = mInputStream.read(buffer);

                    writePacket(mBluetoothGatt, RemoconChar, buffer, size);
                } catch (Exception e) {
                    Log.e(TAG, "" + e);
                }
            } else if (status == RESPONSE_PATRERN_DATA_WRITE_END) {
                try {
                    patternCount++;
                    if (patternCount < 12) {
                        Log.w(TAG, "" + patternCount);
                        PatternTransferStart(patternCount);
                    } else {
                        broadcastUpdate(PROGRESS_SET_INVISIBLE);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }

            Log.i(TAG, "RESPONSE_PATRERN_DATA --");

        } else if (cmd == 0x62) {
            Log.i(TAG, "/  ack:" + characteristic.getValue());
        }
    } else if (MANUFACTURER_NAME_UUID.equals(characteristic.getUuid())) {
        byte getdata[] = characteristic.getValue();
        Log.i(TAG, "Manufacturer Name : " + new String(getdata, 0, getdata.length));
        intent.putExtra(MANUFACTURER_NAME, characteristic.getValue());
    } else if (MODEL_NAME_UUID.equals(characteristic.getUuid())) {
        byte getdata[] = characteristic.getValue();
        Log.i(TAG, "Model Number : " + new String(getdata, 0, getdata.length));
        intent.putExtra(MODEL_NAME, characteristic.getValue());
    } else if (FIRMWARE_REVISON_UUID.equals(characteristic.getUuid())) {
        byte getdata[] = characteristic.getValue();
        Log.i(TAG, "firmware vision = " + new String(getdata, 0, getdata.length));
        intent.putExtra(FIRMWARE_REVISON, characteristic.getValue());
    } else if (HARDWARE_REVISON_UUID.equals(characteristic.getUuid())) {
        byte getdata[] = characteristic.getValue();
        Log.i(TAG, "hardware revision = " + new String(getdata, 0, getdata.length));
        intent.putExtra(HARDWARE_REVISON, characteristic.getValue());
    }

    //        synchronized (mLock) {
    //           mLock.notifyAll();
    //      }
    sendBroadcast(intent);
}

From source file:com.umundus.service.NCallServiceOld.java

/**
 * Enable Key Button Notification//  w ww .  j a  v a 2s .co m
 * 
 */
public void enableKeyInputNotification() {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "enableKeyInputNotification BluetoothAdapter not initialized");
        return;
    }

    BluetoothGattService KeyInputService = mBluetoothGatt.getService(NCALL_REMOCON_SERVICE);
    if (KeyInputService == null) {
        showMessage("key input service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }

    // ?  KEY ? ?? .
    BluetoothGattCharacteristic KeyInputChar = KeyInputService
            .getCharacteristic(NCALL_REMOCON_KEY_NOTI_CHARACTERISTIC);
    if (KeyInputChar == null) {
        showMessage("Key input charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }

    mRequestCompleted = false;

    mBluetoothGatt.setCharacteristicNotification(KeyInputChar, true);

    BluetoothGattDescriptor descriptor = KeyInputChar.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);

    synchronized (mLock) {
        if (!mRequestCompleted && mConnectionState == STATE_DISCOVERED) {
            try {
                mLock.wait(); // onDescriptorWrite ? notify .
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

From source file:com.zologic.tardis.app.MainActivity.java

private void launchComponentActivity() {
    // Enable generic attribute service
    final BluetoothGattService genericAttributeService = mBleManager.getGattService(kGenericAttributeService);
    if (genericAttributeService != null) {
        Log.d(TAG, "kGenericAttributeService found. Check if kServiceChangedCharacteristic exists");

        final UUID characteristicUuid = UUID.fromString(kServiceChangedCharacteristic);
        final BluetoothGattCharacteristic dataCharacteristic = genericAttributeService
                .getCharacteristic(characteristicUuid);
        if (dataCharacteristic != null) {
            Log.d(TAG, "kServiceChangedCharacteristic exists. Enable indication");
            mBleManager.enableIndication(genericAttributeService, kServiceChangedCharacteristic, true);
        } else {//from  w w  w .  j  a va 2s .  co  m
            Log.d(TAG, "Skip enable indications for kServiceChangedCharacteristic. Characteristic not found");
        }
    } else {
        Log.d(TAG,
                "Skip enable indications for kServiceChangedCharacteristic. kGenericAttributeService not found");
    }

    // Launch activity
    showConnectionStatus(false);
    if (mComponentToStartWhenConnected != null) {
        Intent intent = new Intent(MainActivity.this, mComponentToStartWhenConnected);
        if (mComponentToStartWhenConnected == BeaconActivity.class && mSelectedDeviceData != null) {
            intent.putExtra("rssi", mSelectedDeviceData.rssi);
        }
        startActivityForResult(intent, kActivityRequestCode_ConnectedActivity);
    }
}

From source file:com.example.nachiketvatkar.locateus.BluetoothPairing.java

private void launchComponentActivity() {
    // Enable generic attribute service
    final BluetoothGattService genericAttributeService = mBleManager.getGattService(kGenericAttributeService);
    if (genericAttributeService != null) {
        Log.d(TAG, "kGenericAttributeService found. Check if kServiceChangedCharacteristic exists");

        final UUID characteristicUuid = UUID.fromString(kServiceChangedCharacteristic);
        final BluetoothGattCharacteristic dataCharacteristic = genericAttributeService
                .getCharacteristic(characteristicUuid);
        if (dataCharacteristic != null) {
            Log.d(TAG, "kServiceChangedCharacteristic exists. Enable indication");
            mBleManager.enableIndication(genericAttributeService, kServiceChangedCharacteristic, true);
        } else {/*from  www  .j  a  va 2  s . c o m*/
            Log.d(TAG, "Skip enable indications for kServiceChangedCharacteristic. Characteristic not found");
        }
    } else {
        Log.d(TAG,
                "Skip enable indications for kServiceChangedCharacteristic. kGenericAttributeService not found");
    }

    // Launch activity
    showConnectionStatus(false);
    if (mComponentToStartWhenConnected != null) {
        Intent intent = new Intent(BluetoothPairing.this, mComponentToStartWhenConnected);
        //            if (mComponentToStartWhenConnected == BeaconActivity.class && mSelectedDeviceData != null) {
        //                intent.putExtra("rssi", mSelectedDeviceData.rssi);
        //            }
        startActivityForResult(intent, kActivityRequestCode_ConnectedActivity);
    }
}

From source file:com.cs4644.vt.theonering.MainActivity.java

private void launchComponentActivity() {
    // Enable generic attribute service
    final BluetoothGattService genericAttributeService = mBleManager.getGattService(kGenericAttributeService);
    if (genericAttributeService != null) {
        Log.d(TAG, "kGenericAttributeService found. Check if kServiceChangedCharacteristic exists");

        final UUID characteristicUuid = UUID.fromString(kServiceChangedCharacteristic);
        final BluetoothGattCharacteristic dataCharacteristic = genericAttributeService
                .getCharacteristic(characteristicUuid);
        if (dataCharacteristic != null) {
            Log.d(TAG, "kServiceChangedCharacteristic exists. Enable indication");
            mBleManager.enableIndication(genericAttributeService, kServiceChangedCharacteristic, true);
        } else {//from  w  ww  .j a  v a 2s  .c  om
            Log.d(TAG, "Skip enable indications for kServiceChangedCharacteristic. Characteristic not found");
        }
    } else {
        Log.d(TAG,
                "Skip enable indications for kServiceChangedCharacteristic. kGenericAttributeService not found");
    }

    // Launch activity
    showConnectionStatus(false);
    if (mComponentToStartWhenConnected != null) {

        Intent intent = new Intent(MainActivity.this, mComponentToStartWhenConnected);

        //            if (mComponentToStartWhenConnected == BeaconActivity.class && mSelectedDeviceData != null) {
        //                intent.putExtra("rssi", mSelectedDeviceData.rssi);
        //            }

        startActivityForResult(intent, kActivityRequestCode_ConnectedActivity);
    }
}

From source file:com.megster.cordova.ble.central.Peripheral.java

private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service,
        UUID characteristicUUID) {
    BluetoothGattCharacteristic characteristic = null;

    // Check for Notify first
    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0
                && characteristicUUID.equals(c.getUuid())) {
            characteristic = c;/* www.  j a  v a 2s.  co m*/
            break;
        }
    }

    if (characteristic != null)
        return characteristic;

    // If there wasn't Notify Characteristic, check for Indicate
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0
                && characteristicUUID.equals(c.getUuid())) {
            characteristic = c;
            break;
        }
    }

    // As a last resort, try and find ANY characteristic with this UUID, even if it doesn't have the correct properties
    if (characteristic == null) {
        characteristic = service.getCharacteristic(characteristicUUID);
    }

    return characteristic;
}

From source file:com.mbientlab.metawear.api.MetaWearBleService.java

/**
 * Writes a command to MetaWear via the command register UUID
 * @see Characteristics.MetaWear#COMMAND
 *///  w  ww.  j  ava 2  s .c  o m
private void writeCommand(final MetaWearState mwState) {
    gattActions.add(new GattAction() {
        @Override
        public void execAction() {
            byte[] next = mwState.commandBytes.poll();

            if (next != null && mwState.mwGatt != null) {
                BluetoothGattService service = mwState.mwGatt.getService(GATTService.METAWEAR.uuid());
                BluetoothGattCharacteristic command = service.getCharacteristic(MetaWear.COMMAND.uuid());
                command.setValue(next);
                mwState.mwGatt.writeCharacteristic(command);
            } else {
                execGattAction();
            }
        }
    });
}

From source file:com.mbientlab.metawear.api.MetaWearBleService.java

/**
 * Read a characteristic from MetaWear./*from  w w w. jav  a 2 s .  c o m*/
 * An intent with the action CHARACTERISTIC_READ will be broadcasted.
 * @see Action.BluetoothLe#ACTION_CHARACTERISTIC_READ
 */
private void readCharacteristic(final MetaWearState mwState) {
    gattActions.add(new GattAction() {
        @Override
        public void execAction() {
            GATTCharacteristic charInfo = mwState.readCharUuids.poll();

            if (mwState.mwGatt != null) {
                BluetoothGattService service = mwState.mwGatt.getService(charInfo.gattService().uuid());

                BluetoothGattCharacteristic characteristic = service.getCharacteristic(charInfo.uuid());
                mwState.mwGatt.readCharacteristic(characteristic);
            } else {
                execGattAction();
            }
        }
    });
}