无密钥情况下请
打开心率广播并且在手环开启一个运动的前提下,并且小米运动挂在后台下获取,以下是java代码
用了GITHUB上一个BLE通信库
implementation 'com.inuker.bluetooth:library:1.4.0'
public class Bluetoothheart{
private String MAC="";
public static final UUID SERIVER_UUID = UUID.fromString("0000180d-0000-1000-8000-00805F9B34FB");
public static final UUID CHARACTER_UUID = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb");
byte[] value;
public int statue=0;
BluetoothClient mClient ;
Context context;
public String heartrate;
TextView mtext;
public void setContext(Context context){
this.context=context;
}//传上下文
public void setMac(String mac) {
this.MAC = mac;
}//传MAC
public String getheartrate(){
return this.heartrate;
}//取得心率
public void setTextview(TextView textview){
this.mtext=textview;
}//传入要改的textview
public void startble(){
mtext.setVisibility(View.VISIBLE);
mClient = new BluetoothClient(context);
mClient.openBluetooth();
mClient.connect(MAC, new BleConnectResponse() {
@Override
public void onResponse(int code, BleGattProfile profile) {
if (code == REQUEST_SUCCESS) {
}
}
});
mClient.notify(MAC, SERIVER_UUID, CHARACTER_UUID, new BleNotifyResponse() {
@Override
public void onNotify(UUID service, UUID character, byte[] value) {
byte[] Temp=new byte[1];
Temp[0]=value[1];
heartrate=Arrays.toString(Temp);
heartrate=heartrate.substring(1, 3);
mtext.setText(heartrate);
System.out.println(Integer.valueOf(heartrate));
}
@Override
public void onResponse(int code) {
if (code == REQUEST_SUCCESS) {
}
}
});
}
}