Java Code Examples for android.bluetooth.BluetoothGattService#getCharacteristics()

The following examples show how to use android.bluetooth.BluetoothGattService#getCharacteristics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyBleService.java    From science-journal with Apache License 2.0 6 votes vote down vote up
/**
 * FOR DEBUGGING ONLY. This should never be called from production code; we don't want this data
 * in our logs.
 */
@SuppressWarnings("UnusedDeclaration")
public void printServices(String address) {
  if (!DEBUG) {
    return;
  }
  BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
  if (bluetoothGatt == null) {
    Log.d(TAG, "No connection found for: " + address);
    return;
  }
  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()));
      }
    }
  }
}
 
Example 2
Source File: LeFunService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onServicesDiscovered(RxBleDeviceServices services) {
    boolean found = false;
    for (BluetoothGattService service : services.getBluetoothGattServices()) {
        UserError.Log.d(TAG, "Service: " + getUUIDName(service.getUuid()));
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            UserError.Log.d(TAG, "-- Character: " + getUUIDName(characteristic.getUuid()));

            for (final UUID check : huntCharacterstics) {
                if (characteristic.getUuid().equals(check)) {
                    I.readCharacteristic = check;
                    found = true;
                }
            }
        }
    }
    if (found) {
        I.isDiscoveryComplete = true;
        enableNotification();
    } else {
        UserError.Log.e(TAG, "Could not find characteristic during service discovery. This is very unusual");
    }
}
 
Example 3
Source File: BluetoothLeDeviceBase.java    From BlogPracticeDems with Apache License 2.0 6 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        List<BluetoothGattService> services = mBluetoothGatt.getServices();
        for (int i = 0; i < services.size(); i++) {
            HashMap<String, BluetoothGattCharacteristic> charMap = new HashMap<>();
            BluetoothGattService bluetoothGattService = services.get(i);
            String serviceUuid = bluetoothGattService.getUuid().toString();
            List<BluetoothGattCharacteristic> characteristics = bluetoothGattService.getCharacteristics();
            for (int j = 0; j < characteristics.size(); j++) {
                charMap.put(characteristics.get(j).getUuid().toString(), characteristics.get(j));
            }
            servicesMap.put(serviceUuid, charMap);
        }
        BluetoothGattCharacteristic bluetoothGattCharacteristic = getBluetoothGattCharacteristic(UUID_SERVICE, UUID_CHARACTERISTIC);
        if (bluetoothGattCharacteristic == null)
            return;
        enableGattServicesNotification(bluetoothGattCharacteristic);

    } else {
        Log.w(TAG, " --------- onServicesDiscovered received: " + status);
    }
}
 
Example 4
Source File: GattUtils.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Will return the copy of the service
 *
 * @param service The gatt service
 * @return a shallow-ish copy of the service
 */

public @Nullable
BluetoothGattServiceCopy copyService(@Nullable BluetoothGattService service) {
    if (null == service || null == service.getUuid()) {
        return null;
    }
    BluetoothGattServiceCopy newService = new BluetoothGattServiceCopy(service.getUuid(), service.getType());
    if (!service.getIncludedServices().isEmpty()) {
        for (BluetoothGattService includedService : service.getIncludedServices()) {
            BluetoothGattServiceCopy newGattService = new BluetoothGattServiceCopy(includedService.getUuid(), includedService.getType());
            newService.addService(newGattService);
        }
    }
    if (!service.getCharacteristics().isEmpty()) {
        // why not use the copy characteristic method, it will implicitly link itself to the null service
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            BluetoothGattCharacteristicCopy newCharacteristic = new BluetoothGattCharacteristicCopy(characteristic.getUuid(), characteristic.getProperties(), characteristic.getPermissions());
            if (characteristic.getValue() != null) {
                newCharacteristic.setValue(Arrays.copyOf(characteristic.getValue(), characteristic.getValue().length));
            }
            // why not use the copy descriptor method?  It will implicitly link itself to the null characteristic
            for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                BluetoothGattDescriptorCopy newDescriptor = new BluetoothGattDescriptorCopy(descriptor.getUuid(), descriptor.getPermissions());
                if (descriptor.getValue() != null) {
                    newDescriptor.setValue(Arrays.copyOf(descriptor.getValue(), descriptor.getValue().length));
                }
                newCharacteristic.addDescriptor(newDescriptor);
            }
            newService.addCharacteristic(newCharacteristic);
        }
    }
    return newService;
}
 
Example 5
Source File: DeviceControlActivity.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
/**
   * 获取BLE的特征值
   * @param gattServices
   */
  private void getCharacteristic(List<BluetoothGattService> gattServices) {
      if (gattServices == null) return;
      String uuid = null;

      // Loops through available GATT Services.
      for (BluetoothGattService gattService : gattServices) {
          uuid = gattService.getUuid().toString();
          
          //找uuid为0xffe0的服务
          if (uuid.equals(C.SERVICE_UUID)) {
          	List<BluetoothGattCharacteristic> gattCharacteristics =
                      gattService.getCharacteristics();
              //找uuid为0xffe1的特征值
              for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                  uuid = gattCharacteristic.getUuid().toString();
                  if(uuid.equals(C.CHAR_UUID)){
                  	mCharacteristic = gattCharacteristic;
                  	final int charaProp = gattCharacteristic.getProperties();
              		//开启该特征值的数据的监听
              		if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                          mBluetoothLeService.setCharacteristicNotification(
                                  mCharacteristic, true);
                      }
                  	System.out.println("uuid----->" + uuid);
                  }
              }
	}
      }
      
      //如果没找到指定的特征值,直接返回
      if (mCharacteristic == null) {
      	Toast.makeText(DeviceControlActivity.this, "未找到指定特征值", 
      			Toast.LENGTH_LONG).show();
	finish();
}

  }
 
Example 6
Source File: InPenService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onServicesDiscovered(RxBleDeviceServices services) {
    boolean found = false;
    super.onServicesDiscovered(services);
    for (BluetoothGattService service : services.getBluetoothGattServices()) {
        if (D) UserError.Log.d(TAG, "Service: " + getUUIDName(service.getUuid()));
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            if (D)
                UserError.Log.d(TAG, "-- Character: " + getUUIDName(characteristic.getUuid()));

            for (final UUID check : huntCharacterstics) {
                if (characteristic.getUuid().equals(check)) {
                    I.readCharacteristic = check;
                    found = true;
                }
            }
        }
    }
    if (found) {
        I.isDiscoveryComplete = true;
        I.discoverOnce = true;
        loadInfos();
        changeState(mState.next());
    } else {
        UserError.Log.e(TAG, "Could not find characteristic during service discovery. This is very unusual");
        tryGattRefresh();
    }
}
 
Example 7
Source File: BleUtils.java    From thunderboard-android with Apache License 2.0 5 votes vote down vote up
private static List<BluetoothGattCharacteristic> findCharacteristics(BluetoothGatt gatt, UUID serviceUuid, UUID characteristicUuid, int property) {
    if (serviceUuid == null) {
        return null;
    }

    if (characteristicUuid == null) {
        return null;
    }

    if (gatt == null) {
        return null;
    }

    BluetoothGattService service = gatt.getService(serviceUuid);
    if (service == null) {
        return null;
    }

    List<BluetoothGattCharacteristic> results = new ArrayList<>();
    for (BluetoothGattCharacteristic c : service.getCharacteristics()) {
        int props = c.getProperties();
        if (characteristicUuid.equals(c.getUuid())
                && (property == (property & props))) {
            results.add(c);
        }
    }
    return results;
}
 
Example 8
Source File: DexCollectionService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Displays all services and characteristics for debugging purposes.
 *
 * @param bluetoothGatt BLE gatt profile.
 */
private void listAvailableServices(BluetoothGatt bluetoothGatt) {
    Log.d(TAG, "Listing available services:");
    for (BluetoothGattService service : bluetoothGatt.getServices()) {
        Log.d(TAG, "Service: " + service.getUuid().toString());
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            Log.d(TAG, "|-- Characteristic: " + characteristic.getUuid().toString());
        }
    }
}
 
Example 9
Source File: BluetoothLE.java    From Makeblock-App-For-Android with MIT License 5 votes vote down vote up
private BluetoothGattCharacteristic characteristicForProperty(int property){
		List<BluetoothGattService> list = mBLE.getSupportedGattServices();
		if (list == null) return null;  
        for (BluetoothGattService gattService : list) {  
            //-----Service���ֶ���Ϣ-----//  
            int type = gattService.getType();  
            String uuid = gattService.getUuid().toString();
            //-----Characteristics���ֶ���Ϣ-----//  
            List<BluetoothGattCharacteristic> gattCharacteristics =gattService.getCharacteristics();  
            for (final BluetoothGattCharacteristic  gattCharacteristic: gattCharacteristics) {  
                  
                int permission = gattCharacteristic.getPermissions();  
                  
                int properties = gattCharacteristic.getProperties();  
//                Log.d("mb","---->char property:"+Utils.getCharPropertie(property)+" - "+(property&BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)+" - "+BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE);  
                int p = (property&properties);
                int np = property;
                if(np==p){
                	if(np==(int)(BluetoothGattCharacteristic.PROPERTY_WRITE|BluetoothGattCharacteristic.PROPERTY_READ)){
	                //UUID_KEY_DATA�ǿ��Ը�����ģ�鴮��ͨ�ŵ�Characteristic 
                		if((int)(uuid.indexOf("ffe4"))>0){
                			return gattCharacteristic; 
                		}
                	}else{
                		return gattCharacteristic; 
                	}
                }  
            }
        }//  
        return null;
	}
 
Example 10
Source File: ESenseManager.java    From flutter-plugins with MIT License 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    for (BluetoothGattService s : gatt.getServices()) {
        for (BluetoothGattCharacteristic c : s.getCharacteristics()) {
            mCharacteristicMap.put(getKey(c), c);
        }
    }

    // Fire onConnected event after all the services have been discovered
    if(mConnectionListener != null) {
        mConnectionListener.onConnected(ESenseManager.this);
    }
}
 
Example 11
Source File: ESenseManager.java    From flutter-plugins with MIT License 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    for (BluetoothGattService s : gatt.getServices()) {
        for (BluetoothGattCharacteristic c : s.getCharacteristics()) {
            mCharacteristicMap.put(getKey(c), c);
        }
    }

    // Fire onConnected event after all the services have been discovered
    if(mConnectionListener != null) {
        mConnectionListener.onConnected(ESenseManager.this);
    }
}
 
Example 12
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Displays all services and characteristics for debugging purposes.
 *
 * @param bluetoothGatt BLE gatt profile.
 */
private void listAvailableServices(BluetoothGatt bluetoothGatt) {
    Log.d(TAG, "Listing available services:");
    for (BluetoothGattService service : bluetoothGatt.getServices()) {
        Log.d(TAG, "Service: " + service.getUuid().toString());
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            Log.d(TAG, "|-- Characteristic: " + characteristic.getUuid().toString());
        }
    }
}
 
Example 13
Source File: AmazonFreeRTOSDevice.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 5 votes vote down vote up
private static void describeGattServices(List<BluetoothGattService> gattServices) {
    for (BluetoothGattService service : gattServices) {
        Log.d(TAG, "GattService: " + service.getUuid());
        List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
        for (BluetoothGattCharacteristic characteristic : characteristics) {
            Log.d(TAG, " |-characteristics: " +
                    (uuidToName.containsKey(characteristic.getUuid().toString()) ?
                            uuidToName.get(characteristic.getUuid().toString()) : characteristic.getUuid()));
        }
    }
}
 
Example 14
Source File: DeviceControlActivity.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
/**
   * 获取BLE的特征值
   * @param gattServices
   */
  private void getCharacteristic(List<BluetoothGattService> gattServices) {
      if (gattServices == null) return;
      String uuid = null;

      // Loops through available GATT Services.
      for (BluetoothGattService gattService : gattServices) {
          uuid = gattService.getUuid().toString();
          
          //找uuid为0000ffe0-0000-1000-8000-00805f9b34fb的服务
          if (uuid.equals(C.SERVICE_UUID)) {
          	List<BluetoothGattCharacteristic> gattCharacteristics =
                      gattService.getCharacteristics();
              //找uuid为0000ffe1-0000-1000-8000-00805f9b34fb的特征值
              for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                  uuid = gattCharacteristic.getUuid().toString();
                  if(uuid.equals(C.CHAR_UUID)){
                  	mCharacteristic = gattCharacteristic;
                  	final int charaProp = gattCharacteristic.getProperties();
              		//开启该特征值的数据的监听
              		if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                          mBluetoothLeService.setCharacteristicNotification(
                                  mCharacteristic, true);
                      }
                  	System.out.println("uuid----->" + uuid);
                  }
              }
	}
      }
      
      //如果没找到指定的特征值,直接返回
      if (mCharacteristic == null) {
      	Toast.makeText(DeviceControlActivity.this, "未找到指定特征值", 
      			Toast.LENGTH_LONG).show();
	finish();
}

  }
 
Example 15
Source File: BluetoothDebug.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
private int readServiceCharacteristics(BluetoothGattService service, int offset) {
    for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0
                && !isBlacklisted(service, characteristic)) {

            if (offset == 0) {
                readBytes(service.getUuid(), characteristic.getUuid());
                return -1;
            }

            offset -= 1;
        }

        for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
            if (offset == 0) {
                readBytes(service.getUuid(), characteristic.getUuid());
                return -1;
            }

            offset -= 1;
        }
    }

    for (BluetoothGattService included : service.getIncludedServices()) {
        offset = readServiceCharacteristics(included, offset);
        if (offset == -1) {
            return offset;
        }
    }

    return offset;
}
 
Example 16
Source File: DeviceControlActivity.java    From BlunoAccessoryShieldDemo with GNU General Public License v3.0 4 votes vote down vote up
private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) return;
    String uuid = null;
    String unknownServiceString = getResources().getString(R.string.unknown_service);
    String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
    ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
            = new ArrayList<ArrayList<HashMap<String, String>>>();
    mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        HashMap<String, String> currentServiceData = new HashMap<String, String>();
        uuid = gattService.getUuid().toString();
        System.out.println("displayGattServices + uuid="+uuid);
        currentServiceData.put(
                LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));
        currentServiceData.put(LIST_UUID, uuid);
        gattServiceData.add(currentServiceData);

        ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
                new ArrayList<HashMap<String, String>>();
        List<BluetoothGattCharacteristic> gattCharacteristics =
                gattService.getCharacteristics();
        ArrayList<BluetoothGattCharacteristic> charas =
                new ArrayList<BluetoothGattCharacteristic>();

        // Loops through available Characteristics.
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            charas.add(gattCharacteristic);
            HashMap<String, String> currentCharaData = new HashMap<String, String>();
            uuid = gattCharacteristic.getUuid().toString();
            currentCharaData.put(
                    LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
            currentCharaData.put(LIST_UUID, uuid);
            gattCharacteristicGroupData.add(currentCharaData);
        }
        mGattCharacteristics.add(charas);
        gattCharacteristicData.add(gattCharacteristicGroupData);
    }

    SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter(
            this,
            gattServiceData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] {LIST_NAME, LIST_UUID},
            new int[] { android.R.id.text1, android.R.id.text2 },
            gattCharacteristicData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] {LIST_NAME, LIST_UUID},
            new int[] { android.R.id.text1, android.R.id.text2 }
    );
    mGattServicesList.setAdapter(gattServiceAdapter);
}
 
Example 17
Source File: PBluetoothLEClientBak.java    From PHONK with GNU General Public License v3.0 4 votes vote down vote up
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    MLog.d(TAG, "onServicesDiscovered " + gatt + " " + status);
    ReturnObject ret = new ReturnObject();

    if (status == BluetoothGatt.GATT_SUCCESS) {
        List<BluetoothGattService> services = gatt.getServices();
        PhonkNativeArray retServicesArray = new PhonkNativeArray(services.size());
        ret.put("services", retServicesArray);

        int countServices = 0;
        for (BluetoothGattService service : services) {
            MLog.d(TAG, "service " + service.getUuid() + " " + service.getType());

            ReturnObject retService = new ReturnObject();
            retService.put("uuid", service.getUuid());
            retService.put("type", service.getType());
            retServicesArray.addPE(countServices++, retService);

            List<BluetoothGattCharacteristic> gattCharacteristic = service.getCharacteristics();
            PhonkNativeArray retCharArray = new PhonkNativeArray(gattCharacteristic.size());
            retService.put("characteristics", retCharArray);

            int counterCharacteristics = 0;
            for (BluetoothGattCharacteristic characteristic : gattCharacteristic) {
                MLog.d(TAG, "Characteristics" + characteristic.getUuid() + " " + characteristic.getProperties() + " " + characteristic.getWriteType() + " " + characteristic.getValue());
                gatt.readCharacteristic(characteristic);

                ReturnObject retChar = new ReturnObject();
                retChar.put("uuid", characteristic.getUuid());
                retChar.put("writeType", characteristic.getWriteType());
                /*
                retChar.put("descriptors", characteristic.getDescriptors());
                */
                retCharArray.addPE(counterCharacteristics++, retChar);
            }

        }

        if (mCallbackServices != null) {
            mCallbackServices.event(ret);
        }
    } else {
        MLog.d(TAG, "onServicesDiscovered received: " + status);
    }
}
 
Example 18
Source File: DeviceControlActivity.java    From AndroidBleManager with Apache License 2.0 4 votes vote down vote up
private void generateExportString(final List<BluetoothGattService> gattServices) {
    final String unknownServiceString = getResources().getString(R.string.unknown_service);
    final String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
    final StringBuilder exportBuilder = new StringBuilder();

    exportBuilder.append("Device Name: ");
    exportBuilder.append(mDeviceName);
    exportBuilder.append('\n');
    exportBuilder.append("Device Address: ");
    exportBuilder.append(mDeviceAddress);
    exportBuilder.append('\n');
    exportBuilder.append('\n');

    exportBuilder.append("Services:");
    exportBuilder.append("--------------------------");
    exportBuilder.append('\n');

    String uuid = null;
    for (final BluetoothGattService gattService : gattServices) {
        uuid = gattService.getUuid().toString();

        exportBuilder.append(GattAttributeResolver.getAttributeName(uuid, unknownServiceString));
        exportBuilder.append(" (");
        exportBuilder.append(uuid);
        exportBuilder.append(')');
        exportBuilder.append('\n');

        final List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
        for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            uuid = gattCharacteristic.getUuid().toString();

            exportBuilder.append('\t');
            exportBuilder.append(GattAttributeResolver.getAttributeName(uuid, unknownCharaString));
            exportBuilder.append(" (");
            exportBuilder.append(uuid);
            exportBuilder.append(')');
            exportBuilder.append('\n');
        }

        exportBuilder.append('\n');
        exportBuilder.append('\n');
    }

    exportBuilder.append("--------------------------");
    exportBuilder.append('\n');

    mExportString = exportBuilder.toString();
}
 
Example 19
Source File: DeviceControlActivity.java    From BLEConnect with GNU General Public License v2.0 4 votes vote down vote up
private void displayGattServices(List<BluetoothGattService> gattServices) {
	if (gattServices == null)
		return;
	String uuid = null;
	String unknownServiceString = getResources().getString(
			R.string.unknown_service);
	String unknownCharaString = getResources().getString(
			R.string.unknown_characteristic);
	ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
	ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData = new ArrayList<ArrayList<HashMap<String, String>>>();
	mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

	// Loops through available GATT Services.
	for (BluetoothGattService gattService : gattServices) {
		HashMap<String, String> currentServiceData = new HashMap<String, String>();
		uuid = gattService.getUuid().toString();
		currentServiceData.put(LIST_NAME,
				SampleGattAttributes.lookup(uuid, unknownServiceString));
		currentServiceData.put(LIST_UUID, uuid);
		gattServiceData.add(currentServiceData);

		ArrayList<HashMap<String, String>> gattCharacteristicGroupData = new ArrayList<HashMap<String, String>>();
		List<BluetoothGattCharacteristic> gattCharacteristics = gattService
				.getCharacteristics();
		ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<BluetoothGattCharacteristic>();

		// Loops through available Characteristics.
		for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {

			if (UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT)
					.equals(gattCharacteristic.getUuid())) {
				Log.d(TAG, "Found heart rate");
				mNotifyCharacteristic = gattCharacteristic;
			}

			charas.add(gattCharacteristic);
			HashMap<String, String> currentCharaData = new HashMap<String, String>();
			uuid = gattCharacteristic.getUuid().toString();
			currentCharaData.put(LIST_NAME,
					SampleGattAttributes.lookup(uuid, unknownCharaString));
			currentCharaData.put(LIST_UUID, uuid);
			gattCharacteristicGroupData.add(currentCharaData);
		}
		mGattCharacteristics.add(charas);
		gattCharacteristicData.add(gattCharacteristicGroupData);
	}

}
 
Example 20
Source File: DeviceControlActivity.java    From android-BluetoothLeGatt with Apache License 2.0 4 votes vote down vote up
private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) return;
    String uuid = null;
    String unknownServiceString = getResources().getString(R.string.unknown_service);
    String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
    ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
            = new ArrayList<ArrayList<HashMap<String, String>>>();
    mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        HashMap<String, String> currentServiceData = new HashMap<String, String>();
        uuid = gattService.getUuid().toString();
        currentServiceData.put(
                LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));
        currentServiceData.put(LIST_UUID, uuid);
        gattServiceData.add(currentServiceData);

        ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
                new ArrayList<HashMap<String, String>>();
        List<BluetoothGattCharacteristic> gattCharacteristics =
                gattService.getCharacteristics();
        ArrayList<BluetoothGattCharacteristic> charas =
                new ArrayList<BluetoothGattCharacteristic>();

        // Loops through available Characteristics.
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            charas.add(gattCharacteristic);
            HashMap<String, String> currentCharaData = new HashMap<String, String>();
            uuid = gattCharacteristic.getUuid().toString();
            currentCharaData.put(
                    LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
            currentCharaData.put(LIST_UUID, uuid);
            gattCharacteristicGroupData.add(currentCharaData);
        }
        mGattCharacteristics.add(charas);
        gattCharacteristicData.add(gattCharacteristicGroupData);
    }

    SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter(
            this,
            gattServiceData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] {LIST_NAME, LIST_UUID},
            new int[] { android.R.id.text1, android.R.id.text2 },
            gattCharacteristicData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] {LIST_NAME, LIST_UUID},
            new int[] { android.R.id.text1, android.R.id.text2 }
    );
    mGattServicesList.setAdapter(gattServiceAdapter);
}