Java Code Examples for android.bluetooth.BluetoothGatt#GATT_SUCCESS

The following examples show how to use android.bluetooth.BluetoothGatt#GATT_SUCCESS . 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: BLEService.java    From android with MIT License 6 votes vote down vote up
@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("开始监听...");
                }
            }
        }
    }
}
 
Example 2
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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);
	}
}
 
Example 3
Source File: HeartRateServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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);
	}
}
 
Example 5
Source File: BleGattImpl.java    From EasyBle with Apache License 2.0 6 votes vote down vote up
@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);
                }
            }
        });
    }
}
 
Example 6
Source File: ConnectionImpl.java    From easyble-x with Apache License 2.0 6 votes vote down vote up
@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();
        }
    }
}
 
Example 7
Source File: ShareTest.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.i(TAG, "Services Discovered: " + status);
        authenticateConnection(gatt);

    }
}
 
Example 8
Source File: UartPacketManagerBase.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
@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();
}
 
Example 9
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
@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);
    }
}
 
Example 10
Source File: BleWriteNoRspRequest.java    From Android-BluetoothKit with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 11
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
@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);
    }
}
 
Example 12
Source File: ACSUtilityService.java    From ESeal with Apache License 2.0 5 votes vote down vote up
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                 BluetoothGattCharacteristic characteristic,
                                 int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {

    }
}
 
Example 13
Source File: DfuBaseService.java    From microbit with Apache License 2.0 5 votes vote down vote up
@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();
    }
}
 
Example 14
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 15
Source File: UartService.java    From Android-nRF-UART with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
    }
}
 
Example 16
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 17
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                 BluetoothGattCharacteristic characteristic,
                                 int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }
}
 
Example 18
Source File: BleReadRssiRequest.java    From Android-BluetoothKit with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 19
Source File: BleRequestImpl.java    From Android-BLE with Apache License 2.0 5 votes vote down vote up
@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);
            }
        }
    }
}
 
Example 20
Source File: Request.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean isSuccess() {
	return this.status == BluetoothGatt.GATT_SUCCESS;
}