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:de.dmarcini.bt.homelight.HomeLightMainActivity.java

/**
 * Iteriere durch die gefundenen Servivces des entfernten Gertes und finde UART Service
 *
 * @param gattServices Liste von services
 *//*from w ww.java  2s.  co  m*/
private void reconGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) {
        return;
    }
    String uuid;
    String unknownServiceString = getResources().getString(R.string.main_ble_unknown_service);
    //
    // durchsuche die verfgbaren Services
    //
    for (BluetoothGattService gattService : gattServices) {
        uuid = gattService.getUuid().toString();
        //
        // Gibt es den UART Servive, dann gib Bescheid!
        //
        if (HM10GattAttributes.lookup(uuid, unknownServiceString).equals("HM 10 Serial")) {
            btConfig.setIsUART(true);
        } else {
            btConfig.setIsUART(false);
        }
        // get characteristic when UUID matches RX/TX UUID
        btConfig.setCharacteristicTX(gattService.getCharacteristic(ProjectConst.UUID_HM_RX_TX));
        btConfig.setCharacteristicRX(gattService.getCharacteristic(ProjectConst.UUID_HM_RX_TX));
        //
        // wenn die Kommunikation sichergestellt ist, frage nach dem Modul
        //
        askModulForType();
    }

}

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

private void getGattService(BluetoothGattService gattService) {
    if (gattService == null)
        return;//from w  ww  .ja  va  2s.c  om

    setButtonEnable();

    updateBloomRGB();

    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:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

/**
 * Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported
 * asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
 * callback.//from  www . ja  v  a 2s .c o  m
 *
 */
public boolean readCharacteristic(String address, String serviceUuid, String characteristicUuid) {
    Log.d(TAG, "readCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = "
            + characteristicUuid);
    if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid)
            || StringUtils.isEmptyString(characteristicUuid)) {
        Log.w(TAG, "Unknown parameter");
        return false;
    }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);
    if (mBluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }
    BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUuid));
    if (service == null) {
        Log.w(TAG, "Service not found.");
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUuid));
    if (characteristic == null) {
        Log.w(TAG, "characteristic not found.");
        return false;
    }
    boolean result = bluetoothGatt.readCharacteristic(characteristic);
    Log.d(TAG, "Read charac uuid = " + characteristic.getUuid().toString() + ", result = " + result);

    return result;
}

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

public boolean writeRemoteCharacteristic(String address, String serviceUuid, String characteristicUuid,
        byte[] value) {
    Log.d(TAG, "writeRemoteCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = "
            + characteristicUuid);//from   w w w . j  a  va2  s .com
    if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid)
            || StringUtils.isEmptyString(characteristicUuid)) {
        Log.w(TAG, "Unknown parameter");
        return false;
    }
    if (value == null) {
        Log.w(TAG, "value is empty");
        return false;
    }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);

    if (mBluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }
    BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUuid));
    if (service == null) {
        Log.w(TAG, "Service not found.");
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUuid));
    if (characteristic == null) {
        Log.w(TAG, "characteristic not found.");
        return false;
    }

    characteristic.setValue(value);
    boolean result = bluetoothGatt.writeCharacteristic(characteristic);
    Log.d(TAG, "Write charac uuid = " + characteristic.getUuid().toString() + ", result = " + result);

    return result;
}

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

/**
 * Enables or disables notification on a give characteristic.
 *
 * @param enabled If true, enable notification.  False otherwise.
 *//*from   w w  w . j  a  v  a2 s.  c  o  m*/
public boolean setCharacteristicNotification(String address, String serviceUuid, String characteristicUuid,
        boolean enabled) {
    Log.d(TAG, "writeRemoteCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = "
            + characteristicUuid);
    if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid)
            || StringUtils.isEmptyString(characteristicUuid)) {
        Log.w(TAG, "Unknown parameter");
        return false;
    }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);
    if (mBluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }
    BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUuid));
    if (service == null) {
        Log.w(TAG, "Service not found.");
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUuid));
    if (characteristic == null) {
        Log.w(TAG, "characteristic not found.");
        return false;
    }

    bluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    final int charaProp = characteristic.getProperties();
    if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        if (descriptor != null) {
            Log.d(TAG, ">>>> ENABLE_NOTIFICATION_VALUE : " + characteristic.getUuid().toString());
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            bluetoothGatt.writeDescriptor(descriptor);

            return true;
        } else {
            return false;
        }
    } else if ((charaProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        if (descriptor != null) {
            Log.d(TAG, ">>>> ENABLE_INDICATION_VALUE : " + characteristic.getUuid().toString());
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
            bluetoothGatt.writeDescriptor(descriptor);

            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

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

public void RequestDeviceInfomationService(UUID RequestCharacteristic) {
    boolean result = false;
    if (mBluetoothGatt != null) {
        BluetoothGattService disService = mBluetoothGatt.getService(DEVICE_INFOMATION_SERVICE);
        if (disService == null) {
            showMessage("Dis service not found!");
            return;
        }//  ww  w  .j  a v a  2s. c om

        BluetoothGattCharacteristic IdCharacteristic = disService.getCharacteristic(RequestCharacteristic);
        if (IdCharacteristic == null) {
            showMessage("dis service " + RequestCharacteristic.toString() + " charateristic not found!");
            return;
        }

        mRequestCompleted = false;
        result = mBluetoothGatt.readCharacteristic(IdCharacteristic);
        if (result == false) {
            showMessage(RequestCharacteristic.toString() + " reading is failed!");
        }

        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.megster.cordova.ble.central.Peripheral.java

private BluetoothGattCharacteristic findWritableCharacteristic(BluetoothGattService service,
        UUID characteristicUUID, int writeType) {
    BluetoothGattCharacteristic characteristic = null;

    // get write property
    int writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE;
    if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
        writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
    }//from  ww w  . ja  v  a 2s .  co m

    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & writeProperty) != 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.umundus.service.NCallServiceOld.java

public void writeRemoconPatternCharacteristic(byte[] value) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "writeRemoconCharacteristic BluetoothAdapter not initialized");
        return;/*  ww w  .j a  va 2s. com*/
    }

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

    BluetoothGattCharacteristic RemoconChar = RemoconService
            .getCharacteristic(NCALL_REMOCON_SEND_DATA_CHARACTERISTIC);
    if (RemoconChar == null) {
        showMessage("remocon control charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }

    Log.i(TAG, "writeRemoconPatternCharacteristic ++");
    RemoconChar.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(RemoconChar);
    Log.i(TAG, "writeRemoconPatternCharacteristic --");
}

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

/**
 * Enabl Battery Notification/*ww w  .java2 s .co  m*/
 * 
 */
public void enableBatteryNotification() {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "enableBatteryNotification BluetoothAdapter not initialized");
        return;
    }

    BluetoothGattService BatteryService = mBluetoothGatt.getService(BATTERY_SERVICE);
    if (BatteryService == null) {
        showMessage("Battery service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }
    BluetoothGattCharacteristic BatteryChar = BatteryService.getCharacteristic(BATTERY_CHARACTERISTIC);
    if (BatteryChar == null) {
        showMessage("Battery charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(BatteryChar, true);

    mRequestCompleted = false;

    BluetoothGattDescriptor descriptor = BatteryChar.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.umundus.service.NCallServiceOld.java

public void writeRemoconCharacteristic(byte[] value) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "writeRemoconCharacteristic BluetoothAdapter not initialized");
        return;/* w w  w  . j  a v  a2s .  com*/
    }

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

    BluetoothGattCharacteristic RemoconChar = RemoconService
            .getCharacteristic(NCALL_REMOCON_SEND_DATA_CHARACTERISTIC);
    if (RemoconChar == null) {
        showMessage("remocon control charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC);
        return;
    }

    mRequestCompleted = false;

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

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