Java Code Examples for android.bluetooth.BluetoothGattService#addCharacteristic()

The following examples show how to use android.bluetooth.BluetoothGattService#addCharacteristic() . 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: HealthThermometerServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 7 votes vote down vote up
public HealthThermometerServiceFragment() {
  mTemperatureMeasurementCharacteristic =
      new BluetoothGattCharacteristic(TEMPERATURE_MEASUREMENT_UUID,
          BluetoothGattCharacteristic.PROPERTY_INDICATE,
          /* No permissions */ 0);

  mTemperatureMeasurementCharacteristic.addDescriptor(
      Peripheral.getClientCharacteristicConfigurationDescriptor());

  mTemperatureMeasurementCharacteristic.addDescriptor(
      Peripheral.getCharacteristicUserDescriptionDescriptor(TEMPERATURE_MEASUREMENT_DESCRIPTION));

  mMeasurementIntervalCharacteristic =
      new BluetoothGattCharacteristic(
          MEASUREMENT_INTERVAL_UUID,
          (BluetoothGattCharacteristic.PROPERTY_READ |
              BluetoothGattCharacteristic.PROPERTY_WRITE |
              BluetoothGattCharacteristic.PROPERTY_INDICATE),
          (BluetoothGattCharacteristic.PERMISSION_READ |
              BluetoothGattCharacteristic.PERMISSION_WRITE));

  mMeasurementIntervalCCCDescriptor = Peripheral.getClientCharacteristicConfigurationDescriptor();
  mMeasurementIntervalCharacteristic.addDescriptor(mMeasurementIntervalCCCDescriptor);

  mMeasurementIntervalCharacteristic.addDescriptor(
      Peripheral.getCharacteristicUserDescriptionDescriptor(MEASUREMENT_INTERVAL_DESCRIPTION));

  mHealthThermometerService = new BluetoothGattService(HEALTH_THERMOMETER_SERVICE_UUID,
      BluetoothGattService.SERVICE_TYPE_PRIMARY);
  mHealthThermometerService.addCharacteristic(mTemperatureMeasurementCharacteristic);
  mHealthThermometerService.addCharacteristic(mMeasurementIntervalCharacteristic);
}
 
Example 2
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void setNotifyNotificationTest() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_NOTIFY,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);

    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.setNotify(characteristic, true);
    verify(gatt).setCharacteristicNotification(characteristic, true);
    assertEquals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE[0], descriptor.getValue()[0]);
    assertEquals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE[1], descriptor.getValue()[1]);
    verify(gatt).writeDescriptor(descriptor);

    callback.onDescriptorWrite(gatt, descriptor, 0);
    verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0);
}
 
Example 3
Source File: BatteryServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 6 votes vote down vote up
public BatteryServiceFragment() {
  mBatteryLevelCharacteristic =
      new BluetoothGattCharacteristic(BATTERY_LEVEL_UUID,
          BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
          BluetoothGattCharacteristic.PERMISSION_READ);

  mBatteryLevelCharacteristic.addDescriptor(
      Peripheral.getClientCharacteristicConfigurationDescriptor());

  mBatteryLevelCharacteristic.addDescriptor(
      Peripheral.getCharacteristicUserDescriptionDescriptor(BATTERY_LEVEL_DESCRIPTION));

  mBatteryService = new BluetoothGattService(BATTERY_SERVICE_UUID,
      BluetoothGattService.SERVICE_TYPE_PRIMARY);
  mBatteryService.addCharacteristic(mBatteryLevelCharacteristic);
}
 
Example 4
Source File: BLEServicePeripheral.java    From unity-bluetooth with MIT License 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void start(String uuidString) {
    mServiceUUID = UUID.fromString(uuidString);
    if (mBtAdvertiser == null) {
        return;
    }

    BluetoothGattService btGattService = new BluetoothGattService(mServiceUUID, BluetoothGattService.SERVICE_TYPE_PRIMARY);
    btGattService.addCharacteristic(mBtGattCharacteristic);
    BluetoothGattServerCallback btGattServerCallback = createGattServerCallback(mServiceUUID, UUID.fromString(CHARACTERISTIC_UUID));
    mBtGattServer = mBtManager.openGattServer(mActivity.getApplicationContext(), btGattServerCallback);
    mBtGattServer.addService(btGattService);

    mDataBuilder = new AdvertiseData.Builder();
    mDataBuilder.setIncludeTxPowerLevel(false);
    mDataBuilder.addServiceUuid(new ParcelUuid(mServiceUUID));

    mSettingsBuilder=new AdvertiseSettings.Builder();
    mSettingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
    mSettingsBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);

    mBleAdvertiser = mBtAdapter.getBluetoothLeAdvertiser();
    mBleAdvertiser.startAdvertising(mSettingsBuilder.build(), mDataBuilder.build(), mAdvertiseCallback);
}
 
Example 5
Source File: AbstractHOGPServer.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * Setup Battery Service
 *
 * @return the service
 */
private BluetoothGattService setUpBatteryService() {
    final BluetoothGattService service = new BluetoothGattService(SERVICE_BATTERY, BluetoothGattService.SERVICE_TYPE_PRIMARY);

    // Battery Level
    final BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(
            CHARACTERISTIC_BATTERY_LEVEL,
            BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ,
            BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);

    final BluetoothGattDescriptor clientCharacteristicConfigurationDescriptor = new BluetoothGattDescriptor(
            DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION,
            BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE);
    clientCharacteristicConfigurationDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    characteristic.addDescriptor(clientCharacteristicConfigurationDescriptor);

    service.addCharacteristic(characteristic);

    return service;
}
 
Example 6
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testCopyServiceFull() {
    BluetoothGattService service = new BluetoothGattService(UUID.randomUUID(), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    service.addService(new BluetoothGattService(UUID.randomUUID(), BluetoothGattService.SERVICE_TYPE_SECONDARY));
    service.addService(new BluetoothGattService(UUID.randomUUID(), BluetoothGattService.SERVICE_TYPE_SECONDARY));
    service.addService(service);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PERMISSION_READ, BluetoothGattCharacteristic.PROPERTY_NOTIFY);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.randomUUID(), BluetoothGattDescriptor.PERMISSION_WRITE);
    descriptor.setValue(new byte[]{0x01, 0x02, 0x04, 0x21});
    characteristic.addDescriptor(descriptor);
    service.addCharacteristic(characteristic);
    BluetoothGattServiceCopy copyOfservice = new GattUtils().copyService(service);
    Assert.assertNotNull(copyOfservice);
    Assert.assertEquals(service.getIncludedServices().size(), copyOfservice.getIncludedServices().size());
    Assert.assertEquals(service.getCharacteristics().size(), copyOfservice.getCharacteristics().size());
    Assert.assertTrue(Arrays.equals(descriptor.getValue(),
        copyOfservice.getCharacteristic(characteristic.getUuid()).getDescriptor(descriptor.getUuid()).getValue()));
}
 
Example 7
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void queueTestRetryCommand() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0);
    BluetoothGattCharacteristic characteristic2 = new BluetoothGattCharacteristic(UUID.fromString("00002A1E-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0);
    service.addCharacteristic(characteristic);
    service.addCharacteristic(characteristic2);
    when(gatt.readCharacteristic(characteristic)).thenReturn(true);
    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.readCharacteristic(characteristic);

    verify(gatt).readCharacteristic(characteristic);

    // Trigger bonding to start
    callback.onCharacteristicRead(gatt, characteristic, 5);

    Field field = BluetoothPeripheral.class.getDeclaredField("bondStateReceiver");
    field.setAccessible(true);
    BroadcastReceiver broadcastReceiver = (BroadcastReceiver) field.get(peripheral);

    Intent intent = mock(Intent.class);
    when(intent.getAction()).thenReturn(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    when(intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR)).thenReturn(BOND_BONDED);
    when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(device);

    broadcastReceiver.onReceive(context, intent);

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    verify(gatt, times(2)).readCharacteristic(characteristic);
}
 
Example 8
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void setNotifyDisableTest() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_NOTIFY,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);

    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.setNotify(characteristic, false);
    verify(gatt).setCharacteristicNotification(characteristic, false);
    assertEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE[0], descriptor.getValue()[0]);
    assertEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE[1], descriptor.getValue()[1]);
    verify(gatt).writeDescriptor(descriptor);

    callback.onDescriptorWrite(gatt, descriptor, 0);

    verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0);
}
 
Example 9
Source File: NodeTest.java    From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void enableDisableNotificationForFeature(){
    SparseArray <Class <? extends Feature> > temp = new SparseArray<>();
    temp.append(0x01,FakeFeature.class);
    try {
        Manager.addFeatureToNode((byte)0x00,temp);
    } catch (InvalidFeatureBitMaskException e) {
        Assert.fail("Impossible add the FakeFeature");
    }

    BluetoothDevice device = spy(Shadow.newInstanceOf(BluetoothDevice.class));
    BluetoothDeviceShadow shadowDevice = Shadow.extract(device);
    BluetoothGattService dataService = new BluetoothGattService(
            UUID.randomUUID(),
            BluetoothGattService.SERVICE_TYPE_PRIMARY);
    dataService.addCharacteristic(createReadNotifyChar(
            UUID.fromString("000000001-"+BLENodeDefines.FeatureCharacteristics.BASE_FEATURE_COMMON_UUID)
    ));
    shadowDevice.addService(dataService);
    Node n = createNode(device);
    n.connect(RuntimeEnvironment.application);

    TestUtil.execAllAsyncTask();

    Feature f = n.getFeature(FakeFeature.class);
    Assert.assertFalse(n.isEnableNotification(f));
    Assert.assertTrue(n.enableNotification(f));
    Assert.assertTrue(n.isEnableNotification(f));
    Assert.assertTrue(n.disableNotification(f));
    Assert.assertFalse(n.isEnableNotification(f));
}
 
Example 10
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 5 votes vote down vote up
@Test
public void readDescriptor() {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_INDICATE,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002903-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);

    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.readDescriptor(descriptor);

    verify(gatt).readDescriptor(descriptor);

    byte[] originalByteArray = new byte[]{0x01};
    descriptor.setValue(originalByteArray);
    callback.onDescriptorRead(gatt, descriptor, 0);

    ArgumentCaptor<BluetoothPeripheral> captorPeripheral = ArgumentCaptor.forClass(BluetoothPeripheral.class);
    ArgumentCaptor<byte[]> captorValue = ArgumentCaptor.forClass(byte[].class);
    ArgumentCaptor<BluetoothGattDescriptor> captorDescriptor = ArgumentCaptor.forClass(BluetoothGattDescriptor.class);
    ArgumentCaptor<Integer> captorStatus = ArgumentCaptor.forClass(Integer.class);
    verify(peripheralCallback).onDescriptorRead(captorPeripheral.capture(), captorValue.capture(), captorDescriptor.capture(), captorStatus.capture());

    byte[] value = captorValue.getValue();
    assertEquals(0x01, value[0]);
    assertNotEquals(value, originalByteArray);   // Check if the byte array has been copied
    assertEquals(peripheral, captorPeripheral.getValue());
    assertEquals(descriptor, captorDescriptor.getValue());
    assertEquals(GATT_SUCCESS, (int) captorStatus.getValue() );
}
 
Example 11
Source File: RxBleClientMock.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
/**
 * Add a {@link BluetoothGattService} to the device. Calling this method is not required.
 *
 * @param uuid            service UUID
 * @param characteristics characteristics that the service should report. Use {@link CharacteristicsBuilder} to create them.
 */
public DeviceBuilder addService(@NonNull UUID uuid, @NonNull List<BluetoothGattCharacteristic> characteristics) {
    BluetoothGattService bluetoothGattService = new BluetoothGattService(uuid, 0);
    for (BluetoothGattCharacteristic characteristic : characteristics) {
        bluetoothGattService.addCharacteristic(characteristic);
    }
    rxBleDeviceServices.getBluetoothGattServices().add(bluetoothGattService);
    return this;
}
 
Example 12
Source File: AddGattServerCharacteristicDescriptorTransactionTest.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Before
public void before() throws InterruptedException {
    mockContext = InstrumentationRegistry.getInstrumentation().getContext();
    services = new ArrayList<>();
    services.add(new ParcelUuid(UUID.fromString("adabfb00-6e7d-4601-bda2-bffaa68956ba")));
    CountDownLatch cd = new CountDownLatch(1);
    NoOpGattCallback cb = new NoOpGattCallback() {

        @Override
        public void onGattServerStarted(GattServerConnection serverConnection) {
            super.onGattServerStarted(serverConnection);
            service = new BluetoothGattService(services.get(0).getUuid(), BluetoothGattService.SERVICE_TYPE_PRIMARY);
            service.addCharacteristic(gattCharacteristic);
            serverConnection.getServer().addService(service);
            cd.countDown();
        }
    };
    FitbitGatt.getInstance().registerGattEventListener(cb);
    FitbitGatt.getInstance().startGattServer(mockContext);
    FitbitBluetoothDevice device = new FitbitBluetoothDevice(MOCK_ADDRESS, "Stupid");

    conn = new GattConnection(device, InstrumentationRegistry.getInstrumentation().getTargetContext().getMainLooper());
    conn.setMockMode(true);
    conn.setState(GattState.CONNECTED);
    // idempotent, can't put the same connection into the map more than once
    FitbitGatt.getInstance().putConnectionIntoDevices(device, conn);
    try {
        cd.await(1, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail("Timeout during test setup");
    }
}
 
Example 13
Source File: AddGattServerCharacteristicTransactionTest.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Before
public void before() {
    mockContext = InstrumentationRegistry.getInstrumentation().getContext();
    services = new ArrayList<>();
    services.add(new ParcelUuid(UUID.fromString("adabfb00-6e7d-4601-bda2-bffaa68956ba")));
    CountDownLatch cd = new CountDownLatch(1);
    service = new BluetoothGattService(services.get(0).getUuid(), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    service.addCharacteristic(gattCharacteristic);
    NoOpGattCallback cb = new NoOpGattCallback() {
        @Override
        public void onGattServerStarted(GattServerConnection serverConnection) {
            super.onGattServerStarted(serverConnection);
            serverConnection.getServer().addService(service);
            cd.countDown();
        }
    };
    FitbitGatt.getInstance().registerGattEventListener(cb);
    FitbitGatt.getInstance().startGattServer(mockContext);
    FitbitBluetoothDevice device = new FitbitBluetoothDevice(MOCK_ADDRESS, "Stupid");
    conn = new GattConnection(device, InstrumentationRegistry.getInstrumentation().getTargetContext().getMainLooper());
    conn.setMockMode(true);
    conn.setState(GattState.CONNECTED);
    // idempotent, can't put the same connection into the map more than once
    FitbitGatt.getInstance().putConnectionIntoDevices(device, conn);
    try {
        cd.await(1, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail("Timeout during test setup");
    }
}
 
Example 14
Source File: GattServerTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@BeforeClass
public static void before() {
    Timber.plant(new Timber.DebugTree());
    mockContext = InstrumentationRegistry.getInstrumentation().getContext();
    main = new BluetoothGattService(UUID.fromString("adabfb00-6e7d-4601-bda2-bffaa68956ba"), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic txChar = new BluetoothGattCharacteristic(UUID.fromString("ADABFB01-6E7D-4601-BDA2-BFFAA68956BA"),
        BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
        BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED);
    main.addCharacteristic(txChar);
    BluetoothGattCharacteristic rxChar = new BluetoothGattCharacteristic(UUID.fromString("ADABFB02-6E7D-4601-BDA2-BFFAA68956BA"),
        BluetoothGattCharacteristic.PROPERTY_INDICATE | BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    main.addCharacteristic(rxChar);
    liveData = new BluetoothGattService(UUID.fromString("558dfa00-4fa8-4105-9f02-4eaa93e62980"), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic ldChar = new BluetoothGattCharacteristic(UUID.fromString("558DFA01-4FA8-4105-9F02-4EAA93E62980"),
        BluetoothGattCharacteristic.PROPERTY_INDICATE | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    liveData.addCharacteristic(ldChar);
    dncs = new BluetoothGattService(UUID.fromString("16bcfd00-253f-c348-e831-0db3e334d580"), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic notifChar = new BluetoothGattCharacteristic(UUID.fromString("16BCFD02-253F-C348-E831-0DB3E334D580"),
        BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED | BluetoothGattCharacteristic.PERMISSION_WRITE);
    dncs.addCharacteristic(notifChar);
    cpChar = new BluetoothGattCharacteristic(UUID.fromString("16BCFD01-253F-C348-E831-0DB3E334D580"),
        BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
    dncs.addCharacteristic(cpChar);
    BluetoothGattCharacteristic flsChar = new BluetoothGattCharacteristic(UUID.fromString("16BCFD04-253F-C348-E831-0DB3E334D580"),
        BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
    dncs.addCharacteristic(flsChar);
}
 
Example 15
Source File: GattDatabase.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Builds the current {@link BluetoothGattService}, and returns the parent {@link GattDatabase}.
 */
public final GattDatabase build()
{
    m_service = new BluetoothGattService(m_serviceUuid, m_isPrimary ? BluetoothGattService.SERVICE_TYPE_PRIMARY : BluetoothGattService.SERVICE_TYPE_SECONDARY);
    for (BluetoothGattCharacteristic ch : m_characteristics)
    {
        m_service.addCharacteristic(ch);
    }
    m_database.addService(m_service);
    return m_database;
}
 
Example 16
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 5 votes vote down vote up
@Test
public void queueTestConsecutiveReadsWithError() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0);
    BluetoothGattCharacteristic characteristic2 = new BluetoothGattCharacteristic(UUID.fromString("00002A1E-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0);
    service.addCharacteristic(characteristic);
    service.addCharacteristic(characteristic2);
    byte[] byteArray = new byte[] {0x01, 0x02, 0x03};
    characteristic.setValue(byteArray);
    characteristic2.setValue(byteArray);
    when(gatt.readCharacteristic(characteristic)).thenReturn(true);

    peripheral.readCharacteristic(characteristic);
    peripheral.readCharacteristic(characteristic2);

    verify(gatt).readCharacteristic(characteristic);

    callback.onCharacteristicRead(gatt, characteristic, 128);

    verify(peripheralCallback, never()).onCharacteristicUpdate(peripheral, byteArray, characteristic,128);

    verify(gatt).readCharacteristic(characteristic2);

    callback.onCharacteristicRead(gatt, characteristic2, GATT_SUCCESS);

    verify(peripheralCallback).onCharacteristicUpdate(peripheral, byteArray ,characteristic2, 0);
}
 
Example 17
Source File: HeartRateServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 5 votes vote down vote up
public HeartRateServiceFragment() {
  mHeartRateMeasurementCharacteristic =
      new BluetoothGattCharacteristic(HEART_RATE_MEASUREMENT_UUID,
          BluetoothGattCharacteristic.PROPERTY_NOTIFY,
          /* No permissions */ 0);

  mHeartRateMeasurementCharacteristic.addDescriptor(
      Peripheral.getClientCharacteristicConfigurationDescriptor());

  mHeartRateMeasurementCharacteristic.addDescriptor(
      Peripheral.getCharacteristicUserDescriptionDescriptor(HEART_RATE_MEASUREMENT_DESCRIPTION));

  mBodySensorLocationCharacteristic =
      new BluetoothGattCharacteristic(BODY_SENSOR_LOCATION_UUID,
          BluetoothGattCharacteristic.PROPERTY_READ,
          BluetoothGattCharacteristic.PERMISSION_READ);

  mHeartRateControlPoint =
      new BluetoothGattCharacteristic(HEART_RATE_CONTROL_POINT_UUID,
          BluetoothGattCharacteristic.PROPERTY_WRITE,
          BluetoothGattCharacteristic.PERMISSION_WRITE);

  mHeartRateService = new BluetoothGattService(HEART_RATE_SERVICE_UUID,
      BluetoothGattService.SERVICE_TYPE_PRIMARY);
  mHeartRateService.addCharacteristic(mHeartRateMeasurementCharacteristic);
  mHeartRateService.addCharacteristic(mBodySensorLocationCharacteristic);
  mHeartRateService.addCharacteristic(mHeartRateControlPoint);
}
 
Example 18
Source File: GattDatabase.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Builds the current {@link BluetoothGattService}, and returns the parent {@link GattDatabase}.
 */
public final GattDatabase build()
{
    m_service = new BluetoothGattService(m_serviceUuid, m_isPrimary ? BluetoothGattService.SERVICE_TYPE_PRIMARY : BluetoothGattService.SERVICE_TYPE_SECONDARY);
    for (BluetoothGattCharacteristic ch : m_characteristics)
    {
        m_service.addCharacteristic(ch);
    }
    m_database.addService(m_service);
    return m_database;
}
 
Example 19
Source File: FatBeaconBroadcastService.java    From physical-web with Apache License 2.0 5 votes vote down vote up
private void initGattServer() {
  mGattServer = mBluetoothManager.openGattServer(this, mGattServerCallback);
  BluetoothGattService service = new BluetoothGattService(UUID.fromString(SERVICE_UUID),
      BluetoothGattService.SERVICE_TYPE_PRIMARY);
  BluetoothGattCharacteristic webpage = new BluetoothGattCharacteristic(
      CHARACTERISTIC_WEBPAGE_UUID, BluetoothGattCharacteristic.PROPERTY_READ,
      BluetoothGattCharacteristic.PERMISSION_READ);
  service.addCharacteristic(webpage);
  mGattServer.addService(service);
}
 
Example 20
Source File: NodeTest.java    From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void updateFeature(){
    SparseArray <Class <? extends Feature> > temp = new SparseArray<>();
    temp.append(0x01,FakeFeature.class);
    try {
        Manager.addFeatureToNode((byte)0x00,temp);
    } catch (InvalidFeatureBitMaskException e) {
        Assert.fail("Impossible add the FakeFeature");
    }

    BluetoothDevice device = spy(Shadow.newInstanceOf(BluetoothDevice.class));
    BluetoothDeviceShadow shadowDevice = Shadow.extract(device);
    BluetoothGattService dataService = new BluetoothGattService(
            UUID.randomUUID(),
            BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic dataChar = createReadNotifyChar(
            UUID.fromString("000000001-" + BLENodeDefines.FeatureCharacteristics.BASE_FEATURE_COMMON_UUID)
    );
    dataService.addCharacteristic(dataChar);
    shadowDevice.addService(dataService);
    Node n = createNode(device);
    n.connect(RuntimeEnvironment.application);

    TestUtil.execAllAsyncTask();

    BluetoothGatt gatt = shadowDevice.getGattConnection();
    Feature.FeatureListener emptyListener = mock(Feature.FeatureListener.class);
    FakeFeature f = n.getFeature(FakeFeature.class);
    Assert.assertTrue(f != null);
    f.addFeatureListener(emptyListener);
    Assert.assertTrue(n.enableNotification(f));

    TestUtil.execAllAsyncTask();
    f.execAllTask();

    verify(gatt).setCharacteristicNotification(dataChar, true);
    verify(emptyListener).onUpdate(eq(f), any(Feature.Sample.class));

    Assert.assertTrue(n.disableNotification(f));
    verify(gatt).setCharacteristicNotification(dataChar, false);
}