當前位置: 首頁>>代碼示例>>Java>>正文


Java BluetoothGattCharacteristic.getDescriptor方法代碼示例

本文整理匯總了Java中android.bluetooth.BluetoothGattCharacteristic.getDescriptor方法的典型用法代碼示例。如果您正苦於以下問題:Java BluetoothGattCharacteristic.getDescriptor方法的具體用法?Java BluetoothGattCharacteristic.getDescriptor怎麽用?Java BluetoothGattCharacteristic.getDescriptor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.bluetooth.BluetoothGattCharacteristic的用法示例。


在下文中一共展示了BluetoothGattCharacteristic.getDescriptor方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: onCharacteristicChanged

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    Log.d(mTAG, "onCharacteristicChanged");
    byte[] mValue = characteristic.getValue();
    if(characteristic.getUuid().equals(UUID_nRF51822_GET_TEMP)){
        mAirTemperature = (mValue[0] << 8 | mValue[1])/10;
        gatt.setCharacteristicNotification(characteristic, false);
        List<BluetoothGattCharacteristic> lstChars = mGattnRF51822Service.getCharacteristics();
        for(BluetoothGattCharacteristic mCharacteristic : lstChars){
            List<BluetoothGattDescriptor> descriptors = mCharacteristic.getDescriptors();
            BluetoothGattDescriptor mGattnRF51822Descriptor = mCharacteristic.getDescriptor(UUID_nRF51822_DESCRIPTOR_ID);
            if(mGattnRF51822Descriptor != null && mCharacteristic.getUuid().equals(UUID_nRF51822_GET_LIGHT)){
                gatt.setCharacteristicNotification(mCharacteristic, true);
                byte[] mDesValue = {0x01, 0x00};
                mGattnRF51822Descriptor.setValue(mDesValue);
                gatt.writeDescriptor(mGattnRF51822Descriptor);
            }
        }
    }
    else if(characteristic.getUuid().equals(UUID_nRF51822_GET_LIGHT)){
        mLight = (mValue[0] << 8 | mValue[1]);
        gatt.close();
        Log.d(mTAG, "onCharacteristicChanged data=" + getData());
        mIsConnecting = false;
    }
}
 
開發者ID:dmtan90,項目名稱:Sense-Hub-Android-Things,代碼行數:27,代碼來源:nRF51822SensorEntity.java

示例2: setCharacteristicNotification

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
 * Enable or disable notifications/indications for a given characteristic.
 *
 * 是否允許刷新特征碼
 *
 * <p>Once notifications are enabled for a characteristic, a
 * {@link BluetoothGattCallback#onCharacteristicChanged} callback will be
 * triggered if the remote device indicates that the given characteristic
 * has changed.
 *
 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
 *
 * @param characteristic The characteristic for which to enable notifications
 * @param enable         Set to true to enable notifications/indications
 * @return true, if the requested notification status was set successfully
 */
public boolean setCharacteristicNotification(BluetoothGatt gatt,
                                             BluetoothGattCharacteristic characteristic,
                                             boolean enable) {
    if (gatt != null && characteristic != null) {
        BleLog.i(TAG, "Characteristic set notification value: " + enable);
        /** 是否允許刷新特征碼獲取數據 */
        boolean success = gatt.setCharacteristicNotification(characteristic, enable);

        // This is specific to Heart Rate Measurement.(心率測量專用)
        if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
            BleLog.i(TAG, "Heart Rate Measurement set [descriptor] notification value: " + enable);
            BluetoothGattDescriptor descriptor = characteristic
                    .getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));

            // 向描述符中寫入 'ENABLE_NOTIFICATION_VALUE'
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(descriptor);
        }
        return success;
    }
    return false;
}
 
開發者ID:qiu-yongheng,項目名稱:Bluetooth_BLE,代碼行數:39,代碼來源:LiteBleConnector.java

示例3: onServicesDiscovered

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(mTAG, "onServicesDiscovered");
    if(status == BluetoothGatt.GATT_SUCCESS){
        mGattnRF51822Service = gatt.getService(UUID_nRF51822_SERVICE_ID);
        if(mGattnRF51822Service != null){
            List<BluetoothGattCharacteristic> lstChars = mGattnRF51822Service.getCharacteristics();
            for(BluetoothGattCharacteristic mCharacteristic : lstChars){
                List<BluetoothGattDescriptor> descriptors = mCharacteristic.getDescriptors();
                BluetoothGattDescriptor mGattnRF51822Descriptor = mCharacteristic.getDescriptor(UUID_nRF51822_DESCRIPTOR_ID);
                if(mGattnRF51822Descriptor != null && mCharacteristic.getUuid().equals(UUID_nRF51822_GET_TEMP)){
                    gatt.setCharacteristicNotification(mCharacteristic, true);
                    byte[] mValue = {0x01, 0x00};
                    mGattnRF51822Descriptor.setValue(mValue);
                    gatt.writeDescriptor(mGattnRF51822Descriptor);
                }
            }
        }
    }
    else{
        gatt.close();
        mIsConnecting = false;
    }
}
 
開發者ID:dmtan90,項目名稱:Sense-Hub-Android-Things,代碼行數:25,代碼來源:nRF51822SensorEntity.java

示例4: getSupportedGattServices

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public List<BluetoothGattService> getSupportedGattServices() {
    if (mBluetoothGatt == null) {
        return null;
    }

    List<BluetoothGattService> gattServices = mBluetoothGatt.getServices();

    for (BluetoothGattService gattService : gattServices) {
        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            String uuid = gattCharacteristic.getUuid().toString();
            if(uuid.equalsIgnoreCase(UUID_NOTIFY.toString())){
                mNotifyCharacteristic = gattCharacteristic;
                mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
                ULog.i("setCharacteristicNotification : " + uuid);
                BluetoothGattDescriptor descriptor = gattCharacteristic
                        .getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descriptor);
            }
        }
    }
    return gattServices;
}
 
開發者ID:WillFlower,項目名稱:BluetoothCtrl,代碼行數:25,代碼來源:BluetoothLeService.java

示例5: setCharacteristicNotification

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
private void setCharacteristicNotification(
    @NonNull BluetoothGatt bluetoothgatt,
    @NonNull BluetoothGattCharacteristic bluetoothgattcharacteristic,
    boolean flag
) {
  bluetoothgatt.setCharacteristicNotification(bluetoothgattcharacteristic, flag);
  if (FIND_ME_CHARACTERISTIC.equals(bluetoothgattcharacteristic.getUuid())) {
    BluetoothGattDescriptor descriptor = bluetoothgattcharacteristic.getDescriptor(
        CLIENT_CHARACTERISTIC_CONFIG
    );
    if (descriptor != null) {
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
      bluetoothgatt.writeDescriptor(descriptor);
    }
  }
}
 
開發者ID:drfonfon,項目名稱:ITagAntiLost,代碼行數:17,代碼來源:BleService.java

示例6: onCharacteristicChanged

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
public final void onCharacteristicChanged(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
	final String data = ParserUtils.parse(characteristic);

	if (isBatteryLevelCharacteristic(characteristic)) {
		Logger.i(mLogSession, "Notification received from " + characteristic.getUuid() + ", value: " + data);
		final int batteryValue = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
		Logger.a(mLogSession, "Battery level received: " + batteryValue + "%");
		mCallbacks.onBatteryValueReceived(batteryValue);
	} else {
		final BluetoothGattDescriptor cccd = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
		final boolean notifications = cccd == null || cccd.getValue() == null || cccd.getValue().length != 2 || cccd.getValue()[0] == 0x01;

		if (notifications) {
			Logger.i(mLogSession, "Notification received from " + characteristic.getUuid() + ", value: " + data);
			onCharacteristicNotified(gatt, characteristic);
		} else { // indications
			Logger.i(mLogSession, "Indication received from " + characteristic.getUuid() + ", value: " + data);
			onCharacteristicIndicated(gatt, characteristic);
		}
	}
}
 
開發者ID:runtimeco,項目名稱:Android-DFU-App,代碼行數:23,代碼來源:BleManager.java

示例7: enableNotifications

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
public final boolean enableNotifications(final BluetoothGattCharacteristic characteristic) {
	final BluetoothGatt gatt = mBluetoothGatt;
	if (gatt == null || characteristic == null)
		return false;

	// Check characteristic property
	final int properties = characteristic.getProperties();
	if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0)
		return false;

	gatt.setCharacteristicNotification(characteristic, true);
	final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
	if (descriptor != null) {
		descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
		return gatt.writeDescriptor(descriptor);
	}
	return false;
}
 
開發者ID:runtimeco,項目名稱:Android-DFU-App,代碼行數:20,代碼來源:BleManager.java

示例8: actionGattServicesDiscovered

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
public void actionGattServicesDiscovered(Device deviceName) {
	if (bleService.getInternalBleService() == null)
		return;
	
	if(!isPhoneFindingEnabled())
		return;
	
	List<BluetoothGattService> services = bleService.getInternalBleService().getSupportedGattServices();
	for (BluetoothGattService service : services) {
		if (service.getUuid().equals(CASIO_IMMEDIATE_ALERT_SERVICE_UUID)) {
			BluetoothGattCharacteristic characteristic = service.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID);
			if (characteristic != null) {
				bleService.getInternalBleService().setCharacteristicNotification(characteristic, true);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {}
				BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CCC_DESCRIPTOR_UUID);
				if(descriptor == null)
					return;
				descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
				bleService.getInternalBleService().writeDescriptor(descriptor);
				Log.i(TAG, "PhoneFinderService - Discovered!");
				connectedDevice = deviceName;
				break;
			}
		}
	}
}
 
開發者ID:masterjc,項目名稱:bluewatcher,代碼行數:30,代碼來源:PhoneFinderService.java

示例9: setDescriptorValue

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public boolean setDescriptorValue(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, byte[] value){
	BluetoothGattService service = gattServer.getService(serviceUuid);
	if(service == null)
		return false;
	BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid);
	if(characteristic == null)
		return false;
	BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
	if(descriptor == null)
		return false;
	return descriptor.setValue(value);
}
 
開發者ID:MB3hel,項目名稱:Quick-Bluetooth-LE,代碼行數:13,代碼來源:BLEServer.java

示例10: getDescriptorValue

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public byte[] getDescriptorValue(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid){
	BluetoothGattService service = gattServer.getService(serviceUuid);
	if(service == null)
		return null;
	BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid);
	if(characteristic == null)
		return null;
	BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
	if(descriptor == null)
		return null;
	return descriptor.getValue();
}
 
開發者ID:MB3hel,項目名稱:Quick-Bluetooth-LE,代碼行數:13,代碼來源:BLEServer.java

示例11: setBatteryNotifications

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
 * This method tries to enable notifications on the Battery Level characteristic.
 *
 * @param enable <code>true</code> to enable battery notifications, false to disable
 * @return true if request has been sent
 */
public boolean setBatteryNotifications(final boolean enable) {
	final BluetoothGatt gatt = mBluetoothGatt;
	if (gatt == null) {
		return false;
	}

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

	final BluetoothGattCharacteristic batteryLevelCharacteristic = batteryService.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC);
	if (batteryLevelCharacteristic == null)
		return false;

	// Check characteristic property
	final int properties = batteryLevelCharacteristic.getProperties();
	if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0)
		return false;

	gatt.setCharacteristicNotification(batteryLevelCharacteristic, enable);
	final BluetoothGattDescriptor descriptor = batteryLevelCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
	if (descriptor != null) {
		if (enable) {
			descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
			Logger.a(mLogSession, "Enabling battery level notifications...");
			Logger.v(mLogSession, "Enabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC);
			Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
		} else {
			descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
			Logger.a(mLogSession, "Disabling battery level notifications...");
			Logger.v(mLogSession, "Disabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC);
			Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x00-00)");
		}
		return gatt.writeDescriptor(descriptor);
	}
	return false;
}
 
開發者ID:runtimeco,項目名稱:Android-DFU-App,代碼行數:44,代碼來源:BleManager.java

示例12: setDescriptorValue

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public boolean setDescriptorValue(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, byte[] value){
	if(gattConnection == null)
		return false;
	BluetoothGattService service = gattConnection.getService(serviceUuid);
	if(service == null)
		return false;
	BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid);
	if(characteristic == null)
		return false;
	BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
	if(descriptor == null)
		return false;
	descriptor.setValue(value);
	return gattConnection.writeDescriptor(descriptor);
}
 
開發者ID:MB3hel,項目名稱:Quick-Bluetooth-LE,代碼行數:16,代碼來源:BLEClient.java

示例13: getNotificationsWithDescriptor

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
 * Get notification but also set descriptor to Enable notification. You need to wait couple of
 *      seconds before you could use it (at least in the mi band 2)
 * @param service
 * @param Characteristics
 */
public void getNotificationsWithDescriptor(UUID service, UUID Characteristics, UUID Descriptor) {
    if (!isConnectedToGatt || myGatBand == null) {
        Log.d(TAG, "Cant get notifications from BLE, not initialized.");
        return;
    }

    Log.d(TAG, "* Getting gatt service, UUID:" + service.toString());
    BluetoothGattService myGatService =
            myGatBand.getService(service/*Consts.UUID_SERVICE_MIBAND_SERVICE*/);
    if (myGatService != null) {
        Log.d(TAG, "* Getting gatt Characteristic. UUID: " + Characteristics.toString());

        BluetoothGattCharacteristic myGatChar
                = myGatService.getCharacteristic(Characteristics/*Consts.UUID_BUTTON_TOUCH*/);
        if (myGatChar != null) {
            Log.d(TAG, "* Statring listening");

            // second parametes is for starting\stopping the listener.
            boolean status = myGatBand.setCharacteristicNotification(myGatChar, true);
            Log.d(TAG, "* Set notification status :" + status);

            BluetoothGattDescriptor myDescriptor
                    = myGatChar.getDescriptor(Descriptor/*Consts.UUID_DESCRIPTOR_UPDATE_NOTIFICATION*/);
            if (myDescriptor != null) {
                Log.d(TAG, "Writing decriptor: " + Descriptor.toString());
                myDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                status = myGatBand.writeDescriptor(myDescriptor);
                Log.d(TAG, "Writing decriptors result: " + status);
            }
        }
    }
}
 
開發者ID:yonixw,項目名稱:mi-band-2,代碼行數:39,代碼來源:BLEMiBand2Helper.java

示例14: isNotificationEnabled

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public boolean isNotificationEnabled(String mac, BluetoothGattCharacteristic characteristic) {
    BluetoothGatt bluetoothGatt = checkAndGetGattItem(mac);
    boolean result = false;
    if (bluetoothGatt != null) {
        BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(GattInfo.CLIENT_CHARACTERISTIC_CONFIG);
        if (clientConfig != null) {
            result = clientConfig.getValue() == BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
        }
    }
    return result;
}
 
開發者ID:UDOOboard,項目名稱:UDOOBluLib-android,代碼行數:12,代碼來源:UdooBluService.java

示例15: enableCCCD

import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
 * Enables or disables the notifications for given characteristic. This method is SYNCHRONOUS and wait until the
 * {@link android.bluetooth.BluetoothGattCallback#onDescriptorWrite(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattDescriptor, int)} will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}. If
 * connection state will change, or an error will occur, an exception will be thrown.
 *
 * @param gatt           the GATT device
 * @param characteristic the characteristic to enable or disable notifications for
 * @param type           {@link #NOTIFICATIONS} or {@link #INDICATIONS}
 * @throws DfuException
 * @throws UploadAbortedException
 */
private void enableCCCD(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int type) throws DeviceDisconnectedException, DfuException, UploadAbortedException {
    final String debugString = type == NOTIFICATIONS ? "notifications" : "indications";
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to set " + debugString + " state", mConnectionState);

    mReceivedData = null;
    mError = 0;
    if ((type == NOTIFICATIONS && mNotificationsEnabled) || (type == INDICATIONS && mServiceChangedIndicationsEnabled))
        return;

    logi("Enabling " + debugString + "...");
    sendLogBroadcast(LOG_LEVEL_VERBOSE, "Enabling " + debugString + " for " + characteristic.getUuid());

    // enable notifications locally
    gatt.setCharacteristicNotification(characteristic, true);

    // enable notifications on the device
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
    descriptor.setValue(type == NOTIFICATIONS ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    sendLogBroadcast(LOG_LEVEL_DEBUG, "gatt.writeDescriptor(" + descriptor.getUuid() + (type == NOTIFICATIONS ? ", value=0x01-00)" : ", value=0x02-00)"));
    gatt.writeDescriptor(descriptor);

    // We have to wait until device receives a response or an error occur
    try {
        synchronized (mLock) {
            while ((((type == NOTIFICATIONS && !mNotificationsEnabled) || (type == INDICATIONS && !mServiceChangedIndicationsEnabled))
                    && mConnectionState == STATE_CONNECTED_AND_READY && mError == 0 && !mAborted) || mPaused)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }

    if (mAborted)
        throw new UploadAbortedException();

    if (mError != 0)
        throw new DfuException("Unable to set " + debugString + " state", mError);

    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to set " + debugString + " state", mConnectionState);
}
 
開發者ID:Samsung,項目名稱:microbit,代碼行數:54,代碼來源:DfuBaseService.java


注:本文中的android.bluetooth.BluetoothGattCharacteristic.getDescriptor方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。