Java Code Examples for android.bluetooth.bluetoothgatt#GATT_FAILURE

The following examples show how to use android.bluetooth.bluetoothgatt#GATT_FAILURE . 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: BLETransport.java    From esp-idf-provisioning-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

    super.onConnectionStateChange(gatt, status, newState);
    Log.d(TAG, "onConnectionStateChange, New state : " + newState + ", Status : " + status);

    if (status == BluetoothGatt.GATT_FAILURE) {
        EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
        return;
    } else if (status == 133) {
        EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
        return;
    } else if (status != BluetoothGatt.GATT_SUCCESS) {
        // TODO need to check this status
        return;
    }
    if (newState == BluetoothProfile.STATE_CONNECTED) {
        Log.e(TAG, "Connected to GATT server.");
        gatt.discoverServices();
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        Log.e(TAG, "Disconnected from GATT server.");
        EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
    }
}
 
Example 2
Source File: RileyLinkBLE.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
private String getGattStatusMessage(final int status) {
    final String statusMessage;
    if (status == BluetoothGatt.GATT_SUCCESS) {
        statusMessage = "SUCCESS";
    } else if (status == BluetoothGatt.GATT_FAILURE) {
        statusMessage = "FAILED";
    } else if (status == BluetoothGatt.GATT_WRITE_NOT_PERMITTED) {
        statusMessage = "NOT PERMITTED";
    } else if (status == 133) {
        statusMessage = "Found the strange 133 bug";
    } else {
        statusMessage = "UNKNOWN (" + status + ")";
    }

    return statusMessage;
}
 
Example 3
Source File: BLeSerialPortService.java    From Android-BLE-Terminal with MIT License 6 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);
    // Notify connection failure if service discovery failed.
    if (status == BluetoothGatt.GATT_FAILURE) {
        connectFailure();
        return;
    }

    // Save reference to each UART characteristic.
    tx = gatt.getService(SERIAL_SERVICE_UUID).getCharacteristic(TX_CHAR_UUID);
    rx = gatt.getService(SERIAL_SERVICE_UUID).getCharacteristic(RX_CHAR_UUID);

    enableRXNotification();
    // Notify of connection completion.
    notifyOnConnected(context);
}
 
Example 4
Source File: CommandResultTest.java    From neatle with MIT License 5 votes vote down vote up
@Test
public void testGeneral() {
    CommandResult commandResult = new CommandResult(Neatle.createUUID(1), new byte[]{22, 21}, BluetoothGatt.GATT_SUCCESS, 1234556);

    assertEquals(BluetoothGatt.GATT_SUCCESS, commandResult.getStatus());
    assertEquals(1234556, commandResult.getTimestamp());
    assertEquals(Neatle.createUUID(1), commandResult.getUUID());
    assertTrue(commandResult.wasSuccessful());
    assertNotNull(commandResult.toString());

    CommandResult commandResult2 = new CommandResult(Neatle.createUUID(1), new byte[]{22, 21}, BluetoothGatt.GATT_FAILURE, 1234556);
    assertFalse(commandResult2.wasSuccessful());
}
 
Example 5
Source File: OperationResultsTest.java    From neatle with MIT License 5 votes vote down vote up
@Test
public void testSuccessful() {
    CommandResult result1 = new CommandResult(Neatle.createUUID(1), null, BluetoothGatt.GATT_SUCCESS, 0);
    CommandResult result2 = new CommandResult(Neatle.createUUID(2), null, BluetoothGatt.GATT_SUCCESS, 0);
    CommandResult result3 = new CommandResult(Neatle.createUUID(3), null, BluetoothGatt.GATT_FAILURE, 0);

    operationResults.addResult(result1);
    assertTrue(operationResults.wasSuccessful());
    operationResults.addResult(result2);
    assertTrue(operationResults.wasSuccessful());
    operationResults.addResult(result3);
    assertFalse(operationResults.wasSuccessful());
}
 
Example 6
Source File: BleUtils.java    From bleYan with GNU General Public License v2.0 5 votes vote down vote up
public static String getGattStatus(int status) {
    switch (status) {
        case BluetoothGatt.GATT_SUCCESS:
            return "GATT_SUCCESS";

        case BluetoothGatt.GATT_READ_NOT_PERMITTED:
            return "GATT_READ_NOT_PERMITTED";

        case BluetoothGatt.GATT_WRITE_NOT_PERMITTED:
            return "GATT_WRITE_NOT_PERMITTED";

        case BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION:
            return "GATT_INSUFFICIENT_AUTHENTICATION";

        case BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED:
            return "GATT_REQUEST_NOT_SUPPORTED";

        case BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION:
            return "GATT_INSUFFICIENT_ENCRYPTION";

        case BluetoothGatt.GATT_INVALID_OFFSET:
            return "GATT_INVALID_OFFSET";

        case BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH:
            return "GATT_INVALID_ATTRIBUTE_LENGTH";

        case BluetoothGatt.GATT_FAILURE:
            return "GATT_FAILURE";

        default:
            return "STATE_UNKNOWN: " + status;
    }
}
 
Example 7
Source File: HealthThermometerServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 5 votes vote down vote up
@Override
public int writeCharacteristic(BluetoothGattCharacteristic characteristic, int offset, byte[] value) {
  if (offset != 0) {
    return BluetoothGatt.GATT_INVALID_OFFSET;
  }
  // Measurement Interval is a 16bit characteristic
  if (value.length != 2) {
    return BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
  }
  ByteBuffer byteBuffer = ByteBuffer.wrap(value);
  byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
  final int newMeasurementIntervalValue = byteBuffer.getShort();
  if (!isValidMeasurementIntervalValue(newMeasurementIntervalValue)) {
    return BluetoothGatt.GATT_FAILURE;
  }
  getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
      mMeasurementIntervalCharacteristic.setValue(newMeasurementIntervalValue,
          MEASUREMENT_INTERVAL_FORMAT,
          /* offset */ 0);
      if (mMeasurementIntervalCCCDescriptor.getValue() == BluetoothGattDescriptor.ENABLE_INDICATION_VALUE) {
        resetTimer(newMeasurementIntervalValue);
        mTextViewNotifications.setText(R.string.notificationsEnabled);
      }
    }
  });
  return BluetoothGatt.GATT_SUCCESS;
}
 
Example 8
Source File: EddystoneGattService.java    From beacons-android with Apache License 2.0 4 votes vote down vote up
public void readCharacteristic(BluetoothGattServer gattServer, BluetoothDevice device,
                                   int requestId, int offset,
                                   BluetoothGattCharacteristic characteristic) {
//        UUID uuid = characteristic.getUuid();
        int status =  BluetoothGatt.GATT_SUCCESS;

        if (isLocked()) {
            if (characteristic == mUnlockCharacteristic) {
                log("Generating secure unlock challenge");
                characteristic.setValue(new byte[16]);
                new SecureRandom().nextBytes(characteristic.getValue());
            } else {
                if (characteristic != mLockStateCharacteristic) {
                    status = BluetoothGatt.GATT_READ_NOT_PERMITTED;
                }
            }
        }
        else if (characteristic == mUnlockCharacteristic) {
            status = BluetoothGatt.GATT_READ_NOT_PERMITTED;
        } else if (characteristic == mPublicEcdhKeyCharacteristic) {
            log("ECDH Public Key was requested");
            if (0 == offset) {
                characteristic.setValue(null == mEidKeyPair ? new byte[0] : mEidKeyPair.getPublicKey());
            }
        } else if (characteristic == mAdvSlotDataCharacteristic) {
            log("Advertisement slot data requested");
            characteristic.setValue(mConfigCallback.getAdvertisedData());
        } else if (characteristic  == mEidIdentityKeyCharacteristic) {
            log("Identity Key was requested");
            byte[] identityKey = mConfigCallback.getEidIdentityKey();
            if (null == identityKey) {
                status = BluetoothGatt.GATT_FAILURE;
            }
            else {
                characteristic.setValue(aes_transform(true, identityKey, 0, 16));
            }
        }

        gattServer.sendResponse(device, requestId, status, offset,
                status == BluetoothGatt.GATT_SUCCESS ? Arrays.copyOfRange(characteristic.getValue(), offset, characteristic.getValue().length) : null);
    }
 
Example 9
Source File: BluetoothLeUart.java    From Adafruit_Android_BLE_UART with MIT License 4 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);
    // Notify connection failure if service discovery failed.
    if (status == BluetoothGatt.GATT_FAILURE) {
        connectFailure();
        return;
    }

    // Save reference to each UART characteristic.
    tx = gatt.getService(UART_UUID).getCharacteristic(TX_UUID);
    rx = gatt.getService(UART_UUID).getCharacteristic(RX_UUID);

    // Save reference to each DIS characteristic.
    disManuf = gatt.getService(DIS_UUID).getCharacteristic(DIS_MANUF_UUID);
    disModel = gatt.getService(DIS_UUID).getCharacteristic(DIS_MODEL_UUID);
    disHWRev = gatt.getService(DIS_UUID).getCharacteristic(DIS_HWREV_UUID);
    disSWRev = gatt.getService(DIS_UUID).getCharacteristic(DIS_SWREV_UUID);

    // Add device information characteristics to the read queue
    // These need to be queued because we have to wait for the response to the first
    // read request before a second one can be processed (which makes you wonder why they
    // implemented this with async logic to begin with???)
    readQueue.offer(disManuf);
    readQueue.offer(disModel);
    readQueue.offer(disHWRev);
    readQueue.offer(disSWRev);

    // Request a dummy read to get the device information queue going
    gatt.readCharacteristic(disManuf);

    // Setup notifications on RX characteristic changes (i.e. data received).
    // First call setCharacteristicNotification to enable notification.
    if (!gatt.setCharacteristicNotification(rx, true)) {
        // Stop if the characteristic notification setup failed.
        connectFailure();
        return;
    }
    // Next update the RX characteristic's client descriptor to enable notifications.
    BluetoothGattDescriptor desc = rx.getDescriptor(CLIENT_UUID);
    if (desc == null) {
        // Stop if the RX characteristic has no client descriptor.
        connectFailure();
        return;
    }
    desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    if (!gatt.writeDescriptor(desc)) {
        // Stop if the client descriptor could not be written.
        connectFailure();
        return;
    }
    // Notify of connection completion.
    notifyOnConnected(this);
}