Example usage for android.bluetooth BluetoothGattCharacteristic PROPERTY_READ

List of usage examples for android.bluetooth BluetoothGattCharacteristic PROPERTY_READ

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattCharacteristic PROPERTY_READ.

Prototype

int PROPERTY_READ

To view the source code for android.bluetooth BluetoothGattCharacteristic PROPERTY_READ.

Click Source Link

Document

Characteristic property: Characteristic is readable.

Usage

From source file:Main.java

public static JSONArray decodeProperties(BluetoothGattCharacteristic characteristic) {

    // NOTE: props strings need to be consistent across iOS and Android
    JSONArray props = new JSONArray();
    int properties = characteristic.getProperties();

    if ((properties & BluetoothGattCharacteristic.PROPERTY_BROADCAST) != 0x0) {
        props.put("Broadcast");
    }// w w w . j  av  a 2  s.c o m

    if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) != 0x0) {
        props.put("Read");
    }

    if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0x0) {
        props.put("WriteWithoutResponse");
    }

    if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0x0) {
        props.put("Write");
    }

    if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0x0) {
        props.put("Notify");
    }

    if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0x0) {
        props.put("Indicate");
    }

    if ((properties & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) != 0x0) {
        // Android calls this "write with signature", using iOS name for now
        props.put("AuthenticateSignedWrites");
    }

    if ((properties & BluetoothGattCharacteristic.PROPERTY_EXTENDED_PROPS) != 0x0) {
        props.put("ExtendedProperties");
    }

    //      iOS only?
    //
    //            if ((p & CBCharacteristicPropertyNotifyEncryptionRequired) != 0x0) {  // 0x100
    //                [props addObject:@"NotifyEncryptionRequired"];
    //            }
    //
    //            if ((p & CBCharacteristicPropertyIndicateEncryptionRequired) != 0x0) { // 0x200
    //                [props addObject:@"IndicateEncryptionRequired"];
    //            }

    return props;
}

From source file:com.example.emulator.EmulatorFragment.java

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public EmulatorFragment() {
    mBatteryLevelCharacteristic = new BluetoothGattCharacteristic(BATTERY_LEVEL_UUID,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PERMISSION_READ);

    mBatteryLevelCharacteristic.addDescriptor(Peripheral.getClientCharacteristicConfigurationDescriptor());

    mBatteryLevelCharacteristic/*w w  w.  j a va 2 s  . c o  m*/
            .addDescriptor(Peripheral.getCharacteristicUserDescriptionDescriptor(BATTERY_LEVEL_DESCRIPTION));

    mBatteryService = new BluetoothGattService(BATTERY_SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY);
    mBatteryService.addCharacteristic(mBatteryLevelCharacteristic);
}

From source file:kr.ac.kaist.resl.sensorservice.BluetoothService.java

private boolean readCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    if (null == characteristic)
        return false;

    final int charaProp = characteristic.getProperties();

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
        // If there is an active notification on a characteristic,
        // clear it first so it doesn't update the data field on the user interface.
        if (mNotifyCharacteristic != null) {
            bleService.setCharacteristicNotification(gatt, mNotifyCharacteristic, false);
            mNotifyCharacteristic = null;
        }/*from   w  ww  . ja va 2s.  com*/

        //JS: if no data is displayed, immediately read data from the connected sensor.
        gatt.readCharacteristic(characteristic);
    }

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        mNotifyCharacteristic = characteristic;
        bleService.setCharacteristicNotification(gatt, characteristic, true);
    }

    return true;
}

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

public void addService(final String uuid, Map<String, byte[]> characteristics) {
    BluetoothGattService service = new BluetoothGattService(UUID.fromString(uuid),
            BluetoothGattService.SERVICE_TYPE_PRIMARY);
    for (Map.Entry<String, byte[]> entry : characteristics.entrySet()) {
        BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(
                UUID.fromString(entry.getKey()), BluetoothGattCharacteristic.PROPERTY_READ,
                BluetoothGattCharacteristic.PERMISSION_READ);
        characteristic.setValue(entry.getValue());
        service.addCharacteristic(characteristic);
    }//ww w. j ava 2  s .c o  m

    synchronized (this) {
        if (mServices.put(uuid, service) != null) {
            throw new IllegalStateException("already being advertised: " + uuid);
        }
        if (mEnabled) {
            startAdvertising(service);
        }
    }
}

From source file:com.cypress.cysmart.BLEServiceFragments.FindMeService.java

/**
 * Prepare Broadcast receiver to broadcast read characteristics Transmission
 * power/*from   ww w .  j  av  a 2 s .  c om*/
 *
 * @param gattCharacteristic
 */
void prepareBroadcastDataReadtp(BluetoothGattCharacteristic gattCharacteristic) {
    final BluetoothGattCharacteristic characteristic = gattCharacteristic;
    final int charaProp = characteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
        mReadCharacteristic_tp = characteristic;
        BluetoothLeService.readCharacteristic(characteristic);
    }
}

From source file:ti.android.ble.devicemonitor.ServiceView.java

void setItem(int pos) {
    boolean fHasRead;
    boolean fHasNotification;
    boolean fHasWrite;
    BluetoothGattDescriptor clientConfig;

    mChar = mCharList.get(pos);// w w  w.ja v a 2 s  .  c o m
    mProperty = mChar.getProperties();

    fHasRead = (mProperty & BluetoothGattCharacteristic.PROPERTY_READ) > 0;
    fHasNotification = (mProperty & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0;
    fHasWrite = (mProperty & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0;

    // Activate input widgets according to char. property
    mBtnRead.setEnabled(fHasRead);
    mBtnWrite.setEnabled(fHasWrite);
    mData.setEnabled(fHasWrite);
    mBtnNotify.setEnabled(fHasNotification);

    if (fHasNotification) {
        mBtnNotify.setTextAppearance(mContext, R.style.checkboxStyle);
        mBtnNotify.setChecked(mGattService.isNotificationEnabled(mChar));
    } else
        mBtnNotify.setTextAppearance(mContext, R.style.checkboxStyle_Disabled);

    if (fHasWrite) {
        mData.setHint(R.string.write_hint);
    } else {
        mData.setHint("");
    }

    clientConfig = mChar.getDescriptor(GattInfo.CLIENT_CHARACTERISTIC_CONFIG);
    if (clientConfig != null) {
        int perm = clientConfig.getPermissions();
        Log.d(TAG, "perm= " + perm);
    }
    setStatus(GattInfo.uuidToName(mChar.getUuid()));

    // Read the characteristic (if applicable)
    if (fHasRead) {
        mBtGatt.readCharacteristic(mChar);
    }

    // Make sure selection is highlighted
    mCharAdapter.setSelectedPosition(pos);
    mActivity.onSelectionUpdate(mChar);
}

From source file:com.cypress.cysmart.BLEServiceFragments.BatteryInformationService.java

/**
 * Method to get required characteristics from service
 *//* ww  w  . j  av a2s. co  m*/
void getGattData() {
    List<BluetoothGattCharacteristic> gattCharacteristics = mService.getCharacteristics();
    for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
        String uuidchara = gattCharacteristic.getUuid().toString();
        if (uuidchara.equalsIgnoreCase(GattAttributes.BATTERY_LEVEL)) {
            mReadCharacteristic = gattCharacteristic;
            mNotifyCharacteristic = gattCharacteristic;

            /**
             * Checking the various GattCharacteristics and listing in the ListView
             */
            if (checkCharacteristicsPropertyPresence(gattCharacteristic.getProperties(),
                    BluetoothGattCharacteristic.PROPERTY_READ)) {
                mReadButton.setVisibility(View.VISIBLE);
            }
            if (checkCharacteristicsPropertyPresence(gattCharacteristic.getProperties(),
                    BluetoothGattCharacteristic.PROPERTY_NOTIFY)) {
                mNotifyButton.setVisibility(View.VISIBLE);
            }
            prepareBroadcastDataRead(gattCharacteristic);
            break;
        }
    }
}

From source file:org.physical_web.physicalweb.FatBeaconBroadcastService.java

private void initGattServer() {
    mGattServer = mBluetoothManager.openGattServer(this, mGattServerCallback);
    BluetoothGattService service = new BluetoothGattService(UUID.fromString(SERVICE_UUID),
            BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic webpage = new BluetoothGattCharacteristic(CHARACTERISTIC_WEBPAGE_UUID,
            BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);
    service.addCharacteristic(webpage);/* w w  w  . j a  v  a  2  s . c  o m*/
    mGattServer.addService(service);
}

From source file:com.ucai.test.control.DeviceControlActivity.java

/**
 * GATT?,?//from  w ww  .j  av  a2  s .c o  m
 * ??
 * http://d.android.com/reference/android/bluetooth/BluetoothGatt.html
 * ??
 */
/* private final ExpandableListView.OnChildClickListener servicesListClickListner =
    new ExpandableListView.OnChildClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                    int childPosition, long id) {
            if (mGattCharacteristics != null) {
                final BluetoothGattCharacteristic characteristic =
                        mGattCharacteristics.get(groupPosition).get(childPosition);
                final int charaProp = characteristic.getProperties();
                if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                    // If there is an active notification on a characteristic, clear
                    // it first so it doesn't update the data field on the user interface.
                    if (mNotifyCharacteristic != null) {
                        mBluetoothLeService.setCharacteristicNotification(
                                mNotifyCharacteristic, false);
                        mNotifyCharacteristic = null;
                    }
                    mBluetoothLeService.readCharacteristic(characteristic);
                }
                if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                    mNotifyCharacteristic = characteristic;
                    mBluetoothLeService.setCharacteristicNotification(
                            characteristic, true);
                }
                return true;
            }
            return false;
        }
    };*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void autoGetTemperature() {
    int groupPosition = 0;
    int childPosition = 0;
    Log.e("cst", "autoGetTemperature,mDeviceName=" + mDeviceName);
    if (SampleGattAttributes.DEVICE_NAME_TEMPERATURE.equals(mDeviceName)) {
        groupPosition = 2;
        childPosition = 2;
        mDeviceDataName.setText(R.string.device_temperature);
    } else if (SampleGattAttributes.DEVICE_NAME_WEIGHT.equals(mDeviceName)) {
        groupPosition = 1;
        childPosition = 0;
        mDeviceDataName.setText(R.string.device_weight);
    } else if (SampleGattAttributes.DEVICE_NAME_YUNMAI_WEIGHT.equals(mDeviceName)) {
        groupPosition = 2;
        childPosition = 0;
        mDeviceDataName.setText(R.string.device_weight);
    } else {
        return;
    }
    if (mGattCharacteristics != null) {
        final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(groupPosition)
                .get(childPosition);
        final int charaProp = characteristic.getProperties();
        Log.e("cst", "autoGetTemperature,groupPosition=" + groupPosition + ",childPosition=" + childPosition
                + ",charaProp=" + charaProp + ",PROPERTY_READ=" + BluetoothGattCharacteristic.PROPERTY_READ
                + ",PROPERTY_NOTIFY=" + BluetoothGattCharacteristic.PROPERTY_NOTIFY);
        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
            // If there is an active notification on a characteristic, clear
            // it first so it doesn't update the data field on the user interface.
            if (mNotifyCharacteristic != null) {
                mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
                mNotifyCharacteristic = null;
            }
            Log.e("cst", "autoGetTemperature,readCharacteristic(" + characteristic + ")");
            mBluetoothLeService.readCharacteristic(characteristic);

            //                            Log.e("cst", "OnChildClickListener,setCharacteristicNotification("+characteristic+",true)");
            //                            mBluetoothLeService.setCharacteristicNotification(characteristic,true);
        }
        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
            mNotifyCharacteristic = characteristic;
            mBluetoothLeService.setCharacteristicNotification(characteristic, true);
        }
    }
}

From source file:com.cypress.cysmart.BLEServiceFragments.BatteryInformationService.java

/**
 * Preparing Broadcast receiver to broadcast read characteristics
 *
 * @param gattCharacteristic/*from  w  ww . j  a va  2  s  .  c  o m*/
 */
void prepareBroadcastDataRead(BluetoothGattCharacteristic gattCharacteristic) {
    final int charaProp = gattCharacteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
        mReadCharacteristic = gattCharacteristic;
        BluetoothLeService.readCharacteristic(gattCharacteristic);
    }
}