android.bluetooth.BluetoothGatt#GATT_SUCCESS源码实例Demo

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

源代码1 项目: android   文件: BLEService.java
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);

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

    if (BluetoothGatt.GATT_SUCCESS == status) {
        List<BluetoothGattService> gattServices = gatt.getServices();
        if (null == gattServices || gattServices.size() == 0) {
            return;
        }
        for (BluetoothGattService gattService : gattServices) {
            List<BluetoothGattCharacteristic> characteristics = gattService.getCharacteristics();
            for (BluetoothGattCharacteristic characteristic : characteristics) {
                String uuid = characteristic.getUuid().toString();
                print("UUID    Cha:" + uuid);
                if (UUID_BT5_DATA.equalsIgnoreCase(uuid)) {
                    mBluetoothGatt.setCharacteristicNotification(characteristic, true);
                    print("开始监听...");
                }
            }
        }
    }
}
 
源代码2 项目: Android-BLE-Library   文件: BleManagerHandler.java
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public final void onPhyUpdate(@NonNull final BluetoothGatt gatt,
							  @PhyValue final int txPhy, @PhyValue final int rxPhy,
							  final int status) {
	if (status == BluetoothGatt.GATT_SUCCESS) {
		log(Log.INFO, "PHY updated (TX: " + ParserUtils.phyToString(txPhy) +
				", RX: " + ParserUtils.phyToString(rxPhy) + ")");
		if (request instanceof PhyRequest) {
			((PhyRequest) request).notifyPhyChanged(gatt.getDevice(), txPhy, rxPhy);
			request.notifySuccess(gatt.getDevice());
		}
	} else {
		log(Log.WARN, "PHY updated failed with status " + status);
		if (request instanceof PhyRequest) {
			request.notifyFail(gatt.getDevice(), status);
			awaitingRequest = null;
		}
		postCallback(c -> c.onError(gatt.getDevice(), ERROR_PHY_UPDATE, status));
	}
	// PHY update may be requested by the other side, or the Android, without explicitly
	// requesting it. Proceed with the queue only when update was requested.
	if (checkCondition() || request instanceof PhyRequest) {
		nextRequest(true);
	}
}
 
源代码3 项目: EasyBle   文件: BleGattImpl.java
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, final int rssi, int status) {
    super.onReadRemoteRssi(gatt, rssi, status);
    if (status == BluetoothGatt.GATT_SUCCESS) {
        String address = gatt.getDevice().getAddress();
        final BleRssiCallback callback = mRssiCallbackMap.get(address);
        final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (callback != null) {
                    callback.onRssi(rssi, device);
                }
            }
        });
    }
}
 
源代码4 项目: easyble-x   文件: ConnectionImpl.java
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    if (originCallback != null) {
        easyBle.getExecutorService().execute(() -> originCallback.onCharacteristicRead(gatt, characteristic, status));
    }
    if (currentRequest != null) {
        if (currentRequest.type == RequestType.READ_CHARACTERISTIC) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                notifyCharacteristicRead(currentRequest, characteristic.getValue());
            } else {
                handleGattStatusFailed();
            }
            executeNextRequest();
        }
    }
}
 
@Override
public int writeCharacteristic(BluetoothGattCharacteristic characteristic, int offset, byte[] value) {
  if (offset != 0) {
    return BluetoothGatt.GATT_INVALID_OFFSET;
  }
  // Heart Rate control point is a 8bit characteristic
  if (value.length != 1) {
    return BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
  }
  if ((value[0] & 1) == 1) {
    getActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        mHeartRateMeasurementCharacteristic.setValue(INITIAL_EXPENDED_ENERGY,
            EXPENDED_ENERGY_FORMAT, /* offset */ 2);
        mEditTextEnergyExpended.setText(Integer.toString(INITIAL_EXPENDED_ENERGY));
      }
    });
  }
  return BluetoothGatt.GATT_SUCCESS;
}
 
源代码6 项目: Android-nRF-Toolbox   文件: BleManager.java
@Override
public final void onDescriptorWrite(final BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, final int status) {
	if (status == BluetoothGatt.GATT_SUCCESS) {
		// The value has been written. Notify the profile and proceed with the initialization queue.
		profile.onDescriptorWrite(gatt, descriptor);
		operationInProgress = false;
		nextRequest();
	} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
		if (gatt.getDevice().getBondState() != BluetoothDevice.BOND_NONE) {
			// This should never happen but it used to: http://stackoverflow.com/a/20093695/2115352
			DebugLogger.w(TAG, ERROR_AUTH_ERROR_WHILE_BONDED);
			onError(gatt.getDevice(), ERROR_AUTH_ERROR_WHILE_BONDED, status);
		}
	} else {
		DebugLogger.e(TAG, "onDescriptorWrite error " + status);
		onError(gatt.getDevice(), ERROR_WRITE_DESCRIPTOR, status);
	}
}
 
源代码7 项目: Android-BLE   文件: BleRequestImpl.java
@Override
public void onDescriptorWrite(BluetoothGatt gatt,
                              BluetoothGattDescriptor descriptor, int status) {
    if (gatt == null || gatt.getDevice() == null)return;
    UUID uuid = descriptor.getCharacteristic().getUuid();
    BleLog.d(TAG, "write descriptor uuid:" + uuid);
    synchronized (locker) {
        T bleDevice = getBleDeviceInternal(gatt.getDevice().getAddress());
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (null != descWrapperCallback){
                descWrapperCallback.onDescWriteSuccess(bleDevice, descriptor);
            }
            if (notifyCharacteristics.size() > 0 && notifyIndex < notifyCharacteristics.size()) {
                BleLog.d(TAG, "set characteristic notification, notify_index is "+notifyIndex);
                setCharacteristicNotification(gatt.getDevice().getAddress(), true);
            } else {
                BleLog.d(TAG, "set characteristic notification is completed");
                if (notifyWrapperCallback != null) {
                    if (Arrays.equals(descriptor.getValue(), BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
                            || Arrays.equals(descriptor.getValue(), BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)){
                        notifyWrapperCallback.onNotifySuccess(bleDevice);
                    }else if (Arrays.equals(descriptor.getValue(), BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)){
                        notifyWrapperCallback.onNotifyCanceled(bleDevice);
                    }

                }
            }
        }else {
            if (null != descWrapperCallback){
                descWrapperCallback.onDescWriteFailed(bleDevice, status);
            }
        }
    }
}
 
源代码8 项目: Android-BluetoothKit   文件: BleReadRssiRequest.java
@Override
public void onReadRemoteRssi(int rssi, int status) {
    stopRequestTiming();
    
    if (status == BluetoothGatt.GATT_SUCCESS) {
        putIntExtra(Constants.EXTRA_RSSI, rssi);
        onRequestCompleted(Code.REQUEST_SUCCESS);
    } else {
        onRequestCompleted(Code.REQUEST_FAILED);
    }
}
 
源代码9 项目: IoT-Firstep   文件: BluetoothLeService.java
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                 BluetoothGattCharacteristic characteristic,
                                 int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }
}
 
源代码10 项目: xDrip   文件: DexCollectionService.java
@Override
public synchronized void onDescriptorWrite(BluetoothGatt gatt, final BluetoothGattDescriptor descriptor,
                                           int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, "onDescriptorWrite: Wrote GATT Descriptor successfully.");
        descriptor_callback_failures = 0;
    } else {
        Log.d(TAG, "onDescriptorWrite: Error writing GATT Descriptor: " + status);
        if (descriptor_callback_failures == 0) {
            descriptor_callback_failures++;

            // TODO not sure if we want this retry here..
            try {
                if (!mBluetoothGatt.writeDescriptor(descriptor)) {
                    Log.d(TAG, "Failed to write descriptor in on descriptor write retry");
                    unBondBlucon();
                } else {
                    UserError.Log.d(TAG, "Tried to write descriptor again inside onDescriptorWrite");
                }
            } catch (Exception e) {
                UserError.Log.e(TAG, "Exception during callback retry: " + e);
            }

        } else {
            unBondBlucon();
        }
    }
    descriptor_time = 0;
}
 
源代码11 项目: Android-nRF-UART   文件: UartService.java
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
    	Log.w(TAG, "mBluetoothGatt = " + mBluetoothGatt );
    	
        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
    } else {
        Log.w(TAG, "onServicesDiscovered received: " + status);
    }
}
 
源代码12 项目: 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);
                    }
                }
            }
        }
    }
}
 
源代码13 项目: microbit   文件: DfuBaseService.java
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
    // Notify waiting thread
    logi("onServicesDiscovered() :: Start");
    if (status == BluetoothGatt.GATT_SUCCESS) {
        logi("onServicesDiscovered() :: Services discovered");
        mConnectionState = STATE_CONNECTED_AND_READY;
    } else {
        loge("onServicesDiscovered() :: Service discovery error: " + status);
        mError = ERROR_CONNECTION_MASK | status;
    }
    synchronized (mLock) {
        mLock.notifyAll();
    }
}
 
源代码14 项目: ESeal   文件: ACSUtilityService.java
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                 BluetoothGattCharacteristic characteristic,
                                 int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {

    }
}
 
源代码15 项目: IoT-Firstep   文件: BluetoothLeService.java
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
    } else {
        Log.w(TAG, "onServicesDiscovered received: " + status);
    }
}
 
@Override
public void onCharacteristicWrite(BluetoothGattCharacteristic characteristic, int status, byte[] value) {
    stopRequestTiming();

    if (status == BluetoothGatt.GATT_SUCCESS) {
        onRequestCompleted(Code.REQUEST_SUCCESS);
    } else {
        onRequestCompleted(Code.REQUEST_FAILED);
    }
}
 
源代码17 项目: IoT-Firstep   文件: BluetoothLeService.java
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
    } else {
        Log.w(TAG, "onServicesDiscovered received: " + status);
    }
}
 
@Override
public void onRxDataReceived(@NonNull byte[] data, @Nullable String identifier, int status) {
    if (status != BluetoothGatt.GATT_SUCCESS) {
        Log.w(TAG, "onRxDataReceived error:" + status);
        return;
    }

    UartPacket uartPacket = new UartPacket(identifier, UartPacket.TRANSFERMODE_RX, data);

    // Mqtt publish to RX
    if (mMqttManager != null) {
        if (MqttSettings.isPublishEnabled(mContext)) {
            final String topic = MqttSettings.getPublishTopic(mContext, MqttSettings.kPublishFeed_RX);
            if (topic != null) {
                final int qos = MqttSettings.getPublishQos(mContext, MqttSettings.kPublishFeed_RX);
                mMqttManager.publish(topic, uartPacket.getData(), qos);
            }
        }
    }

    try {
        mPacketsSemaphore.acquire();
    } catch (InterruptedException e) {
        Log.w(TAG, "InterruptedException: " + e.toString());
    }
    mReceivedBytes += data.length;
    if (mIsPacketCacheEnabled) {
        mPackets.add(uartPacket);
    }

    // Send data to delegate
    Listener listener = mWeakListener.get();
    if (listener != null) {
        mMainHandler.post(() -> listener.onUartPacket(uartPacket));
    }

    mPacketsSemaphore.release();
}
 
源代码19 项目: xDrip-plus   文件: ShareTest.java
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.i(TAG, "Services Discovered: " + status);
        authenticateConnection(gatt);

    }
}
 
源代码20 项目: Android-BLE-Library   文件: Request.java
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean isSuccess() {
	return this.status == BluetoothGatt.GATT_SUCCESS;
}