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:Main.java

public static BluetoothGattCharacteristic findBluetoothGattCharacteristic(BluetoothGatt gatt, UUID serviceUuid,
        UUID characteristicUuid) {
    if (gatt == null) {
        return null;
    }//from  w w w .j  a  v  a 2 s.co  m
    BluetoothGattService service = gatt.getService(serviceUuid);
    if (service == null) {
        return null;
    }

    return service.getCharacteristic(characteristicUuid);
}

From source file:com.orange.beaconme_sdk.ble.control.BeaconTagDeviceUpdater.java

private void readCharacteristics() {
    Log.i("Updater", "readCharacteristics");
    for (WriteCharacteristicCommand command : commands) {
        final UUID characteristicUUID = command.getCharacteristicUUID();
        BluetoothGattService service = getGatt().getService(command.getServiceUUID());
        if (service != null) {
            BluetoothGattCharacteristic c = service.getCharacteristic(command.getCharacteristicUUID());
            if (c == null) {
                doneUploadingUuid(characteristicUUID);
            } else {
                queue(getReadCharacteristicOperation(c));
            }//from   w  ww  . j a v a 2 s.com
        } else if (command.getServiceUUID().equals(BeaconTagDevice.WAKE_UP_SERVICE_UUID)
                && allowSkipAdvancedService) {
            doneUploadingUuid(characteristicUUID);
        } else {
            close();
            return;
        }
    }
}

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 w  w w  . ja  va 2  s.co  m*/
        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.megster.cordova.rfduino.Peripheral.java

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(TAG, "gatt " + gatt);
    Log.d(TAG, "status " + status);
    super.onServicesDiscovered(gatt, status);

    BluetoothGattService service = gatt.getService(RFDUINO_SERVICE_UUID);
    Log.d(TAG, "service " + service);

    BluetoothGattCharacteristic receiveCharacteristic = service.getCharacteristic(RECEIVE_CHARACTERISTIC_UUID);
    sendCharacteristic = service.getCharacteristic(SEND_CHARACTERISTIC_UUID);
    disconnectCharacteristic = service.getCharacteristic(DISCONNECT_CHARACTERISTIC_UUID);

    if (receiveCharacteristic != null) {
        gatt.setCharacteristicNotification(receiveCharacteristic, true);

        BluetoothGattDescriptor receiveConfigDescriptor = receiveCharacteristic
                .getDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID);
        if (receiveConfigDescriptor != null) {
            receiveConfigDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(receiveConfigDescriptor);
        } else {// w w  w  .  j a  v  a 2  s . c o m
            LOG.e(TAG, "Receive Characteristic can not be configured.");
        }
    } else {
        LOG.e(TAG, "Receive Characteristic is missing.");
    }

    // call the success callback for connect
    PluginResult result = new PluginResult(PluginResult.Status.OK);
    result.setKeepCallback(true);
    connectCallback.sendPluginResult(result);
}

From source file:com.wowwee.util.UartService.java

public void writeRXCharacteristic(byte[] value) {

    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;//from   www  .  j  a v  a2  s.co 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);
}

From source file:com.example.alexanderbolinsky.ble_uart_remote.UartService.java

/**
 * Enable TXNotification/*from www. j  ava  2 s  .  co m*/
 *
 * @return
 */
public void enableTXNotification() {

    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    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;
    }
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);

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

}

From source file:com.shenqu.jlplayer.nRFUARTv2.UartService.java

/**
 * Enable TXNotification/*w w  w  .ja  v a2s. c o  m*/
 *
 * @return
 */
public void enableTXNotification() {
    /*
    if (mBluetoothGatt == null) {
      showMessage("mBluetoothGatt null" + mBluetoothGatt);
      broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
      return;
    }
    */
    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    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;
    }
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);

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

From source file:com.example.alan.bikelog.UartService.java

/**
 * Enable TXNotification/*from ww  w . j  av  a 2 s. c om*/
 *
 * @return 
 */
public void enableTXNotification() {
    /*
    if (mBluetoothGatt == null) {
       showMessage("mBluetoothGatt null" + mBluetoothGatt);
       broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
       return;
    }
       */
    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    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;
    }
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);

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

}

From source file:com.shenqu.jlplayer.nRFUARTv2.UartService.java

public boolean writeRXCharacteristic(byte[] value) {
    //Log.i(TAG, System.currentTimeMillis() + " write RXchar " + HexCodec.HexEncodeStr(value, false));
    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return false;
    }/*from w  w w  .j a v  a2  s  .co  m*/
    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);
    //        if (!status)
    //            return status;
    //        BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
    //        if (TxChar != null) {
    //            readCharacteristic(TxChar);
    //            Log.i(TAG, System.currentTimeMillis() + " write TXchar - status = " + status + " TxChar = " + TxChar.getStringValue(0));
    //        }
    return status;
}

From source file:com.cardiograph.service.UartService.java

/**
 * Enable TXNotification//from   w ww.j  av a  2s  .  co  m
 *
 * @return 
 */
public void enableTXNotification() {

    if (mBluetoothGatt == null) {
        showMessage("mBluetoothGatt null" + mBluetoothGatt);
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }

    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    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;
    }
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);

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

}