android.bluetooth.BluetoothGatt#getService ( )源码实例Demo

下面列出了android.bluetooth.BluetoothGatt#getService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Android-nRF-Toolbox   文件: UARTProfile.java
@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;
	}
 
源代码2 项目: BlueSTSDK_Android   文件: Node.java
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;
        }
 
源代码3 项目: bean-sdk-android   文件: TestBeanBLE.java
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);
    }
 
源代码4 项目: neatle   文件: ReadCommand.java
@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));
}
 
源代码5 项目: bleYan   文件: AppBleService.java
@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);
        }
    }
}
 
源代码6 项目: Android-nRF-Toolbox   文件: HRManager.java
@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;
}
 
源代码7 项目: EasyBle   文件: BleGattImpl.java
@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);
            }
        });
    }
}
 
源代码8 项目: EasyBle   文件: BleGattImpl.java
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;
}
 
源代码9 项目: BleLib   文件: MultipleBleService.java
/**
 * 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;
}
 
源代码10 项目: thunderboard-android   文件: BleUtils.java
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);
}
 
源代码11 项目: Android-nRF-Toolbox   文件: RSCManager.java
@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;
}
 
源代码12 项目: Android-nRF-Toolbox   文件: BatteryManager.java
@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;
}
 
源代码13 项目: bean-sdk-android   文件: TestBeanBLE.java
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");
}
 
源代码14 项目: Android-BLE-Library   文件: BleManagerHandler.java
@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);
}
 
源代码15 项目: Android-BLE   文件: BleRequestImpl.java
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;
}
 
源代码16 项目: GizwitsBLE   文件: AndroidBle.java
@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);
	}
}
 
源代码17 项目: Android-nRF-Toolbox   文件: TemplateManager.java
@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;
}
 
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);
}
 
源代码19 项目: android_wear_for_ios   文件: BLEService.java
@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);
                    }
                }
            }
        }
    }
}
 
源代码20 项目: EasyBle   文件: BleGattImpl.java
@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);
            }
        });
    }
}