Java Code Examples for android.bluetooth.BluetoothGatt#getServices()

The following examples show how to use android.bluetooth.BluetoothGatt#getServices() . 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: AndroidBle.java    From GizwitsBLE with Apache License 2.0 6 votes vote down vote up
@Override
public ArrayList<BleGattService> getServices(String address) {
	BluetoothGatt gatt = mBluetoothGatts.get(address);
	if (gatt == null) {
		return null;
	}

	ArrayList<BleGattService> list = new ArrayList<BleGattService>();
	List<BluetoothGattService> services = gatt.getServices();
	for (BluetoothGattService s : services) {
		BleGattService service = new BleGattService(s);
		// service.setInfo(btQuery.getGattServiceInfo(s.getUuid()));
		list.add(service);
	}
	return list;
}
 
Example 2
Source File: BtBridge.java    From sony-smartband-open-api with MIT License 6 votes vote down vote up
@Override
// TODO: maybe here we should set status == CONNECTED
public void onServicesDiscovered( BluetoothGatt gatt, int status ) {
    if( status != BluetoothGatt.GATT_SUCCESS ) {
        Log.e( CLASS, "onServicesDiscovered: Status != SUCCESS: status=" + status );
        return;
    }
    mGatt = gatt;

    List<BluetoothGattService> services = gatt.getServices();
    Log.d( CLASS, "onServicesDiscovered: services=" + services.size() );

    for( BtBridgeListener listener : mListeners ) {
        listener.onServicesDiscovered( services );
    }
}
 
Example 3
Source File: BLEUtils.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
/**
 * Set a Notification Setting for The Matching Characteristic of a Service
 *
 * @param gatt               The Bluetooth GATT
 * @param gattService        the service we must find and match
 * @param gattCharacteristic the characteristic we must find and match
 * @param gattDescriptor     the descriptor we must write to we must find and match
 * @param value              The exact setting we are setting
 * @return Whether the instruction to write passed or failed.
 */
public static boolean SetNotificationForCharacteristic(BluetoothGatt gatt, GattService gattService, GattCharacteristic gattCharacteristic, UUID gattDescriptor, Notifications value) {
    boolean written = false;
    List<BluetoothGattService> services = gatt.getServices();
    for (BluetoothGattService service : services) {
        BluetoothGattCharacteristic characteristic = BLEUtils.getCharacteristic(service, gattService, gattCharacteristic);
        if (characteristic != null) {
            gatt.setCharacteristicNotification(characteristic, value.isEnabled());
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(gattDescriptor);
            if (descriptor != null) {
                //writing this descriptor causes the device to send updates
                descriptor.setValue(value.getDescriptorValue());
                written = gatt.writeDescriptor(descriptor);
            }
            return written;
        }
    }

    return false;
}
 
Example 4
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 5
Source File: BluetoothPeripheral.java    From blessed-android with MIT License 6 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status != GATT_SUCCESS) {
        Timber.e("service discovery failed due to internal error '%s', disconnecting", statusToString(status));
        disconnect();
        return;
    }

    final List<BluetoothGattService> services = gatt.getServices();
    Timber.i("discovered %d services for '%s'", services.size(), getName());

    if (listener != null) {
        listener.connected(BluetoothPeripheral.this);
    }

    callbackHandler.post(new Runnable() {
        @Override
        public void run() {
            peripheralCallback.onServicesDiscovered(BluetoothPeripheral.this);
        }
    });
}
 
Example 6
Source File: BLEService.java    From android with MIT License 6 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);

    print("发现服务...");

    if (BluetoothGatt.GATT_SUCCESS == status) {
        List<BluetoothGattService> gattServices = gatt.getServices();
        if (null == gattServices || gattServices.size() == 0) {
            return;
        }
        for (BluetoothGattService gattService : gattServices) {
            List<BluetoothGattCharacteristic> characteristics = gattService.getCharacteristics();
            for (BluetoothGattCharacteristic characteristic : characteristics) {
                String uuid = characteristic.getUuid().toString();
                print("UUID    Cha:" + uuid);
                if (UUID_BT5_DATA.equalsIgnoreCase(uuid)) {
                    mBluetoothGatt.setCharacteristicNotification(characteristic, true);
                    print("开始监听...");
                }
            }
        }
    }
}
 
Example 7
Source File: HealthThermometerActivity.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);
    boolean startNotificationForCharacteristicFromHere = true;
    List<BluetoothGattService> services = gatt.getServices();
    if (services != null) {
        for (BluetoothGattService s : services) {
            if (s.getCharacteristics() != null) {
                for (BluetoothGattCharacteristic ch : s.getCharacteristics()) {
                    if (GattCharacteristic.TemperatureType.uuid.equals(ch.getUuid())) {
                        startNotificationForCharacteristicFromHere = false;
                        gatt.readCharacteristic(ch);
                        break;
                    }

                }
            }
        }
    }

    if (startNotificationForCharacteristicFromHere) {
        BLEUtils.SetNotificationForCharacteristic(gatt, GattService.HealthThermometer,
                GattCharacteristic.Temperature,
                BLEUtils.Notifications.INDICATE);
    }

}
 
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: 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 10
Source File: DexCollectionService.java    From xDrip-Experimental 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 11
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 12
Source File: BikeService.java    From android with MIT License 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);

    print("发现服务:" + status);

    if (BluetoothGatt.GATT_SUCCESS == status) {
        List<BluetoothGattService> gattServices = gatt.getServices();
        if (null == gattServices || gattServices.size() == 0) {
            return;
        }
        for (BluetoothGattService gattService : gattServices) {
            String serviceUUID = gattService.getUuid().toString();
            print("UUID GATT:" + serviceUUID);
            List<BluetoothGattCharacteristic> characteristics = gattService.getCharacteristics();
            for (BluetoothGattCharacteristic characteristic : characteristics) {
                String uuid = characteristic.getUuid().toString();
                print("UUID     Charac:" + uuid);
                print("UUID     Status:" + getProperties(characteristic));
                print("UUID     Status:" + characteristic.getInstanceId());
                List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
                for (BluetoothGattDescriptor descriptor : descriptors) {
                    print("UUID          Desc:" + descriptor.getUuid().toString());
                    print("UUID          Perm:" + String.valueOf(descriptor.getPermissions()));
                }
                if (UUID_RECEIVE.toString().equalsIgnoreCase(uuid)) {
                    mBluetoothGatt.setCharacteristicNotification(characteristic, true);
                    print("开始监听:" + uuid);
                }
            }
        }
    }
}
 
Example 13
Source File: BleManager.java    From FastBle with Apache License 2.0 5 votes vote down vote up
public List<BluetoothGattService> getBluetoothGattServices(BleDevice bleDevice) {
    BluetoothGatt gatt = getBluetoothGatt(bleDevice);
    if (gatt != null) {
        return gatt.getServices();
    }
    return null;
}
 
Example 14
Source File: DeviceActivity.java    From BLERW with Apache License 2.0 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
	for (BluetoothGattService service : gatt.getServices()) {
		if ((service == null) || (service.getUuid() == null)) {
			continue;
		}
		if (BleUuid.SERVICE_DEVICE_INFORMATION.equalsIgnoreCase(service
				.getUuid().toString())) {
			mReadManufacturerNameButton
					.setTag(service.getCharacteristic(UUID
							.fromString(BleUuid.CHAR_MANUFACTURER_NAME_STRING)));
			mReadSerialNumberButton
					.setTag(service.getCharacteristic(UUID
							.fromString(BleUuid.CHAR_SERIAL_NUMBEAR_STRING)));
			runOnUiThread(new Runnable() {
				public void run() {
					mReadManufacturerNameButton.setEnabled(true);
					mReadSerialNumberButton.setEnabled(true);
				};
			});
		}
		if (BleUuid.SERVICE_IMMEDIATE_ALERT.equalsIgnoreCase(service
				.getUuid().toString())) {
			runOnUiThread(new Runnable() {
				public void run() {
					mWriteAlertLevelButton.setEnabled(true);
				};
			});
			mWriteAlertLevelButton.setTag(service
					.getCharacteristic(UUID
							.fromString(BleUuid.CHAR_ALERT_LEVEL)));
		}
	}

	runOnUiThread(new Runnable() {
		public void run() {
			setProgressBarIndeterminateVisibility(false);
		};
	});
}
 
Example 15
Source File: BluetoothLeService.java    From OpenFit with MIT License 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(LOG_TAG, "onServicesDiscovered: "+status);
    if(status == BluetoothGatt.GATT_SUCCESS) {    
        // loops through available GATT Services.
        for(BluetoothGattService gattService : gatt.getServices()) {
            String uuid = gattService.getUuid().toString();
            String type = gattServiceType[gattService.getType()];

            Log.d(LOG_TAG, "onServicesDiscovered type: "+type);
            Log.d(LOG_TAG, "onServicesDiscovered uuid: "+uuid);
            //Log.d(LOG_TAG, "onServicesDiscovered: getCharacteristic: "+mWriteCharacteristic);
            for(BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
                String cUuid = gattCharacteristic.getUuid().toString();
                int cInstanceId = gattCharacteristic.getInstanceId();
                int cPermissions = gattCharacteristic.getPermissions();
                int cProperties = gattCharacteristic.getProperties();
                byte[] cValue = gattCharacteristic.getValue();
                int cWriteType = gattCharacteristic.getWriteType();

                Log.d(LOG_TAG, "onServicesDiscovered cUuid: "+cUuid);
                Log.d(LOG_TAG, "onServicesDiscovered cInstanceId: "+cInstanceId);
                Log.d(LOG_TAG, "onServicesDiscovered cPermissions: "+cPermissions);
                Log.d(LOG_TAG, "onServicesDiscovered cProperties: "+cProperties);
                Log.d(LOG_TAG, "onServicesDiscovered cValue: "+cValue);
                Log.d(LOG_TAG, "onServicesDiscovered cWriteType: "+cWriteType);
            }
        }
        Log.d(LOG_TAG, "BluetoothLe Service discovered: "+status);
        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
    }
    else {
        Log.d(LOG_TAG, "BluetoothLe onServicesDiscovered received: "+status);
    }
}
 
Example 16
Source File: BTDeviceProfile.java    From BLEService with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public BTDeviceProfile(BluetoothDevice device, BluetoothGatt gatt, int connectionState) {
	mDeviceAddress = device.getAddress();
	mConnectionState = connectionState;
	mServiceProfileList = new ArrayList<BTServiceProfile>();
	for (BluetoothGattService service : gatt.getServices()) {
		BTServiceProfile serviceProfile = new BTServiceProfile(service);
		mServiceProfileList.add(serviceProfile);
	}
}
 
Example 17
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 18
Source File: GattClientCallback.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);
    Timber.v("[%s] onServicesDiscovered: Gatt Response Status %s", getDeviceMacFromGatt(gatt), GattStatus.getStatusForCode(status));
    Timber.d("[%s][Threading] Originally called on thread : %s", getDeviceMacFromGatt(gatt), Thread.currentThread().getName());
    ArrayList<GattClientListener> copy = new ArrayList<>(listeners.size());
    copy.addAll(listeners);
    for (GattClientListener listener : copy) {
        if(listener.getDevice() != null && listener.getDevice().equals(gatt.getDevice())) {
            handler.post(() -> listener.onServicesDiscovered(gatt, status));
        }
    }
    GattConnection conn = FitbitGatt.getInstance().getConnection(gatt.getDevice());
    if(conn != null) {
        List<BluetoothGattService> discoveredServices = gatt.getServices();
        // since this is one of the events that could happen asynchronously, we will
        // need to iterate through our connection listeners
        handler.post(() -> {
            for (ConnectionEventListener asyncConnListener : conn.getConnectionEventListeners()) {
                TransactionResult.Builder builder = new TransactionResult.Builder();
                builder.transactionName(GattClientDiscoverServicesTransaction.NAME);
                if(status == BluetoothGatt.GATT_SUCCESS) {
                    builder.resultStatus(TransactionResult.TransactionResultStatus.SUCCESS);
                } else {
                    builder.resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
                }
                asyncConnListener.onServicesDiscovered(builder
                    .transactionName(GattClientDiscoverServicesTransaction.NAME)
                    .serverServices(discoveredServices)
                    .gattState(conn.getGattState())
                    .responseStatus(GattDisconnectReason.getReasonForCode(status).ordinal()).build(), conn);
            }
        });
    }
}
 
Example 19
Source File: DeviceServicesActivity.java    From EFRConnect-android with Apache License 2.0 4 votes vote down vote up
/**
 * READ ALL THE SERVICES, PRINT IT ON LOG AND RECOGNIZES HOMEKIT ACCESSORIES
 *****************/
public void getServicesInfo(BluetoothGatt gatt) {

    List<BluetoothGattService> gattServices = gatt.getServices();
    Log.i("onServicesDiscovered", "Services count: " + gattServices.size());

    for (BluetoothGattService gattService : gattServices) {
        String serviceUUID = gattService.getUuid().toString();
        Log.i("onServicesDiscovered", "Service UUID " + serviceUUID + " - Char count: " + gattService.getCharacteristics().size());
        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();

        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {

            String CharacteristicUUID = gattCharacteristic.getUuid().toString();
            Log.i("onServicesDiscovered", "Characteristic UUID " + CharacteristicUUID + " - Properties: " + gattCharacteristic.getProperties());

            if (gattCharacteristic.getUuid().toString().equals(ota_control.toString())) {
                if (gattCharacteristics.contains(bluetoothGatt.getService(ota_service).getCharacteristic(ota_data))) {
                    if (!gattServices.contains(bluetoothGatt.getService(homekit_service))) {
                        Log.i("onServicesDiscovered", "Device in DFU Mode");
                    } else {
                        Log.i("onServicesDiscovered", "OTA_Control found");
                        List<BluetoothGattDescriptor> gattDescriptors = gattCharacteristic.getDescriptors();

                        for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) {
                            String descriptor = gattDescriptor.getUuid().toString();

                            if (gattDescriptor.getUuid().toString().equals(homekit_descriptor.toString())) {
                                kit_descriptor = gattDescriptor;
                                Log.i("descriptor", "UUID: " + descriptor);
                                //bluetoothGatt.readDescriptor(gattDescriptor);
                                byte[] stable = {(byte) 0x00, (byte) 0x00};
                                homeKitOTAControl(stable);
                                homekit = true;

                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example 20
Source File: SerialInterface_BLE.java    From PodEmu with GNU General Public License v3.0 4 votes vote down vote up
@Override
@TargetApi(21)
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
    PodEmuLog.debug("SIBLE: GATT onServiceDiscovered with status=" + status);
    super.onServicesDiscovered(gatt, status);
    boolean isSerialSupported = false;

    for (BluetoothGattService service : gatt.getServices())
    {
        PodEmuLog.debug("SIBLE: Service: " + service.getUuid());
        //if (Arrays.asList(BLE_UUIDS).contains(service.getUuid()))
        if(SERVICE_UUID.equals(service.getUuid()))
        {
            PodEmuLog.debug("SIBLE: BLE found serial device!");

            //gatt.readCharacteristic(service.getCharacteristic(CHARACTERISTIC_VERSION_UUID));
            //gatt.readCharacteristic(service.getCharacteristic(CHARACTERISTIC_DESC_UUID));
            //gatt.setCharacteristicNotification(service.getCharacteristic(CHARACTERISTIC_MESSAGE_UUID), true);
            //gatt.setCharacteristicNotification(service.getCharacteristic(CHARACTERISTIC_RFCOMM_TRANSFER_UUID), true);

            BluetoothGattCharacteristic bleGattCharacteristic = service.getCharacteristic(CHARACTERISTIC_SERIAL_UUID);

            if(bleGattCharacteristic != null)
            {
                isSerialSupported = true;
                gatt.readCharacteristic(service.getCharacteristic(CHARACTERISTIC_SERIAL_UUID));
                gatt.setCharacteristicNotification(bleGattCharacteristic, true);
            }
        }
    }

    if(isSerialSupported)
    {
        PodEmuLog.debug("SIBLE: BLE serial supported!");
    }
    else
    {
        PodEmuLog.debug("SIBLE: BLE serial NOT supported!");
    }
}