Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#getService()

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#getService() . 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: BtBand.java    From sony-smartband-open-api with MIT License 6 votes vote down vote up
public void onCharacteristicRead( BluetoothGattCharacteristic characteristic ) {
    BluetoothGattService service = characteristic.getService();
    BaseProfile serviceProfile = Profiles.getService( service.getUuid() );

    Log.d( CLASS, "onCharacteristicRead: " + getCharacteristicInfo( characteristic ) );
    if( serviceProfile.getClass().equals( BatteryProfile.class ) ) {
        handleBatteryCallback( characteristic );
    } else if( serviceProfile.getClass().equals( AHServiceProfile.class ) ) {
        handleAHServiceCallback( characteristic );
    } else if( serviceProfile.getClass().equals( DeviceInformationProfile.class ) ) {
        handleDeviceInfoCallback( characteristic );
    } else if( serviceProfile.getClass().equals( AADeviceServiceProfile.class ) ) {
        handleAADeviceServiceCallback( characteristic );
    } else if( serviceProfile.getClass().equals( GenericAccessProfile.class ) ) {
        handleGenericAccessCallback( characteristic );
    } else {
        Log.w( CLASS, "onCharacteristicRead: unhandled event. class=" + serviceProfile.getClass() );
    }
    processQueue();
}
 
Example 2
Source File: BtBand.java    From sony-smartband-open-api with MIT License 6 votes vote down vote up
public void onCharacteristicChanged( BluetoothGattCharacteristic characteristic ) {
    BluetoothGattService service = characteristic.getService();
    BaseProfile serviceProfile = Profiles.getService( service.getUuid() );

    Log.d( CLASS, "onCharacteristicChanged: " + getCharacteristicInfo( characteristic ) );
    if( serviceProfile.getClass().equals( BatteryProfile.class ) ) {
        handleBatteryCallback( characteristic );
    } else if( serviceProfile.getClass().equals( AHServiceProfile.class ) ) {
        handleAHServiceCallback( characteristic );
    } else if( serviceProfile.getClass().equals( DeviceInformationProfile.class ) ) {
        handleDeviceInfoCallback( characteristic );
    } else if( serviceProfile.getClass().equals( AADeviceServiceProfile.class ) ) {
        handleAADeviceServiceCallback( characteristic );
    } else if( serviceProfile.getClass().equals( GenericAccessProfile.class ) ) {
        handleGenericAccessCallback( characteristic );
    } else {
        Log.w( CLASS, "onCharacteristicChanged: unhandled event. class=" + serviceProfile.getClass() );
    }
    processQueue();
}
 
Example 3
Source File: InfoActivity.java    From Bluefruit_LE_Connect_Android with MIT License 6 votes vote down vote up
@Override
public void onDataAvailable(BluetoothGattCharacteristic characteristic) {
    BluetoothGattService service = characteristic.getService();
    String key = new ElementPath(service.getUuid().toString(), service.getInstanceId(), characteristic.getUuid().toString(), null, null, null).getKey();

    byte[] data = characteristic.getValue();
    mValuesMap.put(key, data);

    // Update UI
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            updateUI();

        }
    });
}
 
Example 4
Source File: InfoActivity.java    From Bluefruit_LE_Connect_Android with MIT License 6 votes vote down vote up
@Override
public void onDataAvailable(BluetoothGattDescriptor descriptor) {
    BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
    BluetoothGattService service = characteristic.getService();
    final String key = new ElementPath(service.getUuid().toString(), service.getInstanceId(), characteristic.getUuid().toString(), descriptor.getUuid().toString(), null, null).getKey();
    final byte[] value = descriptor.getValue();
    mValuesMap.put(key, value);

    // Update UI
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            updateUI();
        }
    });
}
 
Example 5
Source File: GattServerCallback.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
private boolean checkCharacteristicAndCharacteristicService(GattServerConnection conn, BluetoothDevice device, @Nullable BluetoothGattCharacteristic characteristic, int requestId, int offset) {
    BluetoothGattServer server = conn.getServer();
    if (characteristic == null) {
        Timber.w("[%s] The characteristic wasn't attached to the descriptor! Returning error", device);
        returnErrorToRemoteClient(conn, device, requestId, offset);
        return true;
    }
    UUID charUuid = characteristic.getUuid();
    BluetoothGattService referencedService = characteristic.getService();
    if (referencedService == null) {
        Timber.w("[%s] The expected service wasn't attached to the characteristic! Returning error", device);
        returnErrorToRemoteClient(conn, device, requestId, offset);
        return true;
    }
    if (server.getService(referencedService.getUuid()) == null) {
        Timber.w("[%s] The expected service wasn't hosted! Returning error", device);
        returnErrorToRemoteClient(conn, device, requestId, offset);
        return true;
    }
    BluetoothGattService hostedService = server.getService(referencedService.getUuid());
    if (hostedService.getCharacteristic(charUuid) == null) {
        Timber.w("[%s] The expected characteristic wasn't hosted! Returning error", device);
        returnErrorToRemoteClient(conn, device, requestId, offset);
        return true;
    }
    return false;
}
 
Example 6
Source File: GattUtils.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * To prevent the characteristic from changing out from under us we need to copy it
 * <p>
 * This may happen under high throughput/concurrency
 *
 * @param characteristic The characteristic to be copied
 * @return The shallow-ish copy of the characteristic
 */
public @Nullable
BluetoothGattCharacteristicCopy copyCharacteristic(@Nullable BluetoothGattCharacteristic characteristic) {
    if (null == characteristic || null == characteristic.getUuid()) {
        return null;
    }
    BluetoothGattCharacteristicCopy newCharacteristic =
            new BluetoothGattCharacteristicCopy(characteristic.getUuid(),
                    characteristic.getProperties(), characteristic.getPermissions());
    if (characteristic.getValue() != null) {
        newCharacteristic.setValue(Arrays.copyOf(characteristic.getValue(), characteristic.getValue().length));
    }
    if (!characteristic.getDescriptors().isEmpty()) {
        for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
            BluetoothGattDescriptorCopy newDescriptor = new BluetoothGattDescriptorCopy(descriptor.getUuid(), descriptor.getPermissions());
            if (descriptor.getValue() != null) {
                newDescriptor.setValue(Arrays.copyOf(descriptor.getValue(), descriptor.getValue().length));
            }
            newCharacteristic.addDescriptor(newDescriptor);
        }
    }
    if (characteristic.getService() != null) {
        BluetoothGattServiceCopy newService = new BluetoothGattServiceCopy(characteristic.getService().getUuid(), characteristic.getService().getType());
        newService.addCharacteristic(newCharacteristic);
    }
    return newCharacteristic;
}
 
Example 7
Source File: BtBand.java    From sony-smartband-open-api with MIT License 5 votes vote down vote up
public void onDescriptorWrite( BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor ) {
    BluetoothGattService service = characteristic.getService();
    BaseProfile serviceProfile = Profiles.getService( service.getUuid() );

    Log.d( CLASS, "onDescriptorWrite: " + getDescriptorInfo( descriptor ) );

    UUID uuid = characteristic.getUuid();

    if( serviceProfile.getClass().equals( AHServiceProfile.class ) ) {
        if( uuid.equals( AHServiceProfile.DATA_UUID ) ) {
            mDataNotificationsEnabled = !mDataNotificationsEnabled;
        } else if( uuid.equals( AHServiceProfile.DEBUG_UUID ) ) {
            mDebugNotificationsEnabled = !mDebugNotificationsEnabled;
        } else if( uuid.equals( AHServiceProfile.EVENT_UUID ) ) {
            mEventNotificationsEnabled = !mEventNotificationsEnabled;
        } else if( uuid.equals( AHServiceProfile.ACCEL_DATA_UUID ) ) {
            mAccNotificationsEnabled = !mAccNotificationsEnabled ;
        } else {
            Log.w( CLASS, "onCharacteristicChanged: unhandled event. uuid=" + uuid
                    + " class=" + serviceProfile.getClass() );
        }
    } else {
        Log.w( CLASS, "onCharacteristicChanged: unhandled event. class=" + serviceProfile.getClass() );
    }

    processQueue();
}