基于蓝牙BLE的智能设备开发(一)

本文介绍了基于蓝牙BLE的智能设备开发,详细解析了BLE的Service、Characteristic和Descriptor,提供了相关UUID实例,并涉及数据转换工具类的实现,包括时间格式化、数据编码解码等方法。
摘要由CSDN通过智能技术生成
优点:
快速搜索,快速连接,超低功耗保持连接和传输数据


弱点:
数据传输速率低


背景:
Android 4.3才开始支持BLE API

穿戴设备发展


BLE的构成

Service
Characteristic(接收的 特性的 UUID)
Descriptor(数据到蓝牙的 特性 的UUID)
这三部分都由UUID作为唯一标示符。
public static String MYCJ_BLE = "0000ffe1-0000-1000-8000-00805f9b34fb";
public static String MYCJ_BLE_READ = "0000fff1-0000-1000-8000-00805f9b34fb";
public static String MYCJ_BLE_WRITE = "0000fff2-0000-1000-8000-00805f9b34fb";


一个蓝牙4.0的终端可以包含多个Service,一个Service可以包含多个Characteristic,一个Characteristic包含一个Value和多个Descriptor,一个Descriptor包含一个Value



计算卡路里的消耗:


根据 步数(步), 身高(厘米), 体重(千克), 时间(小时)算出消耗的能量KCal(千卡) kcal = 体重 * 时间 * 指数(k) k= 30 / 速度( N分钟 / 400米) = 30 / 多少分钟400米


根据用户传入的身高与步数, 返回用户步行的里程


根据传入的当前步数与目标步数,计算完成的百分比


天气预报

传入天气及温度的String, 返回协议的bytes[]


通过温度湿度的传感器,进行天气的预警

package com.example.mybletestdemo;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
/**
 * 数据转换工具类
 * @author Administrator
 *
 */
public class DataUtil {


private static StringBuffer sbTime;
private static String now, year, month, day, hour, min, ss;
private static SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMddHHmmssSSS");


private static String resultString = null;
private static String resultTrimData = null;


public static DecimalFormat df = new DecimalFormat("0.00");


public DataUtil() {


}


/**
* 时间同步 取得当前的时间, 并转为需要的格式: 16进制String并以F4开头
*/
public static String getTimeFormat() {
sbTime = new StringBuffer();
now = null;
now = sdf.format(new Date());
year = Integer
.toHexString(Integer.parseInt(now.substring(0, 4)) - 1900);
now.substring(4, 6);
month = Integer.toHexString(Integer.parseInt(now.substring(4, 6)));
if (month.length() == 1)
month = '0' + month;
day = Integer.toHexString(Integer.parseInt(now.substring(6, 8)));
if (day.length() == 1)
day = '0' + day;
hour = Integer.toHexString(Integer.parseInt(now.substring(8, 10)));
if (hour.length() == 1)
hour = '0' + hour;
min = Integer.toHexString(Integer.parseInt(now.substring(10, 12)));
if (min.length() == 1)
min = '0' + min;
ss = Integer.toHexString(Integer.parseInt(now.substring(12, 14)));
if (ss.length() == 1)
ss = '0' + ss;
sbTime.append("F4");
sbTime.append(year);
sbTime.append(month);
sbTime.append(day);
sbTime.append(hour);
sbTime.append(min);
sbTime.append(ss);
sbTime.append("00");
now = sbTime.toString().toUpperCase();
return now;
}


/**
* 取得String字符的前两位
*/
public static String getTag(String data) {
if (!data.isEmpty()) {
resultString = data.substring(0, 2);
}
return resultString;
}


/**
* 去除String字符中所有的空白符
*/
public static String getTrimData(String data) {
if (!data.isEmpty()) {
resultTrimData = data.replaceAll("\\s*", "");
}
return resultTrimData;
}


/**
* byte[]转变为16进制String字符, 每个字节2位, 不足补0
*/
public static String getStringByBytes(byte[] bytes) {
String result = null;
String hex = null;
if (bytes != null && bytes.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(bytes.length);
for (byte byteChar : bytes) {
hex = Integer.toHexString(byteChar & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
stringBuilder.append(hex.toUpperCase());
}
result = stringBuilder.toString();
}
return result;
}


/**
* 把16进制String字符转变为byte[]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那些特立独行的猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值