Example usage for android.bluetooth BluetoothGattService getCharacteristics

List of usage examples for android.bluetooth BluetoothGattService getCharacteristics

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattService getCharacteristics.

Prototype

public List<BluetoothGattCharacteristic> getCharacteristics() 

Source Link

Document

Returns a list of characteristics included in this service.

Usage

From source file:com.wolkabout.hexiwear.service.BluetoothService.java

private void storeCharacteristicsFromService(BluetoothGattService gattService) {
    for (BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
        final String characteristicUuid = gattCharacteristic.getUuid().toString();
        final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);

        if (characteristic == Characteristic.ALERT_IN) {
            Log.d(TAG, "ALERT_IN DISCOVERED");
            alertIn = gattCharacteristic;
            setTime();//from w  w  w.j  av a2 s .  c  o  m
            NotificationService_.intent(BluetoothService.this).start();
        } else if (characteristic != null) {
            Log.v(TAG, characteristic.getType() + ": " + characteristic.name());
            readableCharacteristics.put(characteristicUuid, gattCharacteristic);
        } else {
            Log.v(TAG, "UNKNOWN: " + characteristicUuid);
        }
    }
}

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

/**
 * Method to get required characteristics from service
 *///  ww  w.j  a v a 2s . c om
private void getGattData() {
    LinearLayout ll_layout = (LinearLayout) rootView.findViewById(R.id.linkloss_layout);
    LinearLayout im_layout = (LinearLayout) rootView.findViewById(R.id.immalert_layout);
    LinearLayout tp_layout = (LinearLayout) rootView.findViewById(R.id.transmission_layout);
    RelativeLayout tpr_layout = (RelativeLayout) rootView.findViewById(R.id.transmission_rel_layout);

    for (int position = 0; position < mExtraservice.size(); position++) {
        HashMap<String, BluetoothGattService> item = mExtraservice.get(position);
        BluetoothGattService bgs = item.get("UUID");
        List<BluetoothGattCharacteristic> gattCharacteristicsCurrent = bgs.getCharacteristics();
        for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristicsCurrent) {
            String uuidchara = gattCharacteristic.getUuid().toString();
            if (uuidchara.equalsIgnoreCase(GattAttributes.ALERT_LEVEL)) {
                if (bgs.getUuid().toString().equalsIgnoreCase(GattAttributes.LINK_LOSS_SERVICE)) {
                    ll_layout.setVisibility(View.VISIBLE);
                    mSpinnerLinkLoss = (CustomSpinner) rootView.findViewById(R.id.linkloss_spinner);
                    // Create an ArrayAdapter using the string array and a
                    // default
                    // spinner layout
                    ArrayAdapter<CharSequence> adapter_linkloss = ArrayAdapter.createFromResource(getActivity(),
                            R.array.findme_immediate_alert_array, android.R.layout.simple_spinner_item);
                    // Specify the layout to use when the list of choices
                    // appears
                    adapter_linkloss.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    // Apply the adapter to the spinner
                    mSpinnerLinkLoss.setAdapter(adapter_linkloss);
                    mSpinnerLinkLoss.setOnItemSelectedListener(new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                            if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("No Alert")) {
                                byte[] convertedBytes = convertingTobyteArray(IMM_NO_ALERT);
                                BluetoothLeService.writeCharacteristicNoresponse(gattCharacteristic,
                                        convertedBytes);
                                Toast.makeText(getActivity(),
                                        getResources().getString(R.string.find_value_written_toast)
                                                + IMM_NO_ALERT_TEXT
                                                + getResources().getString(R.string.find_value_success_toast),
                                        Toast.LENGTH_SHORT).show();
                            }
                            if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Mid Alert")) {
                                byte[] convertedBytes = convertingTobyteArray(IMM_MID_ALERT);
                                BluetoothLeService.writeCharacteristicNoresponse(gattCharacteristic,
                                        convertedBytes);
                                Toast.makeText(getActivity(),
                                        getResources().getString(R.string.find_value_written_toast)
                                                + IMM_MID_ALERT_TEXT
                                                + getResources().getString(R.string.find_value_success_toast),
                                        Toast.LENGTH_SHORT).show();
                            }
                            if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("High Alert")) {
                                byte[] convertedBytes = convertingTobyteArray(IMM_HIGH_ALERT);
                                BluetoothLeService.writeCharacteristicNoresponse(gattCharacteristic,
                                        convertedBytes);
                                Toast.makeText(getActivity(),
                                        getResources().getString(R.string.find_value_written_toast)
                                                + IMM_HIGH_ALERT_TEXT
                                                + getResources().getString(R.string.find_value_success_toast),
                                        Toast.LENGTH_SHORT).show();
                            }

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub

                        }
                    });
                }
                if (bgs.getUuid().toString().equalsIgnoreCase(GattAttributes.IMMEDIATE_ALERT_SERVICE)) {
                    im_layout.setVisibility(View.VISIBLE);
                    mSpinnerImmediateAlert = (CustomSpinner) rootView.findViewById(R.id.immediate_spinner);
                    // Create an ArrayAdapter using the string array and a
                    // default
                    // spinner layout
                    ArrayAdapter<CharSequence> adapter_immediate_alert = ArrayAdapter.createFromResource(
                            getActivity(), R.array.findme_immediate_alert_array,
                            android.R.layout.simple_spinner_item);
                    // Specify the layout to use when the list of choices
                    // appears
                    adapter_immediate_alert
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    // Apply the adapter to the spinner
                    mSpinnerImmediateAlert.setAdapter(adapter_immediate_alert);
                    mSpinnerImmediateAlert.setOnItemSelectedListener(new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                            if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("No Alert")) {
                                byte[] convertedBytes = convertingTobyteArray(IMM_NO_ALERT);
                                BluetoothLeService.writeCharacteristicNoresponse(gattCharacteristic,
                                        convertedBytes);
                                Toast.makeText(getActivity(),
                                        getResources().getString(R.string.find_value_written_toast)
                                                + IMM_NO_ALERT_TEXT
                                                + getResources().getString(R.string.find_value_success_toast),
                                        Toast.LENGTH_SHORT).show();
                            }
                            if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Mid Alert")) {
                                byte[] convertedBytes = convertingTobyteArray(IMM_MID_ALERT);
                                BluetoothLeService.writeCharacteristicNoresponse(gattCharacteristic,
                                        convertedBytes);
                                Toast.makeText(getActivity(),
                                        getResources().getString(R.string.find_value_written_toast)
                                                + IMM_MID_ALERT_TEXT
                                                + getResources().getString(R.string.find_value_success_toast),
                                        Toast.LENGTH_SHORT).show();
                            }
                            if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("High Alert")) {
                                byte[] convertedBytes = convertingTobyteArray(IMM_HIGH_ALERT);
                                BluetoothLeService.writeCharacteristicNoresponse(gattCharacteristic,
                                        convertedBytes);
                                Toast.makeText(getActivity(),
                                        getResources().getString(R.string.find_value_written_toast)
                                                + IMM_HIGH_ALERT_TEXT
                                                + getResources().getString(R.string.find_value_success_toast),
                                        Toast.LENGTH_SHORT).show();
                            }

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub

                        }
                    });
                }

            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.TRANSMISSION_POWER_LEVEL)) {
                tp_layout.setVisibility(View.VISIBLE);
                tpr_layout.setVisibility(View.VISIBLE);
                mReadCharacteristic_tp = gattCharacteristic;
                mTransmissionPower = (ImageView) rootView.findViewById(R.id.findme_tx_power_img);
                mTransmissionPowerValue = (TextView) rootView.findViewById(R.id.findme_tx_power_txt);
                if (mReadCharacteristic_tp != null) {
                    prepareBroadcastDataReadtp(mReadCharacteristic_tp);
                }

            }

        }
    }

}

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

public void onGattRead(BluetoothDevice device, BluetoothGattService service) {
    Map<String, byte[]> characteristics;
    ImmutableMap.Builder<String, byte[]> builder = new ImmutableMap.Builder();
    for (BluetoothGattCharacteristic c : service.getCharacteristics()) {
        builder.put(c.getUuid().toString(), c.getValue());
    }//from www. j  ava  2  s  .  c om
    characteristics = builder.build();

    synchronized (this) {
        if (mScanSeens == null) {
            return;
        }
        Integer rssi = mScanSeens.get(device);
        if (rssi == null) {
            return;
        }
        mScanHandler.onDiscovered(service.getUuid().toString(), characteristics, rssi);
        mOnServiceReadCallbacks++;
    }
}

From source file:edu.stanford.cs.sing.helena.DeviceControlActivity.java

private void checkServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null)
        return;/*from   w  w  w . jav  a  2 s  .co  m*/
    String uuid;
    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        uuid = gattService.getUuid().toString();
        //only interested in helena service
        if (uuid.equals(HelenaGattAttributes.HELENA_SERVICE)) {
            mHelenaService = gattService;
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
            // Loops through available Characteristics.
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                //only interested in this characteristic
                if (gattCharacteristic.getUuid().toString().equals(HelenaGattAttributes.UUID_LISTED_DEVICE)) {
                    mNotifyCharacteristic = gattCharacteristic;
                    final int charaProp = gattCharacteristic.getProperties();
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                        //Initiate notifications
                        mNotifyCharacteristic = gattCharacteristic;
                        mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
                    }

                }
            }

        } else {
            Log.d(TAG, "Skipping other services");
        }
    }
}

From source file:com.google.android.apps.forscience.ble.MyBleService.java

public void printServices(String address) {
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    if (bluetoothGatt == null) {
        Log.d(TAG, "No connection found for: " + address);
        return;/*from www .  ja v  a 2  s .  c  o  m*/
    }
    for (BluetoothGattService service : bluetoothGatt.getServices()) {
        Log.d(TAG, "Service ================================");
        Log.d(TAG, "Service UUID: " + service.getUuid());
        Log.d(TAG, "Service Type: " + service.getType());

        for (BluetoothGattCharacteristic charact : service.getCharacteristics()) {
            Log.d(TAG, "Charact UUID: " + charact.getUuid());
            Log.d(TAG, "Charact prop: " + charact.getProperties());

            if (charact.getValue() != null) {
                Log.d(TAG, "Charact Value: " + new String(charact.getValue()));
            }
        }
    }
}

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

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

    // Check for Notify first
    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0
                && characteristicUUID.equals(c.getUuid())) {
            characteristic = c;//from  w ww.  j  a v  a  2  s .co m
            break;
        }
    }

    if (characteristic != null)
        return characteristic;

    // If there wasn't Notify Characteristic, check for Indicate
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 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.megster.cordova.ble.central.Peripheral.java

public JSONObject asJSONObject(BluetoothGatt gatt) {

    JSONObject json = asJSONObject();/*from   w  w w . j  a v  a  2 s  .  c om*/

    try {
        JSONArray servicesArray = new JSONArray();
        JSONArray characteristicsArray = new JSONArray();
        json.put("services", servicesArray);
        json.put("characteristics", characteristicsArray);

        if (connected && gatt != null) {
            for (BluetoothGattService service : gatt.getServices()) {
                servicesArray.put(UUIDHelper.uuidToString(service.getUuid()));

                for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                    JSONObject characteristicsJSON = new JSONObject();
                    characteristicsArray.put(characteristicsJSON);

                    characteristicsJSON.put("service", UUIDHelper.uuidToString(service.getUuid()));
                    characteristicsJSON.put("characteristic",
                            UUIDHelper.uuidToString(characteristic.getUuid()));
                    //characteristicsJSON.put("instanceId", characteristic.getInstanceId());

                    characteristicsJSON.put("properties", Helper.decodeProperties(characteristic));
                    // characteristicsJSON.put("propertiesValue", characteristic.getProperties());

                    if (characteristic.getPermissions() > 0) {
                        characteristicsJSON.put("permissions", Helper.decodePermissions(characteristic));
                        // characteristicsJSON.put("permissionsValue", characteristic.getPermissions());
                    }

                    JSONArray descriptorsArray = new JSONArray();

                    for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                        JSONObject descriptorJSON = new JSONObject();
                        descriptorJSON.put("uuid", UUIDHelper.uuidToString(descriptor.getUuid()));
                        descriptorJSON.put("value", descriptor.getValue()); // always blank

                        if (descriptor.getPermissions() > 0) {
                            descriptorJSON.put("permissions", Helper.decodePermissions(descriptor));
                            // descriptorJSON.put("permissionsValue", descriptor.getPermissions());
                        }
                        descriptorsArray.put(descriptorJSON);
                    }
                    if (descriptorsArray.length() > 0) {
                        characteristicsJSON.put("descriptors", descriptorsArray);
                    }
                }
            }
        }
    } catch (JSONException e) { // TODO better error handling
        e.printStackTrace();
    }

    return json;
}

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   ww  w.  j  av  a 2 s. 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:com.wearme.fat.ui.MainActivity.java

private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null)
        return;//from   w  ww.ja v  a 2 s. co m
    for (BluetoothGattService gattService : gattServices) {
        //f433bd80
        //06aba6de
        if (gattService.getUuid().toString().startsWith("f433bd80")) {
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                if (gattCharacteristic.getUuid().toString().startsWith("29f11080")) {

                    GattAttributes.mobileToEquipmentCharacteristic = gattCharacteristic;

                } else if (gattCharacteristic.getUuid().toString().startsWith("1a2ea400")) {
                    mBluetoothLeService.setCharacteristicNotification(gattCharacteristic, true);
                }
            }
        }

        if (gattService.getUuid().toString().startsWith("06aba6de")) {
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                if (gattCharacteristic.getUuid().toString().startsWith("29f11080")) {

                    GattAttributes.mobileToEquipmentCharacteristic = gattCharacteristic;

                } else if (gattCharacteristic.getUuid().toString().startsWith("1a2ea400")) {
                    mBluetoothLeService.setCharacteristicNotification(gattCharacteristic, true);
                }
            }
        }
    }
}

From source file:com.beestar.ble.ble.ui.BleScanActivity.java

private void setBleServiceListener() {
    mBleService.setOnServicesDiscoveredListener(new BleService.OnServicesDiscoveredListener() {
        @Override//from  w w  w.  j  a va 2 s  .  c  om
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                gattServiceList = gatt.getServices();
                serviceList.clear();
                for (BluetoothGattService service : gattServiceList) {
                    String serviceUuid = service.getUuid().toString();
                    serviceList.add(MyGattAttributes.lookup(serviceUuid, "Unknown") + "\n" + serviceUuid);
                    Log.i(TAG, MyGattAttributes.lookup(serviceUuid, "Unknown") + "\n" + serviceUuid);

                    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                    String[] charArra = new String[characteristics.size()];
                    for (int i = 0; i < characteristics.size(); i++) {
                        String charUuid = characteristics.get(i).getUuid().toString();
                        charArra[i] = MyGattAttributes.lookup(charUuid, "Unknown") + "\n" + charUuid;
                    }
                    characteristicList.add(charArra);
                }
                mHandler.sendEmptyMessage(SERVICE_SHOW);
            }
        }
    });
    //        //Ble??
    //        mBleService.setOnLeScanListener(new BleService.OnLeScanListener() {
    //            @Override
    //            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
    //                //???Ble?????
    //            }
    //        });
    //        //Ble
    //        mBleService.setOnConnectListener(new BleService.OnConnectListener() {
    //            @Override
    //            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    //                if (newState == BluetoothProfile.STATE_DISCONNECTED) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_CONNECTING) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_CONNECTED) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
    //                    //Ble
    //                }
    //            }
    //        });
    //        //Ble??
    //        mBleService.setOnServicesDiscoveredListener(new BleService.OnServicesDiscoveredListener() {
    //            @Override
    //            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    //
    //            }
    //        });
    //        //Ble?
    //        mBleService.setOnDataAvailableListener(new BleService.OnDataAvailableListener() {
    //            @Override
    //            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    //                //???
    //            }
    //
    //            @Override
    //            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    //                //??
    //            }
    //        @Override
    //        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    //
    //        }
    //        });

    mBleService.setOnReadRemoteRssiListener(new BleService.OnReadRemoteRssiListener() {
        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            Log.i(TAG, "onReadRemoteRssi: rssi = " + rssi);
        }
    });
}