108 lines
3.5 KiB
Java
108 lines
3.5 KiB
Java
package com.example.nrftest;
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
import android.bluetooth.BluetoothGatt;
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
|
import android.bluetooth.BluetoothGattService;
|
|
import android.content.Context;
|
|
import android.util.Log;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.util.UUID;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import no.nordicsemi.android.ble.BleManager;
|
|
import no.nordicsemi.android.ble.callback.profile.ProfileDataCallback;
|
|
import no.nordicsemi.android.ble.data.Data;
|
|
|
|
public class DecaManager extends BleManager<DecaManagerCallbacks> {
|
|
private static final String TAG = "DecaManager";
|
|
|
|
/** Decawave Service UUID. */
|
|
public final static UUID LBS_UUID_SERVICE = UUID.fromString("680c21d9-c946-4c1f-9c11-baa1c21329e7");
|
|
/** */
|
|
public final static UUID LBS_UUID_LOCATION_DATA_CHAR = UUID.fromString("003bbdf2-c634-4b3d-ab56-7ec889b89a37");
|
|
|
|
|
|
|
|
private BluetoothGattCharacteristic _locationDataCharacteristic;
|
|
|
|
|
|
|
|
public DecaManager(@NonNull final Context context)
|
|
{
|
|
super(context);
|
|
}
|
|
|
|
|
|
@NonNull
|
|
@Override
|
|
protected BleManagerGattCallback getGattCallback() {
|
|
return mGattCallback;
|
|
}
|
|
|
|
|
|
/**
|
|
* BluetoothGatt callbacks object.
|
|
*/
|
|
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
|
@Override
|
|
protected void initialize() {
|
|
|
|
requestMtu(512).enqueue();
|
|
|
|
setNotificationCallback(_locationDataCharacteristic).with(mButtonCallback);
|
|
// readCharacteristic(mLedCharacteristic).with(mLedCallback).enqueue();
|
|
// readCharacteristic(mButtonCharacteristic).with(mButtonCallback).enqueue();
|
|
enableNotifications(_locationDataCharacteristic).enqueue();
|
|
}
|
|
|
|
@Override
|
|
public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
|
final BluetoothGattService service = gatt.getService(LBS_UUID_SERVICE);
|
|
if (service != null) {
|
|
_locationDataCharacteristic = service.getCharacteristic(LBS_UUID_LOCATION_DATA_CHAR);
|
|
}
|
|
|
|
// boolean writeRequest = false;
|
|
// if (mLedCharacteristic != null) {
|
|
// final int rxProperties = mLedCharacteristic.getProperties();
|
|
// writeRequest = (rxProperties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0;
|
|
// }
|
|
//
|
|
// mSupported = mButtonCharacteristic != null && mLedCharacteristic != null && writeRequest;
|
|
// return mSupported;
|
|
|
|
return _locationDataCharacteristic != null;
|
|
}
|
|
|
|
@Override
|
|
protected void onDeviceDisconnected() {
|
|
_locationDataCharacteristic = null;
|
|
}
|
|
};
|
|
|
|
|
|
|
|
private final ProfileDataCallback mButtonCallback = new ProfileDataCallback() {
|
|
@Override
|
|
public void onDataReceived(@NonNull BluetoothDevice device, @NonNull Data data) {
|
|
Log.d(TAG, "onDataReceived: length=" + data.size() + " data=" + data);
|
|
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
stream.write(data.getByte(i));
|
|
}
|
|
|
|
mCallbacks.onTagLocationData(stream.toByteArray());
|
|
}
|
|
|
|
@Override
|
|
public void onInvalidDataReceived(@NonNull final BluetoothDevice device,
|
|
@NonNull final Data data) {
|
|
Log.w(TAG, "Invalid data received: " + data);
|
|
}
|
|
};
|
|
}
|