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.example.bluetoothmodule.TreadmillService.java

private void Enable_SporCalories_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;/*w ww .j  ava 2s.  c o  m*/
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_SPORT_CALORIES_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

/**
 * Enable TXNotification/*from   ww  w.j a va 2  s .  c  o m*/
 *
 * @return 
 */
private void enableTXNotification() {
    BluetoothGattService RxService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
    if (TxChar == null) {
        showMessage("Tx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    Log.i("Chandler", "RX_SERVICE_UUID");
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);
    BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

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

private void Enable_HeartRate_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(HR_SERVICE_UUID);
    if (TreadmillService == null) {
        showMessage("HR_SERVICE service not found!");
        return;//w w  w . j  av a  2s. co m
    }
    Log.i("Chandler", "Hr_SERVICE_UUID");
    BluetoothGattCharacteristic Characteristic = TreadmillService.getCharacteristic(UUID_HR_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("Hr 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_DISPLAY_STATE_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("Rx service not found!");
        //broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;/*from   ww w.j  a  v  a2 s.  c  o  m*/
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_DISPLAY_STATE_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("DISPLAY STATE charateristic not found!");
        //broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    Log.i("Chandler", "UUID_DISPLAY_STATE_CHARACTERISTIC");
    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

public void writeRXCharacteristic(byte[] value) {
    BluetoothGattService RxService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    showMessage("mBluetoothGatt null" + mBluetoothGatt);
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;//from   w ww .  ja  va  2 s. c  o m
    }
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        showMessage("Rx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    RxChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RxChar);

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

From source file:com.example.user.wase.view.fragment.EquipmentScanner.java

private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null)
        return;//  w w  w. j  ava  2s .  co  m
    String uuid = null;
    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        HashMap<String, String> currentServiceData = new HashMap<String, String>();
        uuid = gattService.getUuid().toString();
        currentServiceData.put(LIST_NAME, HERE_GattAttributes.lookup(uuid, "unknown"));

        // If the service exists for HM 10 Serial, say so.
        if (HERE_GattAttributes.lookup(uuid, "unknown") == "HM 10 Serial") {
        } else {
        }
        currentServiceData.put(LIST_UUID, uuid);
        gattServiceData.add(currentServiceData);

        // get characteristic when UUID matches RX/TX UUID
        characteristicTX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
        characteristicRX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);

        if (characteristicTX != null && mBluetoothLeService != null) {
            characteristicRX.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothLeService.writeCharacteristic(characteristicTX);
            mBluetoothLeService.setCharacteristicNotification(characteristicRX, true);
        }
    }

}

From source file:com.touchKin.touchkinapp.DeviceControlActivity.java

private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null)
        return;/*  ww w.j  a v  a 2  s.c  om*/
    String uuid = null;
    String unknownServiceString = getResources().getString(R.string.unknown_service);
    String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
    ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData = new ArrayList<ArrayList<HashMap<String, String>>>();
    mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        HashMap<String, String> currentServiceData = new HashMap<String, String>();
        uuid = gattService.getUuid().toString();
        currentServiceData.put(LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));
        currentServiceData.put(LIST_UUID, uuid);
        gattServiceData.add(currentServiceData);

        ArrayList<HashMap<String, String>> gattCharacteristicGroupData = new ArrayList<HashMap<String, String>>();
        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
        ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<BluetoothGattCharacteristic>();

        // Loops through available Characteristics.
        characteristicTX = gattService
                .getCharacteristic(UUID.fromString("00001525-1212-efde-1523-785feabcd123"));
        characteristicRX = gattService
                .getCharacteristic(UUID.fromString("00001525-1212-efde-1523-785feabcd123"));
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            charas.add(gattCharacteristic);
            HashMap<String, String> currentCharaData = new HashMap<String, String>();
            uuid = gattCharacteristic.getUuid().toString();
            currentCharaData.put(LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
            currentCharaData.put(LIST_UUID, uuid);
            gattCharacteristicGroupData.add(currentCharaData);
        }
        mGattCharacteristics.add(charas);
        gattCharacteristicData.add(gattCharacteristicGroupData);
    }

    SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter(this, gattServiceData,
            android.R.layout.simple_expandable_list_item_2, new String[] { LIST_NAME, LIST_UUID },
            new int[] { android.R.id.text1, android.R.id.text2 }, gattCharacteristicData,
            android.R.layout.simple_expandable_list_item_2, new String[] { LIST_NAME, LIST_UUID },
            new int[] { android.R.id.text1, android.R.id.text2 });
    mGattServicesList.setAdapter(gattServiceAdapter);
}

From source file:com.polkapolka.bluetooth.le.DeviceControlActivity.java

private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null)
        return;/*  w ww .  j a  v a  2  s. c o m*/
    String uuid = null;
    String unknownServiceString = getResources().getString(R.string.unknown_service);
    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        HashMap<String, String> currentServiceData = new HashMap<String, String>();
        uuid = gattService.getUuid().toString();
        currentServiceData.put(LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));

        // If the service exists for HM 10 Serial, say so.
        if (SampleGattAttributes.lookup(uuid, unknownServiceString) == "HM 10 Serial") {
            isSerial.setText(getString(R.string.verbinding_herkend));
        } else {
            isSerial.setText("No, serial :-(");
        }
        currentServiceData.put(LIST_UUID, uuid);
        gattServiceData.add(currentServiceData);

        // get characteristic when UUID matches RX/TX UUID
        characteristicTX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
        characteristicRX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
    }

}

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

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

    int read = BluetoothGattCharacteristic.PROPERTY_READ;

    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & read) != 0 && characteristicUUID.equals(c.getUuid())) {
            characteristic = c;//from w ww .  j a  v  a 2s  .c o  m
            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:de.blinkt.openvpn.bluetooth.service.UartService.java

/***
 * ???/*from  w  w  w .jav a  2 s .c  o  m*/
 * @param value  
 * @return
 */
public boolean writeRXCharacteristic(byte[] value) {
    try {
        //mBluetoothGatt?????
        if (mBluetoothGatt == null) {
            Log.e("Blue_Chanl", "????");
            mConnectionState = STATE_DISCONNECTED;
            broadcastUpdate(ACTION_GATT_DISCONNECTED);
            return false;
        }
        BluetoothGattService RxService = null;

        //??
        if (RxService == null) {
            //"6E400001-B5A3-F393-E0A9-E50E24DCCA9E" 
            RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
        }

        showMessage("mBluetoothGatt null" + mBluetoothGatt);
        if (RxService == null) {
            showMessage("Rx service not found!");
            broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
            return false;
        }

        //???
        BluetoothGattCharacteristic RxChar = null;
        if (RxChar == null) {

            //"6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
            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);
        Log.d(TAG, "writeRXCharacteristic: " + value);

        //?
        boolean status = mBluetoothGatt.writeCharacteristic(RxChar);

        Log.e("Blue_Chanl", "write TXchar - status=" + status);
        if (!status) {
            try {
                Thread.sleep(500);
                status = mBluetoothGatt.writeCharacteristic(RxChar);
                Log.e("Blue_Chanl", "??write TXchar - status=" + status);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return true;
}