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

The following examples show how to use android.bluetooth.BluetoothGatt#getService() . 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: ReadCommand.java    From neatle with MIT License 6 votes vote down vote up
@Override
protected void start(Connection connection, BluetoothGatt gatt) {
    BluetoothGattService service = gatt.getService(serviceUUID);
    if (service != null) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID);
        if (characteristic != null) {
            NeatleLogger.d("Reading characteristics " + characteristicUUID);
            if (gatt.readCharacteristic(characteristic)) {
                return;
            }
            NeatleLogger.d("Read failed" + characteristicUUID);
        } else {
            NeatleLogger.e("Could not find characteristics " + characteristicUUID);
        }
    } else {
        NeatleLogger.e("Could not find service " + serviceUUID);
    }

    finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
}
 
Example 2
Source File: TestBeanBLE.java    From bean-sdk-android with MIT License 6 votes vote down vote up
private void readHardwareVersion(BluetoothGatt gatt) throws InterruptedException {

        BluetoothGattService dis = gatt.getService(Constants.UUID_DEVICE_INFO_SERVICE);
        BluetoothGattCharacteristic hwv = dis.getCharacteristic(Constants.UUID_DEVICE_INFO_CHAR_HARDWARE_VERSION);
        if (hwv == null) {
            fail("FAILURE: No characteristic Hardware Version");
        }
        if (!gatt.readCharacteristic(hwv)) {
            fail("FAILURE: Read char failed Hardware Version");
        }
        readHardwareVersionLatch.await(10, TimeUnit.SECONDS);
        if (hardwareVersionString == null) {
            fail("FAILURE: No hardware version");
        }

        Log.i(TAG, "SUCCESS: Read hardware Version " + hardwareVersionString);
    }
 
Example 3
Source File: UARTProfile.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
	protected Deque<BleManager.Request> initGatt(final BluetoothGatt gatt) {
		final BluetoothGattService service = gatt.getService(UART_SERVICE_UUID);
		txCharacteristic = service.getCharacteristic(UART_TX_CHARACTERISTIC_UUID);
		rxCharacteristic = service.getCharacteristic(UART_RX_CHARACTERISTIC_UUID);

		final int rxProperties = rxCharacteristic.getProperties();
		boolean writeRequest = (rxProperties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0;

		// Set the WRITE REQUEST type when the characteristic supports it. This will allow to send
		// long write (also if the characteristic support it).
		// In case there is no WRITE REQUEST property, this manager will divide texts longer then
		// 20 bytes into up to 20 bytes chunks.
		if (writeRequest)
			rxCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

		// We don't want to enable notifications on TX characteristic as we are not showing them here.
		// A watch may be just used to send data. At least now.
//		final LinkedList<BleProfileApi.Request> requests = new LinkedList<>();
//		requests.add(BleProfileApi.Request.newEnableNotificationsRequest(txCharacteristic));
//		return requests;
		return null;
	}
 
Example 4
Source File: Node.java    From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean checkServiceChangedCharacteristics(BluetoothGatt gatt){

            BluetoothGattService genericGatt = gatt.getService(SERVICE_CHANGED_SERVICE_UUID);
            if (genericGatt == null)
                return false;

            BluetoothGattCharacteristic serviceChangeChar = genericGatt.getCharacteristic(SERVICE_CHANGED_CHAR_UUID);
            if (serviceChangeChar == null)
                return false;

            BluetoothGattDescriptor desc = serviceChangeChar.getDescriptor(NOTIFY_CHAR_DESC_UUID);
            if (desc == null)
                return  false;

            enqueueWriteDesc(new WriteDescCommand(desc,BluetoothGattDescriptor.ENABLE_INDICATION_VALUE));
            return true;
        }
 
Example 5
Source File: BLEService.java    From android_wear_for_ios with MIT License 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(TAG_LOG, "onServicesDiscovered received: " + status);
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattService service = gatt.getService(UUID.fromString(service_ancs));
        if (service == null) {
            Log.d(TAG_LOG, "cant find service");
        } else {
            Log.d(TAG_LOG, "find service");
            Log.d(TAG_LOG, String.valueOf(bluetooth_gatt.getServices()));

            // subscribe data source characteristic
            BluetoothGattCharacteristic data_characteristic = service.getCharacteristic(UUID.fromString(characteristics_data_source));

            if (data_characteristic == null) {
                Log.d(TAG_LOG, "cant find data source chara");
            } else {
                Log.d(TAG_LOG, "find data source chara :: " + data_characteristic.getUuid());
                Log.d(TAG_LOG, "set notify:: " + data_characteristic.getUuid());
                bluetooth_gatt.setCharacteristicNotification(data_characteristic, true);
                BluetoothGattDescriptor descriptor = data_characteristic.getDescriptor(
                        UUID.fromString(descriptor_config));
                if(descriptor == null){
                    Log.d(TAG_LOG, " ** cant find desc :: " + descriptor.getUuid());
                }else{
                    Log.d(TAG_LOG, " ** find desc :: " + descriptor.getUuid());
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    bluetooth_gatt.writeDescriptor(descriptor);
                    if(api_level >= 21) {
                        stop_le_scanner();
                    }else {
                        bluetooth_adapter.stopLeScan(le_scan_callback);
                    }
                }
            }
        }
    }
}
 
Example 6
Source File: AppBleService.java    From bleYan with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onDiscoverServices(BluetoothGatt gatt) {
    //for test
    final BluetoothGattService gattService = gatt
.getService(UUID.fromString("E54EAA50-371B-476C-99A3-74d267e3edbe"));
    if (gattService != null) {
        final BluetoothGattCharacteristic connectConf =
                gattService.getCharacteristic(UUID.fromString("E54EAA55-371B-476C-99A3-74D267E3EDBE"));
        if(connectConf != null) {
            AppLog.i(TAG, "onDiscoverServices: connectConf()");
            connectConf.setValue(new byte[]{ (byte) 0x88 });
            write(connectConf);
        }
    }
}
 
Example 7
Source File: BleSensor.java    From BLE-Heart-rate-variability-demo with MIT License 5 votes vote down vote up
private BluetoothGattCharacteristic getCharacteristic(BluetoothGatt bluetoothGatt, String uuid) {
    final UUID serviceUuid = UUID.fromString(getServiceUUID());
    final UUID characteristicUuid = UUID.fromString(uuid);

    final BluetoothGattService service = bluetoothGatt.getService(serviceUuid);
    return service.getCharacteristic(characteristicUuid);
}
 
Example 8
Source File: TemplateManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
	// TODO Initialize required characteristics.
	// It should return true if all has been discovered (that is that device is supported).
	final BluetoothGattService service = gatt.getService(SERVICE_UUID);
	if (service != null) {
		requiredCharacteristic = service.getCharacteristic(MEASUREMENT_CHARACTERISTIC_UUID);
	}
	final BluetoothGattService otherService = gatt.getService(OTHER_SERVICE_UUID);
	if (otherService != null) {
		deviceNameCharacteristic = otherService.getCharacteristic(WRITABLE_CHARACTERISTIC_UUID);
	}
	return requiredCharacteristic != null && deviceNameCharacteristic != null;
}
 
Example 9
Source File: AndroidBle.java    From GizwitsBLE with Apache License 2.0 5 votes vote down vote up
@Override
public BleGattService getService(String address, UUID uuid) {
	BluetoothGatt gatt = mBluetoothGatts.get(address);
	if (gatt == null) {
		return null;
	}

	BluetoothGattService service = gatt.getService(uuid);
	if (service == null) {
		return null;
	} else {
		return new BleGattService(service);
	}
}
 
Example 10
Source File: BleRequestImpl.java    From Android-BLE with Apache License 2.0 5 votes vote down vote up
public BluetoothGattCharacteristic gattCharacteristic(BluetoothGatt gatt, UUID serviceUUID, UUID characteristicUUID){
    BluetoothGattService gattService = gatt.getService(serviceUUID);
    if (gattService == null){
        BleLog.e(TAG, "serviceUUID is null");
        return null;
    }
    BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(characteristicUUID);
    if (characteristic == null){
        BleLog.e(TAG, "characteristicUUID is null");
        return null;
    }
    return characteristic;
}
 
Example 11
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Deprecated
private boolean internalReadBatteryLevel() {
	final BluetoothGatt gatt = bluetoothGatt;
	if (gatt == null || !connected)
		return false;

	final BluetoothGattService batteryService = gatt.getService(BleManager.BATTERY_SERVICE);
	if (batteryService == null)
		return false;

	final BluetoothGattCharacteristic batteryLevelCharacteristic =
			batteryService.getCharacteristic(BleManager.BATTERY_LEVEL_CHARACTERISTIC);
	return internalReadCharacteristic(batteryLevelCharacteristic);
}
 
Example 12
Source File: TestBeanBLE.java    From bean-sdk-android with MIT License 5 votes vote down vote up
private void writeCharacteristicOADIdentify(BluetoothGatt gatt) {
    BluetoothGattService oads = gatt.getService(Constants.UUID_OAD_SERVICE);
    BluetoothGattCharacteristic iden = oads.getCharacteristic(Constants.UUID_OAD_CHAR_IDENTIFY);
    byte[] zeros = new byte[2];
    zeros[0] = 0;
    zeros[1] = 0;
    iden.setValue(zeros);
    if (!gatt.writeCharacteristic(iden)) {
        Log.e(TAG, "FAILURE: Write char OAD Identify 0x0000");
        fail("FAILURE: Write char OAD Identify 0x0000");
    }
    Log.i(TAG, "SUCCESS: Wrote characteristic");
}
 
Example 13
Source File: BatteryManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) {
	final BluetoothGattService service = gatt.getService(BATTERY_SERVICE_UUID);
	if (service != null) {
		batteryLevelCharacteristic = service.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC_UUID);
	}
	return batteryLevelCharacteristic != null;
}
 
Example 14
Source File: RSCManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
	final BluetoothGattService service = gatt.getService(RUNNING_SPEED_AND_CADENCE_SERVICE_UUID);
	if (service != null) {
		rscMeasurementCharacteristic = service.getCharacteristic(RSC_MEASUREMENT_CHARACTERISTIC_UUID);
	}
	return rscMeasurementCharacteristic != null;
}
 
Example 15
Source File: BleUtils.java    From thunderboard-android with Apache License 2.0 5 votes vote down vote up
public static boolean readCharacteristic(BluetoothGatt gatt, UUID serviceUuid, UUID characteristicUuid) {
    if (gatt == null) {
        return false;
    }
    BluetoothGattService service = gatt.getService(serviceUuid);
    if (service == null) {
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid);
    if (characteristic == null) {
        return false;
    }
    return gatt.readCharacteristic(characteristic);
}
 
Example 16
Source File: MultipleBleService.java    From BleLib with Apache License 2.0 5 votes vote down vote up
/**
 * Write data to characteristic, and send to remote bluetooth le device.
 *
 * @param address            The address to read from.
 * @param serviceUUID        remote device service uuid
 * @param characteristicUUID remote device characteristic uuid
 * @param value              Send to remote ble device data.
 * @return if write success return true
 */
public boolean writeCharacteristic(String address, String serviceUUID,
                                   String characteristicUUID, String value) {
    BluetoothGatt bluetoothGatt = mBluetoothGattMap.get(address);
    if (bluetoothGatt != null) {
        BluetoothGattService service =
                bluetoothGatt.getService(UUID.fromString(serviceUUID));
        BluetoothGattCharacteristic characteristic =
                service.getCharacteristic(UUID.fromString(characteristicUUID));
        characteristic.setValue(value);
        return bluetoothGatt.writeCharacteristic(characteristic);
    }
    return false;
}
 
Example 17
Source File: BleGattImpl.java    From EasyBle with Apache License 2.0 5 votes vote down vote up
private boolean checkUuid(String serviceUuid, String charUuid, BluetoothGatt gatt,
                          final BleDevice device, final BleCallback callback) {
    if (gatt == null) {
        return false;
    }
    BluetoothGattService service = gatt.getService(UUID.fromString(serviceUuid));
    if (service == null) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER,
                        "the remote device doesn't contain this service uuid", device);
            }
        });
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(charUuid));
    if (characteristic == null) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER,
                        "the service of remote device doesn't contain this characteristic uuid", device);
            }
        });
        return false;
    }
    return true;
}
 
Example 18
Source File: BleGattImpl.java    From EasyBle with Apache License 2.0 5 votes vote down vote up
@Override
public void read(final BleDevice device, String serviceUuid, String readUuid, final BleReadCallback callback) {
    checkNotNull(callback, BleReadCallback.class);
    if (!checkConnection(device, callback)) {
        return;
    }
    BluetoothGatt gatt = mGattMap.get(device.address);
    if (!checkUuid(serviceUuid, readUuid, gatt, device, callback)) {
        return;
    }

    OperationIdentify identify = getOperationIdentifyFromMap(mReadCallbackMap, device.address, serviceUuid, readUuid);
    if (identify != null) {
        mReadCallbackMap.put(identify, callback);
    } else {
        mReadCallbackMap.put(new OperationIdentify(device.address, serviceUuid, readUuid), callback);
    }

    BluetoothGattService service = gatt.getService(UUID.fromString(serviceUuid));
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(readUuid));
    boolean readable = (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) > 0;
    if (!readable) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER, "the characteristic is not readable", device);
            }
        });
        return;
    }
    if (!gatt.readCharacteristic(characteristic)) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER, "read fail because of unknown reason", device);
            }
        });
    }
}
 
Example 19
Source File: HRManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
	final BluetoothGattService service = gatt.getService(HR_SERVICE_UUID);
	if (service != null) {
		heartRateCharacteristic = service.getCharacteristic(HEART_RATE_MEASUREMENT_CHARACTERISTIC_UUID);
	}
	return heartRateCharacteristic != null;
}
 
Example 20
Source File: BleGattImpl.java    From EasyBle with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final BleDevice device, String serviceUuid, String writeUuid, byte[] data, final BleWriteCallback callback) {
    checkNotNull(callback, BleWriteCallback.class);
    if (data == null || data.length < 1 || data.length > 509) {
        callback.onFailure(BleCallback.FAIL_OTHER, data == null ? "data is null" :
                "data length must range from 1 to 509", device);
        return;
    }
    if (data.length > 20) {
        Logger.w("data length is greater than the default(20 bytes), make sure  MTU >= " + (data.length + 3));
    }
    if (!checkConnection(device, callback)) {
        return;
    }
    BluetoothGatt gatt = mGattMap.get(device.address);
    if (!checkUuid(serviceUuid, writeUuid, gatt, device, callback)) {
        return;
    }

    OperationIdentify identify = getOperationIdentifyFromMap(mWriteCallbackMap, device.address, serviceUuid, writeUuid);
    if (identify != null) {
        mWriteCallbackMap.put(identify, callback);
    } else {
        mWriteCallbackMap.put(new OperationIdentify(device.address, serviceUuid, writeUuid), callback);
    }

    BluetoothGattService service = gatt.getService(UUID.fromString(serviceUuid));
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(writeUuid));
    boolean writable = (characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE |
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0;
    if (!writable) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER, "the characteristic is not writable", device);
            }
        });
        return;
    }
    if (!characteristic.setValue(data) || !gatt.writeCharacteristic(characteristic)) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER, "write fail because of unknown reason", device);
            }
        });
    }
}