I am building an app for my company where there is a feature it should get a heart rate from a Bluetooth watch.
I am using this library where I can get the basic data like device name and battery level and all but I can't get the data that I want (Step count and Heart rate).
Is there any other way to make it work? I have been working on this for weeks.
These are services I got from nRF:
The code that I have written:
package com.example.myapplicationrx;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.widget.Button;
import com.polidea.rxandroidble2.RxBleClient;
import com.polidea.rxandroidble2.RxBleConnection;
import com.polidea.rxandroidble2.RxBleDevice;
import java.util.UUID;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
public class MainActivity extends AppCompatActivity {
Disposable connectionDisposable;
RxBleDevice device;
RxBleClient rxBleClient;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = findViewById(R.id.start);
Button read = findViewById(R.id.read);
Context context = getApplicationContext();
rxBleClient = RxBleClient.create(context);
String macAddress = "EE:00:52:C1:66:95";
device = rxBleClient.getBleDevice(macAddress);
start.setOnClickListener(v -> {
connectionDisposable = device.establishConnection(false)
.flatMapSingle(rxBleConnection -> rxBleConnection.readCharacteristic(convertFromInteger(0x2A37)))
.subscribe(
characteristicValue -> {
System.out.println(new String(characteristicValue));
},
throwable -> {
// Handle an error here.
System.out.println(throwable);
}
);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
connectionDisposable.dispose();
}
}, 10000);
});
}
public UUID convertFromInteger ( int i){
final long MSB = 0x0000000000001000L;
final long LSB = 0x800000805f9b34fbL;
long value = i & 0xFFFFFFFF;
return new UUID(MSB | (value << 32), LSB);
}
private Observable<RxBleConnection> prepareConnectionObservable () {
return device
.establishConnection(false);
}
private String byteArrayToHex ( byte[] a){
StringBuilder sb = new StringBuilder(a.length * 2);
for (byte b : a)
sb.append(String.format("%02x", b & 0xff));
return sb.toString();
}
}
errors when I read heart rate
I/System.out: com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException: Characteristic not found with UUID 00002a37-0000-1000-8000-00805f9b34fb