Java 类android.bluetooth.BluetoothGattDescriptor 实例源码

项目:Sense-Hub-Android-Things    文件:nRF51822SensorEntity.java   
@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;
    }
}
项目:BluetoothCtrl    文件:BluetoothLeService.java   
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;
}
项目:BLE-PEPS    文件:BluetoothLeClass.java   
/**
 * Enables or disables notification on a give characteristic.
 *
 * @param characteristic Characteristic to act on.
 * @param enabled If true, enable notification.  False otherwise.
 */
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
    for(BluetoothGattDescriptor dp:descriptors){
        dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(dp);
    }




}
项目:BLE-PEPS    文件:BluetoothLeClass.java   
/**
 * Enables or disables notification on a give characteristic.
 *
 * @param characteristic Characteristic to act on.
 * @param enabled If true, enable notification.  False otherwise.
 */
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
    for(BluetoothGattDescriptor dp:descriptors){
        dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(dp);
    }




}
项目:Quick-Bluetooth-LE    文件:BLEServer.java   
public BLEServer(Context context, BLEServerDelegate delegate){
    this.context = context;
    this.delegate = delegate;
    if(this.delegate == null){
        this.delegate = new BLEServerDelegate() {
            @Override
            public void onAdvertise(AdvertiseError error) {}
            @Override
            public void onDeviceConnected(BluetoothDevice device) {}
            @Override
            public void onDeviceDisconnected(BluetoothDevice device) {}
            @Override
            public void onCharacteristicChangedServer(BluetoothGattCharacteristic characteristic) {}
            @Override
            public void onDescriptorChanged(BluetoothGattDescriptor descriptor) {}
        };
    }
}
项目:sample-bluetooth-le-gattserver    文件:GattServerActivity.java   
@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset,
                                    BluetoothGattDescriptor descriptor) {
    if (TimeProfile.CLIENT_CONFIG.equals(descriptor.getUuid())) {
        Log.d(TAG, "Config descriptor read");
        byte[] returnValue;
        if (mRegisteredDevices.contains(device)) {
            returnValue = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
        } else {
            returnValue = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
        }
        mBluetoothGattServer.sendResponse(device,
                requestId,
                BluetoothGatt.GATT_FAILURE,
                0,
                returnValue);
    } else {
        Log.w(TAG, "Unknown descriptor read request");
        mBluetoothGattServer.sendResponse(device,
                requestId,
                BluetoothGatt.GATT_FAILURE,
                0,
                null);
    }
}
项目:ITagAntiLost    文件:BleService.java   
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);
    }
  }
}
项目:Bluetooth_BLE    文件:LiteBleConnector.java   
/**
 * 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;
}
项目:blefun-androidthings    文件:GattServer.java   
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
    if (DESCRIPTOR_CONFIG.equals(descriptor.getUuid())) {
        if (Arrays.equals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE, value)) {
            Log.d(TAG, "Subscribe device to notifications: " + device);
            mRegisteredDevices.add(device);
        } else if (Arrays.equals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE, value)) {
            Log.d(TAG, "Unsubscribe device from notifications: " + device);
            mRegisteredDevices.remove(device);
        }

        if (responseNeeded) {
            mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, null);
        }
    } else {
        Log.w(TAG, "Unknown descriptor write request");
        if (responseNeeded) {
            mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_FAILURE, 0, null);
        }
    }
}
项目:Android-DFU-App    文件:BleManager.java   
@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;
}
项目:Android-DFU-App    文件:BleManager.java   
@Override
public final boolean enableIndications(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_INDICATE) == 0)
        return false;

    gatt.setCharacteristicNotification(characteristic, true);
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
    if (descriptor != null) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        return gatt.writeDescriptor(descriptor);
    }
    return false;
}
项目:BLE-HID-Peripheral-for-Android    文件:HidPeripheral.java   
@Override
public void onDescriptorWriteRequest(final BluetoothDevice device, final int requestId, final BluetoothGattDescriptor descriptor, final boolean preparedWrite, final boolean responseNeeded, final int offset, final byte[] value) {
    super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);
    Log.d(TAG, "onDescriptorWriteRequest descriptor: " + descriptor.getUuid() + ", value: " + Arrays.toString(value) + ", responseNeeded: " + responseNeeded + ", preparedWrite: " + preparedWrite);

    descriptor.setValue(value);

    if (responseNeeded) {
        if (BleUuidUtils.matches(DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION, descriptor.getUuid())) {
            // send empty
            if (gattServer != null) {
                gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, EMPTY_BYTES);
            }
        }
    }
}
项目:Android-DFU-App    文件:BleManager.java   
/**
 * Enables notifications on given characteristic
 *
 * @return true is the request has been sent, false if one of the arguments was <code>null</code> or the characteristic does not have the CCCD.
 */
protected 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;

    Logger.d(mLogSession, "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
    gatt.setCharacteristicNotification(characteristic, true);
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
    if (descriptor != null) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        Logger.v(mLogSession, "Enabling notifications for " + characteristic.getUuid());
        Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
        return gatt.writeDescriptor(descriptor);
    }
    return false;
}
项目:Sense-Hub-Android-Things    文件:nRF51822SensorEntity.java   
@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;
    }
}
项目:Bluetooth_BLE    文件:LiteBleConnector.java   
/**
 * 读描述符
 * @param bleCallback
 */
private void handleDescriptorReadCallback(final BleDescriptorCallback bleCallback) {
    if (bleCallback != null) {
        listenAndTimer(bleCallback, MSG_READ_DES, new BluetoothGattCallback() {
            AtomicBoolean msgRemoved = new AtomicBoolean(false);

            @Override
            public void onDescriptorRead(BluetoothGatt gatt,
                                         BluetoothGattDescriptor descriptor, int status) {
                if (!msgRemoved.getAndSet(true)) {
                    handler.removeMessages(MSG_READ_DES, this);
                }
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    bleCallback.onSuccess(descriptor);
                } else {
                    bleCallback.onFailure(new GattException(status));
                }
            }
        });
    }
}
项目:Android-DFU-App    文件:BleManager.java   
@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);
        }
    }
}
项目:microbit    文件:DfuBaseService.java   
@Override
public void onDescriptorRead(final BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, final int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        if (CLIENT_CHARACTERISTIC_CONFIG.equals(descriptor.getUuid())) {
            if (SERVICE_CHANGED_UUID.equals(descriptor.getCharacteristic().getUuid())) {
                // We have enabled indications for the Service Changed characteristic
                mServiceChangedIndicationsEnabled = descriptor.getValue()[0] == 2;
                mRequestCompleted = true;
            }
        }
    } else {
        loge("Descriptor read error: " + status);
        mError = ERROR_CONNECTION_MASK | status;
    }
    // Notify waiting thread
    synchronized (mLock) {
        mLock.notifyAll();
    }
}
项目:sample-bluetooth-le-gattserver    文件:TimeProfile.java   
/**
 * Return a configured {@link BluetoothGattService} instance for the
 * Current Time Service.
 */
public static BluetoothGattService createTimeService() {
    BluetoothGattService service = new BluetoothGattService(TIME_SERVICE,
            BluetoothGattService.SERVICE_TYPE_PRIMARY);

    // Current Time characteristic
    BluetoothGattCharacteristic currentTime = new BluetoothGattCharacteristic(CURRENT_TIME,
            //Read-only characteristic, supports notifications
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PERMISSION_READ);
    BluetoothGattDescriptor configDescriptor = new BluetoothGattDescriptor(CLIENT_CONFIG,
            //Read/write descriptor
            BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE);
    currentTime.addDescriptor(configDescriptor);

    // Local Time Information characteristic
    BluetoothGattCharacteristic localTime = new BluetoothGattCharacteristic(LOCAL_TIME_INFO,
            //Read-only characteristic
            BluetoothGattCharacteristic.PROPERTY_READ,
            BluetoothGattCharacteristic.PERMISSION_READ);

    service.addCharacteristic(currentTime);
    service.addCharacteristic(localTime);

    return service;
}
项目:Make-A-Pede-Android-App    文件:BluetoothLeService.java   
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (bluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    bluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
        descriptor.setValue(
                enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] {0x00, 0x00});
        bluetoothGatt.writeDescriptor(descriptor);
    }
}
项目:Snach-Android    文件:BLEManager.java   
private void setNotifySensor(BluetoothGatt gatt) {
    BluetoothGattCharacteristic characteristic = gatt.getService(Globals.SNACH_SYSTEM_SERVICE_UUID).getCharacteristic(Globals.SNACH_SYSTEM_UART_TX_UUID);
    gatt.setCharacteristicNotification(characteristic, true);

    BluetoothGattDescriptor desc = characteristic.getDescriptor(Globals.SNACH_DESCRIPTOR_UUID);
    if(desc != null) {
        desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        Log.i("BLE", "Descriptor write: " + gatt.writeDescriptor(desc));
    }

}
项目:android-ponewheel    文件:BluetoothUtilImpl.java   
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    Log.i(TAG, "onDescriptorWrite: " + status);
    descriptorWriteQueue.remove();  //pop the item that we just finishing writing
    //if there is more to write, do it!
    if(descriptorWriteQueue.size() > 0) {
        gatt.writeDescriptor(descriptorWriteQueue.element());
    } else if(characteristicReadQueue.size() > 0) {
        gatt.readCharacteristic(characteristicReadQueue.element());
    }
}
项目:AndroidSDK2.0    文件:BTLEAdt.java   
@Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            NLog.d("call onDescriptorWrite status : " + status);
            NLog.d("found service v2");

//            broadcastUpdate(ACTION_GATT_SERVICES_READY_TO_CONNECT);
            StartConnection();
        }
项目:TrainAppTFG    文件:BluetoothLeService.java   
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    super.onDescriptorWrite(gatt, descriptor, status);
    Log.d("BLUETOOTH", "onDescriptorWrite: " + status);
    sIsWritting = false;
    nextWrite(); //Si la cola de escriuras y no se está escribiendo -> escribeme
}
项目:TrainAppTFG    文件:BluetoothLeService.java   
public void mensaje_encenderSensorCC2650(){
    if(this.config != null){
        mBluetoothGatt.setCharacteristicNotification(movementCharacteristic, true);

        movementConf.setValue(ENCENDER_SENSOR_ACELEROMETRO);
        write(movementConf);

        this.config.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        write(config);

        this.movementPeriod.setValue(PERIODO_MOVEMENT_SENSOR);
        write(movementPeriod);
    }
}
项目:Bluetooth_BLE    文件:LiteBleConnector.java   
/**
 * write data to specified descriptor
 */
public boolean writeDescriptor(BluetoothGattDescriptor descriptor, byte[] data, BleDescriptorCallback bleCallback) {
    if (BleLog.isPrint) {
        BleLog.i(TAG, descriptor.getUuid() + " descriptor write bytes: "
                      + Arrays.toString(data) + " ,hex: " + HexUtil.encodeHexStr(data));
    }
    handleDescriptorWriteCallback(bleCallback);
    descriptor.setValue(data);
    return handleAfterInitialed(getBluetoothGatt().writeDescriptor(descriptor), bleCallback);
}
项目:NaiveDemos    文件:BleOperationService.java   
/**
 * 操作2:通知
 * 操作结果返回到 GattCallback 中 onCharacteristicChanged()回调方法当中
 *
 * @param characteristic 需要操作的特征
 * @param enable         若为 true 表示要开启notify,若为 false 表示要停止 notify
 * @return 若为 true 表示设置 notify 状态成功
 */
private boolean notifyCharacteristic(BluetoothGattCharacteristic characteristic,
                                     boolean enable) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        LogUtils.w(TAG, "蓝牙适配器为 null:notify");
        return false;
    }
    if (mState != State.STATE_CONNECTED) {
        LogUtils.w(TAG, "notify(): 当前状态为非连接状态");
        return false;
    }
    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0) {
        LogUtils.w(TAG, "characteristic不能被notify");
        return false;
    }

    boolean suc = mBluetoothGatt.setCharacteristicNotification(characteristic, enable);
    if (!suc) {
        //notify 操作失败
        LogUtils.w(TAG, "set notify 操作失败");
        return false;
    }
    //特殊操作,注意三元表达式
    BluetoothGattDescriptor descriptor =
            characteristic.getDescriptor(Attributes.UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR);

    if (null != descriptor) {
        descriptor.setValue(enable ?
                BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
                BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
        return mBluetoothGatt.writeDescriptor(descriptor);
    }

    return false;

}
项目:sample-bluetooth-le-gattserver    文件:GattServerActivity.java   
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId,
                                     BluetoothGattDescriptor descriptor,
                                     boolean preparedWrite, boolean responseNeeded,
                                     int offset, byte[] value) {
    if (TimeProfile.CLIENT_CONFIG.equals(descriptor.getUuid())) {
        if (Arrays.equals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE, value)) {
            Log.d(TAG, "Subscribe device to notifications: " + device);
            mRegisteredDevices.add(device);
        } else if (Arrays.equals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE, value)) {
            Log.d(TAG, "Unsubscribe device from notifications: " + device);
            mRegisteredDevices.remove(device);
        }

        if (responseNeeded) {
            mBluetoothGattServer.sendResponse(device,
                    requestId,
                    BluetoothGatt.GATT_SUCCESS,
                    0,
                    null);
        }
    } else {
        Log.w(TAG, "Unknown descriptor write request");
        if (responseNeeded) {
            mBluetoothGattServer.sendResponse(device,
                    requestId,
                    BluetoothGatt.GATT_FAILURE,
                    0,
                    null);
        }
    }
}
项目:bluewatcher    文件:PhoneFinderService.java   
@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;
            }
        }
    }
}
项目:Quick-Bluetooth-LE    文件:MainActivity.java   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    deviceCount = (TextView) findViewById(R.id.lbl_device_count);
    slider = (SeekBar) findViewById(R.id.seekBar);
    listView = (ListView) findViewById(R.id.lst_main_options);
    deviceList = new ArrayList<>();
    deviceAddresses = new ArrayList<>();
    deviceNames = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1);
    devices = (ListView) findViewById(R.id.lst_discovered_devices);
    devices.setAdapter(deviceNames);
    listView.setOnItemClickListener(this);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        bleServer = new BLEServer(MainActivity.this, this);
        sliderCharacteristic = bleServer.buildCharacteristic(sliderCharacteristicUuid, new BluetoothGattDescriptor[0],
                BLEServer.CharProperties.Read | BLEServer.CharProperties.Write | BLEServer.CharProperties.Notify,
                BLEServer.CharPermissions.Read | BLEServer.CharPermissions.Write);
        comService = bleServer.buildService(communicationServiceUuid,
                BLEServer.ServiceType.Primary, new BluetoothGattCharacteristic[]{sliderCharacteristic});
        bleServer.addService(comService);
    }

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
        bleClient = new BLEClient(MainActivity.this, this);
        bleClient.setUseNewMethod(false); //This requires more work with permissions than the old method

    }

}
项目:bluewatcher    文件:WatchCtrlService.java   
@Override
public void actionGattServicesDiscovered(Device deviceName) {
    if (bleService.getInternalBleService() == null)
        return;
    connectedDevice = deviceName;
    if (!deviceName.isGBA400() && !connectedDevice.isSTB1000())
        return;
    List<BluetoothGattService> services = bleService.getInternalBleService().getSupportedGattServices();
    for (BluetoothGattService service : services) {
        if (service.getUuid().equals(WATCH_FEATURES_SERVICE_UUID)) {
            BluetoothGattCharacteristic characteristic = service.getCharacteristic(FUNCTION_SWITCH_CHARACTERISTIC);
            if (characteristic != null) {
                BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CCC_DESCRIPTOR_UUID);
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                bleService.getInternalBleService().writeDescriptor(descriptor);
                gattCharacteristic = characteristic;
                gattCharacteristic.setValue(READY_MESSAGE.getBytes());
                gattCharacteristic.setWriteType(2);
                bleService.getInternalBleService().writeCharacteristic(gattCharacteristic);
                clientAvailable = true;
                Log.i(TAG, "WatchCtrlService - Discovered!" + characteristic.getUuid().toString());
                reloadPhoneControlModes();
                break;
            }
        }
    }
}
项目:Quick-Bluetooth-LE    文件:ServerTest.java   
@Override
public void onDescriptorChanged(final BluetoothGattDescriptor descriptor) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            outText.setText("Descriptor\nValue: " + server.getDescriptorValueString(serviceUuid, charUuid, descUuid));
        }
    });
}
项目:bluewatcher    文件:WatchCtrlService.java   
@Override
public BluetoothGattService createService() {
    BluetoothGattService bluetoothgattservice = new BluetoothGattService(WATCH_CTRL_SERVICE_UUID, 0);
    BluetoothGattCharacteristic bluetoothgattcharacteristic = new BluetoothGattCharacteristic(KEY_CONTAINER_CHARACTERISTIC_UUID, 4, 16);
    bluetoothgattcharacteristic.setValue(new byte[0]);
    BluetoothGattCharacteristic bluetoothgattcharacteristic1 = new BluetoothGattCharacteristic(NAME_OF_APP_CHARACTERISTIC_UUID, 2, 3);
    bluetoothgattcharacteristic1.setValue(READY_MESSAGE.getBytes());
    BluetoothGattDescriptor bluetoothgattdescriptor = new BluetoothGattDescriptor(CCC_DESCRIPTOR_UUID, 17);
    bluetoothgattdescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    bluetoothgattcharacteristic1.addDescriptor(bluetoothgattdescriptor);
    bluetoothgattservice.addCharacteristic(bluetoothgattcharacteristic);
    bluetoothgattservice.addCharacteristic(bluetoothgattcharacteristic1);
    return bluetoothgattservice;
}
项目:Quick-Bluetooth-LE    文件:BLEServer.java   
public boolean setDescriptorValue(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, String 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.getBytes());
}
项目:Quick-Bluetooth-LE    文件:BLEServer.java   
public BluetoothGattCharacteristic buildCharacteristic(UUID characteristicUuid, BluetoothGattDescriptor[] descriptors, int properties, int permissions){
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(characteristicUuid, properties, permissions);
    for(BluetoothGattDescriptor descriptor : descriptors){
        characteristic.addDescriptor(descriptor);
    }
    return characteristic;
}
项目:Quick-Bluetooth-LE    文件:BLEClient.java   
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);
}
项目:Quick-Bluetooth-LE    文件:BLEClient.java   
public boolean setDescriptorValue(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, String 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.getBytes());
    return gattConnection.writeDescriptor(descriptor);
}
项目:Quick-Bluetooth-LE    文件:BLEClient.java   
public String getDescriptorValueString(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid){
    BluetoothGattService service = gattConnection.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 new String(descriptor.getValue());
}
项目:Quick-Bluetooth-LE    文件:BLEClient.java   
public BluetoothGattDescriptor getDescriptor(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid){
    if(gattConnection == null)
        return null;
    BluetoothGattService service = gattConnection.getService(serviceUuid);
    if(service == null)
        return null;
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid);
    if(characteristic == null)
        return null;
    return characteristic.getDescriptor(descriptorUuid);
}
项目:Quick-Bluetooth-LE    文件:BLEClient.java   
public void receiveNotifications(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, boolean receive){
    BluetoothGattCharacteristic characteristic = getCharacteristic(serviceUuid, characteristicUuid);
    gattConnection.setCharacteristicNotification(characteristic, receive);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    gattConnection.writeDescriptor(descriptor);
}
项目:sdc-1-quickstart-android    文件:MainActivity.java   
/**
 * Callback indicating the result of a descriptor write operation.
 *
 * @param gatt       GATT client invoked {@link BluetoothGatt#writeDescriptor}
 * @param descriptor Descriptor that was writte to the associated
 *                   remote device.
 * @param status     The result of the write operation
 *                   {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
 */
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    super.onDescriptorWrite(gatt, descriptor, status);

    // boolean indicating whether or not the next step is successful, default is false
    boolean success = false;

    // Check if writing descriptor was successful and force the action notification if it
    // was
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // Check if the SPIN Service is found
        final BluetoothGattService spinService = gatt.getService(SPIN_SERVICE_UUID);
        if (spinService != null) {
            // Check if the Command Characteristic is found, write the new value and store
            // the result
            final BluetoothGattCharacteristic commandCharacteristic
                    = spinService.getCharacteristic(COMMAND_CHARACTERISTIC_UUID);
            if (commandCharacteristic != null) {
                // Set the value to 0x0801
                commandCharacteristic.setValue(
                        new byte[]{
                                (byte) 0x08,    // commandId = force action notification (8)
                                (byte) 0x01     // enable = false (0) or true (1)
                        }
                );

                success = gatt.writeCharacteristic(commandCharacteristic);
            }
        }
    }

    onStep(gatt, success);
}