Example usage for android.bluetooth BluetoothGattCharacteristic PROPERTY_NOTIFY

List of usage examples for android.bluetooth BluetoothGattCharacteristic PROPERTY_NOTIFY

Introduction

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

Prototype

int PROPERTY_NOTIFY

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

Click Source Link

Document

Characteristic property: Characteristic supports notification

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");
    }/*from   ww  w .j  a  v  a2 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/*from  w  ww  . j  ava  2s  .co 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;
        }/* w  w w .  ja v a2 s . c o m*/

        //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:com.cypress.cysmart.BLEServiceFragments.GlucoseService.java

/**
 * Stopping Broadcast receiver to broadcast notify characteristics
 *
 * @param gattCharacteristic/*  w  w w.ja  v a2 s  . c  om*/
 */
void stopBroadcastDataNotify(BluetoothGattCharacteristic gattCharacteristic) {
    final BluetoothGattCharacteristic characteristic = gattCharacteristic;
    final int charaProp = characteristic.getProperties();

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        if (mNotifyCharacteristic != null) {
            BluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
        }

    }

}

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

/**
 * Preparing Broadcast receiver to broadcast notify characteristics
 *
 * @param gattCharacteristic/*from w ww.  ja  v a2s .c o  m*/
 */
void prepareBroadcastDataNotify(BluetoothGattCharacteristic gattCharacteristic) {
    final BluetoothGattCharacteristic characteristic = gattCharacteristic;
    final int charaProp = characteristic.getProperties();

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

}

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);//from w w w  .j  a 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.GATTDBFragments.GattDescriptorDetails.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.gattdb_descriptor_details, container, false);
    this.mContainer = container;
    mApplication = (CySmartApplication) getActivity().getApplication();
    mCharacteristicName = (TextView) rootView.findViewById(R.id.txtcharacteristicname);
    mDescriptorName = (TextView) rootView.findViewById(R.id.txtdescriptorname);
    mDescriptorValue = (TextView) rootView.findViewById(R.id.txtdescriptorvalue);
    mHexValue = (TextView) rootView.findViewById(R.id.txtdescriptorHexvalue);
    mBackBtn = (ImageView) rootView.findViewById(R.id.imgback);

    mProgressDialog = new ProgressDialog(getActivity());

    mBluetoothGattCharacteristic = mApplication.getBluetoothgattcharacteristic();
    String CharacteristicUUID = mBluetoothGattCharacteristic.getUuid().toString();
    mCharacteristicName.setText(GattAttributes.lookup(CharacteristicUUID, CharacteristicUUID));

    mDescriptor = mApplication.getBluetoothgattDescriptor();
    String DescriptorUUID = mDescriptor.getUuid().toString();
    mDescriptorName.setText(GattAttributes.lookup(DescriptorUUID, DescriptorUUID));

    mReadButton = (Button) rootView.findViewById(R.id.btn_read);
    mNotifyButton = (Button) rootView.findViewById(R.id.btn_write_notify);
    mIndicateButton = (Button) rootView.findViewById(R.id.btn_write_indicate);
    if (DescriptorUUID.equalsIgnoreCase(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG)) {
        if (getGattCharacteristicsPropertices(mBluetoothGattCharacteristic.getProperties(),
                BluetoothGattCharacteristic.PROPERTY_NOTIFY)) {
            mNotifyButton.setVisibility(View.VISIBLE);
            mNotifyButton.setText(getResources().getString(R.string.gatt_services_notify));
        }//w w  w .j a va  2 s .  c o m
        if (getGattCharacteristicsPropertices(mBluetoothGattCharacteristic.getProperties(),
                BluetoothGattCharacteristic.PROPERTY_INDICATE)) {
            mIndicateButton.setVisibility(View.VISIBLE);
            mIndicateButton.setText(getResources().getString(R.string.gatt_services_indicate));
        }
    } else {
        mNotifyButton.setVisibility(View.GONE);
    }
    startNotifyText = getResources().getString(R.string.gatt_services_notify);
    stopNotifyText = getResources().getString(R.string.gatt_services_stop_notify);
    startIndicateText = getResources().getString(R.string.gatt_services_indicate);
    stopIndicateText = getResources().getString(R.string.gatt_services_stop_indicate);
    mReadButton.setOnClickListener(this);
    mNotifyButton.setOnClickListener(this);
    mIndicateButton.setOnClickListener(this);
    mBackBtn.setOnClickListener(this);
    if (mDescriptor != null) {
        BluetoothLeService.readDescriptor(mDescriptor);
    }
    return rootView;
}

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

/**
 * Method to get required characteristics from service
 *//*  www  . j  av a 2s.  com*/
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:com.cypress.cysmart.BLEServiceFragments.CapsenseService.java

/**
 * Stopping Broadcast receiver to broadcast notify characteristics
 *
 * @param gattCharacteristic//from www.ja  v  a  2  s .  c o  m
 */
private static void stopBroadcastDataNotify(BluetoothGattCharacteristic gattCharacteristic) {
    final BluetoothGattCharacteristic characteristic = gattCharacteristic;
    final int charaProp = characteristic.getProperties();

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        if (characteristic != null) {
            Logger.d("Stopped notification");
            BluetoothLeService.setCharacteristicNotification(characteristic, false);
        }

    }

}

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

/**
 * GATT?,?// ww  w . j a  v  a2 s .co 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);
        }
    }
}