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:com.guangyao.bluetoothtest.service.BluetoothLeService.java

/**
 * @brief enableTXNotification/*from   ww  w  .ja v a 2s.co m*/
 */
@SuppressLint("InlinedApi")
public void enableTXNotification() {
    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        return;
    }

    BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
    if (TxChar == null) {
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);
    BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.guangyao.bluetoothtest.service.BluetoothLeService.java

/**
 * @brief writeRXCharacteristic//from w  ww .  j  a va  2  s. c  o m
 */
public boolean writeRXCharacteristic(byte[] value) {
    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        return false;
    }

    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        return false;
    }
    RxChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RxChar);
    Log.d(TAG, "Send commandstatus" + status + "-->" + DataHandlerUtils.bytesToHexStr(value));
    return status;
}

From source file:dev.coatl.co.Urband_IMU_Recorder.presenter.fragments.FragmentMenuPanel.java

@Override
public void onClick(View v) {
    /* Gets the ID of the clicked object */
    int id = v.getId();
    Log.i(TAG, Integer.toString(id));
    /* Gets the activity of running */
    ActivityHomePanel activity = (ActivityHomePanel) getActivity();

    /* Determines what to do according to the ID */
    final String actualUser_str = Urband_Preferences
            .getActualUser(getActivity(), Urband_Preferences.USER_NUMBER).toString();
    final String actualSession_actNum_str = Urband_Preferences
            .getActualSession(getActivity(), Urband_Preferences.ACTIVITY_NUMBER).toString();
    if (id == R.id.button_recording) {
        /* Gets USER_NUM, ACTIVITY_NUM and GESTURE_NUM from preferences and store them as strings for file creation */
        miActualUser = actualUser_str;//from  w  w  w .j  a  v a  2  s. c o m
        miActivitySession = actualSession_actNum_str;
        miGestureSession = Urband_Preferences.getActualSession(getActivity(), Urband_Preferences.GESTURE_NUMBER)
                .toString();

        /* Generates a vibrator object to control the device vibration */
        Vibrator vib = (Vibrator) activity.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);

        /* Determines if starting or ending recording */
        if (!Urband_Preferences.getConfig(getActivity(), Urband_Preferences.isRecordingSession)) {
            /* Generates a file name according to session type selected */
            Toast.makeText(activity, "Se graba una actividad", Toast.LENGTH_LONG).show();
            file = new File(dir, "Urband_U" + miActualUser + "_A" + miActivitySession + ".txt");

            /* Creates the file acording to actual user and type of session */
            try {
                miFile = new FileWriter(file, true);
            } catch (IOException e) {
                e.printStackTrace();
            }

            /* Generates a vibration pattern before start recording to notify the user */
            try {
                vib.vibrate(100);
                Thread.sleep(900);
                vib.vibrate(100);
                Thread.sleep(900);
                vib.vibrate(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            /* Enable recordNow flag */
            recordNow = true;
        } else /* ending recording */
        {
            /* Disable recordNow flag */
            recordNow = false;

            /* Vibrates to notify the user */
            vib.vibrate(300);

            /* Writes last data and closes the file */
            try {
                if (miFile != null) {
                    miFile.flush();
                    miFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        Urband_Preferences.setConfig(getActivity(), Urband_Preferences.isRecordingSession,
                !Urband_Preferences.getConfig(getActivity(), Urband_Preferences.isRecordingSession));
        Log.v("Esta grabando?: ",
                Urband_Preferences.getConfig(getActivity(), Urband_Preferences.isRecordingSession) ? "Si"
                        : "No");
        recordingMovementSession();
    } else if (id == R.id.button_plus_actividad) /* Button adds one to the count of activities */
    {
        /* Gets ACTIVITY_NUM from preferences and adds one to the result */
        BluetoothGattService batteryService = BluetoothService.mBluetoothGatt
                .getService(UUID.fromString(BluetoothGattAttributes.GESTURE_SERVICE));
        if (batteryService == null) {
            Log.i("TAG", "fifo service not found! FRAGMENTMENUPANEL");
            return;
        }
        BluetoothGattCharacteristic FIFO = batteryService
                .getCharacteristic(UUID.fromString(BluetoothGattAttributes.GESTURE_DEBUG_CHAR09));
        if (FIFO == null) {
            Log.i("TAG", "fifo level not found! FRAGMENTMENUPANEL");
            return;
        }
        BluetoothService.mBluetoothGatt.readCharacteristic(FIFO);
        cuenta_actividades = Urband_Preferences.getActualSession(getActivity(),
                Urband_Preferences.ACTIVITY_NUMBER) + 1;

        if (cuenta_actividades > 40) {
            cuenta_actividades = 40;
        }
        /* Updates ACTIVITY_NUM into preferences */
        Urband_Preferences.setActualSession(getActivity(), Urband_Preferences.ACTIVITY_NUMBER,
                cuenta_actividades);
        /* Set textView with the new value */
        Actividades.setText(actualSession_actNum_str + " de 40");
        Log.i("Session de Actividad: ", actualSession_actNum_str);
    }

    else if (id == R.id.button_minus_actividad) {
        cuenta_actividades = Urband_Preferences.getActualUser(getActivity(), Urband_Preferences.ACTIVITY_NUMBER)
                - 1;
        if (cuenta_actividades <= 0) {
            cuenta_actividades = 1;
        }
        Urband_Preferences.setActualUser(getActivity(), Urband_Preferences.ACTIVITY_NUMBER, cuenta_actividades);
        final String actualUser_actnum = Urband_Preferences
                .getActualUser(getActivity(), Urband_Preferences.ACTIVITY_NUMBER).toString();
        Actividades.setText(actualUser_actnum + " de 40");
        Log.v("Session de Actividad: ", actualUser_actnum);
    }

    else if (id == R.id.button_plus_user) {
        cuenta_usuarios = Urband_Preferences.getActualUser(getActivity(), Urband_Preferences.USER_NUMBER) + 1;
        Urband_Preferences.setActualUser(getActivity(), Urband_Preferences.USER_NUMBER, cuenta_usuarios);
        Usuarios.setText(actualUser_str);
        Log.v("Usuario: ", actualUser_str);
    }

    else if (id == R.id.button_minus_user) {
        cuenta_usuarios = Urband_Preferences.getActualUser(getActivity(), Urband_Preferences.USER_NUMBER) - 1;
        if (cuenta_usuarios == 0) {
            cuenta_usuarios = 1;
        }
        Urband_Preferences.setActualUser(getActivity(), Urband_Preferences.USER_NUMBER, cuenta_usuarios);
        Usuarios.setText(actualUser_str);
        Log.v("Usuario: ", actualUser_str);
    }

    /* Determines which is the actual session and display description on TextView sessionDescription */
    int indexActivity = Urband_Preferences.getActualSession(getActivity(), Urband_Preferences.ACTIVITY_NUMBER)
            - 1;
    sessionDescription.setText(ActivitySessionsDescription[indexActivity]);
}

From source file:com.sdingba.su.alphabet_demotest.view.lanya.UartService.java

/**
 * ?   ?/*from w w  w  .  ja va 2s.  com*/
 * @param value
 */
public void writeRXCharacteristic(byte[] value) {

    if (mBluetoothGatt == null) {
        showMessage("SDingBaLanYan : mBluetoothGatt(is null) = null" + mBluetoothGatt);
        Log.d(TAG, "SDingBaLanYan : mBluetoothGatt(is null) = null" + mBluetoothGatt);

        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }

    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        showMessage("SDingBaLanYan : Rx service not found!---RX_SERVICE_UUID");
        Log.d(TAG, "SDingBaLanYan : Rx service not found!---RX_SERVICE_UUID");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }

    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        showMessage(" ==Rx== charateristic not found!");
        Log.d(TAG, " ==Rx== charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }

    RxChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RxChar);

    Log.d(TAG, "SDingBaLanYan ???write TXchar - status=" + status);
}

From source file:com.aprilbrother.blueduino.UartService.java

public boolean writeRXCharacteristic(byte[] value) {
    BluetoothGattService RxService = null;
    if (mBluetoothGatt != null) {
        RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    } else {/*from   w  w w .ja v  a 2s . c  om*/
        showMessage("mBluetoothGatt null" + mBluetoothGatt);
    }
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return false;
    }
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        showMessage("Rx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return false;
    }
    RxChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RxChar);
    Log.d(TAG, "write TXchar - status=" + status);

    return status;
}

From source file:com.example.bluetoothmodule.TreadmillService.java

private void Read_Uint_CHARACTERISTIC_Data() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;/*  w w w  .  j  a  v a 2  s  .co  m*/
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService.getCharacteristic(UUID_UNIT_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("DISPLAY STATE charateristic not found!");
        return;
    }
    readCharacteristic(Characteristic);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

private void Enable_SPEED_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;/* w  w w  .  ja v  a  2  s  .  c  o  m*/
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService.getCharacteristic(UUID_SPEED_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("SPEED charateristic not found!");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(Characteristic, true);
    BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

private void Enable_INCLINE_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;//from  w  w  w.j  av  a2 s .  c om
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_INCLINE_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("SPEED charateristic not found!");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(Characteristic, true);
    BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

private void Enable_SportTime_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;/*  w w w  . jav  a2s .  c  om*/
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_SPORT_TIME_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("SPEED charateristic not found!");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(Characteristic, true);
    BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

private void Enable_SportDistance_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;//w w w . j  a  v a2  s .co m
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_SPORT_DISTANCE_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("SPEED charateristic not found!");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(Characteristic, true);
    BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}