-
Notifications
You must be signed in to change notification settings - Fork 6
同步数据(ID=0X51)
Harry edited this page Jan 20, 2018
·
2 revisions
现在时间已经同步了。 是时候获取手环上的数据了。 在获取手环数据之前,你应该知道手环上有什么类型的数据。
1.当前计步、卡路里、睡眠数据
手环当前的计步、卡路里、睡眠数据
2.手环单机测量心率、血氧、血压数据
点击手环的按钮,例如进入手环上面的心率测量界面,这个时候就开始测量心率了。测量完成后手环会震动,并且将数据储存在手环中。
3.整点测量数据(计步、卡路里、心率、血氧、血压、睡眠时间)
要想手环每到整点就记录一组数据(计步、卡路里、心率、血氧、血压、睡眠),必须先开启手环的整点测量功能
向手环发送下面的数据
byte[] bytes = new byte[7];
bytes[0] = (byte) 0xAB;
bytes[1] = (byte) 0;
bytes[2] = (byte) 4;
bytes[3] = (byte) 0xFF;
bytes[4] = (byte) 0x78;
bytes[5] = (byte) 0x80;
bytes[6] = (byte) control; // control=0 关闭整点测量 control=1 开启整点测量
如果手环已经开启了整点测量,并且用户戴上手环,手环就会每次到整点都会测量并记录下一组数据
同步数据的时候你要告诉手环你需要哪个时间点之后的数据,所以你要发一个时间给手环。这是同步数据最重要的一件事。如果你不发时间过去,你每次调用同步数据的指令,手环都要把它所有的数据给你,这样很浪费。
同步数据需要发的指令:
Calendar calendar = Calendar.getInstance();
//这个时间就是你需要传入的时间,用来告诉手环你需要这个时间点之后的数据,
//这个时间点之前的数据你不需要了,或许你已经储存在了你的数据库中了。
calendar.setTimeInMillis(timeInMillis);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
byte[] data = new byte[12];
data[0] = (byte) 0xAB;
data[1] = (byte) 0;
data[2] = (byte) 9;
data[3] = (byte) 0xff;
data[4] = (byte) 0x51; //ID
data[5] = (byte) 0x80;
// data[6] = (byte)0;
data[7] = (byte) ((year - 2000));
data[8] = (byte) month;
data[9] = (byte) day;
data[10] = (byte) hour;
data[11] = (byte) minute;
一旦你发出这个指令,手环接收到了之后,手环就会给你发送数据。接下来要做的是解析手环返回来的数据。
final byte[] txValue = intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA);
Log.i(TAG, "Received command" + DataHandlerUtils.bytesToHexStr(txValue));
List<Integer> datas = DataHandlerUtils.bytesToArrayList(txValue);
Log.i(TAG, datas.toString());
//1.当前计步、卡路里、睡眠数据
if (datas.get(0) == 0xAB && datas.get(4) == 0x51 && datas.get(5) == 0x08) {
Log.d(TAG,"steps calories and sleep data current");
//计步
int steps = (datas.get(6) << 16) + (datas.get(7) << 8) + datas.get(8);
float distance = (steps * 0.7f)/1000;//If the user does not tell you his stride, by default he walked 0.7m every step
//卡路里
int calories =(datas.get(9) << 16) + (datas.get(10) << 8) + datas.get(11);
//睡眠
long shallowSleep = (datas.get(12) * 60 + datas.get(13)) * 60 * 1000L;
long deepSleep = (datas.get(14) * 60 + datas.get(15)) * 60 * 1000L;
long sleepTime = shallowSleep+deepSleep;
int wake_times = datas.get(16)
}
//2.手环单机测量心率、血氧、血压数据
if ((datas.get(0) == 0xAB && datas.get(4) == 0x51)){
int year = datas.get(6) + 2000;
int month = datas.get(7);
int day = datas.get(8);
int hour = datas.get(9);
int min = datas.get(10);
String time= year+"-"+month+"-"+day+" "+hour+":"+min;
Log.i(TAG,"hourly_time "+time);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
long timeInMillis=0;
try {
Date date = sdf.parse(time);
timeInMillis=date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
if (datas.get(5) == 0x11){
Log.d(TAG,"the Heart rate data from band measure");
//心率
int hrValue = datas.get(11);
}else if (datas.get(5) == 0x12){
Log.d(TAG,"the Blood oxygen data from band measure");
//血氧
int bloodOxygen = datas.get(11);
}else if (datas.get(5) == 0x14){
Log.d(TAG,"the Blood pressure data from band measure");
//血压数据
int bloodPressureHigh= datas.get(11);
int bloodPressureLow = datas.get(12);
}
}
//3.整点测量数据(计步、卡路里、心率、血氧、血压、睡眠时间)
if (datas.get(0) == 0xAB && datas.get(4) == 0x51 && datas.get(5) == 0x20){//Hourly
Log.d(TAG,"the steps, calories, heart rate, blood oxygen,blood pressure data from hourly measure");
int year = datas.get(6) + 2000;
int month = datas.get(7);
int day = datas.get(8);
//注意:收到整点数据,我们要加一个小时
int hour = datas.get(9)+1;
String time= year+"-"+month+"-"+day+" "+hour;
Log.i(TAG,"hourly_time "+time);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");
long timeInMillis=0;
try {
Date date = sdf.parse(time);
timeInMillis=date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
SPUtils.putLong(mContext, SPUtils.HOURLY_MEASURETIME, timeInMillis);
//计步
int steps = (datas.get(10) << 16) + (datas.get(11) << 8) + datas.get(12);
//卡路里
int calories = (datas.get(13) << 16) + (datas.get(14) << 8) + datas.get(15);
float distance = (steps * 0.7f)/1000;//If the user does not tell you his stride, by default he walked 0.7m every step
BigDecimal bd = new BigDecimal((double) distance);
BigDecimal bigDecimal = bd.setScale(2, RoundingMode.HALF_UP);
float distance2 = bigDecimal.floatValue();
//心率
int heartRate = datas.get(16);
//血氧
int bloodOxygen = datas.get(17);
//血压
int bloodPressure_high = datas.get(18);
int bloodPressure_low = datas.get(19);
}
if (datas.get(0) == 0){
Log.d(TAG,"second packet data from hourly measure");
long timeInMillis=SPUtils.getLong(mContext,SPUtils.HOURLY_MEASURETIME,0);
Log.i(TAG,"second packet---"+MyUtils.formatTime(timeInMillis,"yyyy-MM-dd HH:mm:ss"));
//睡眠时间
long shwllow_time = datas.get(1) * 60 * 60 * 1000L + datas.get(2) * 60 * 1000L;
long deep_time = datas.get(3) * 60 * 60 * 1000L + datas.get(4) * 60 * 1000L;
long total_time = shwllow_time + deep_time;
int wake_times = datas.get(5);
}