1.手机发送指令到BLE设备

手机向蓝牙设备发送指令,调用的是BluetoothGatt.writeCharacteristic(BluetoothGattCharacteristic characteristic)方法,本项目中由于涉及多设备管理,故由一个统一的工具类来发送指令。

在Activity中发送指令:

public static final UUID UUID_S_EXTRA = UUID.fromString("0000ff00-0000-1000-8000-00805f9b34fb");//扩展服务

public static final UUID UUID_S_EXTRA_C = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");//设备设置

public static final byte[] VALUE_FIND_LIGHT_ON = {(byte) 0x03, (byte) 0x01, (byte) 0x30}; // 指示灯打开

public static final byte[] VALUE_FIND_LIGHT_OFF = {(byte) 0x03, (byte) 0x00, (byte) 0x30}; // 指示灯关闭

byte[] value = BluetoothUtils.VALUE_FIND_LIGHT_ON;

buttonView.setChecked(!isChecked);

BluetoothUtils.sendValueToBle(mMac, BluetoothUtils.UUID_S_EXTRA, BluetoothUtils.UUID_S_EXTRA_C, value);

** 发送指令时,首先根据mac找出该设备对应的BluetoothGatt,然后发送对应的参数。*

2.判断指令发送是否成功

对于手机已经发送出去的指令,如何判断设备是否接收到并生效比较关键,这与很多业务相关联。这一点,在上篇文章中也提到,我们使用BluetoothGattCallback onCharacteristicWrite来解决,设备收到指令后,会进入该回调参数,我们可以获取被写入指令设备的MAC,写入的值,写入的状态,写入的通道等信息,据此来判断是否成功。

3.手机获取BLE设备的指令

手机与设备建立连接后,可订阅设备相应的通道(如电量、按键指令等),订阅后,方可接收到设备发送的指令信息,该信息从BluetoothGattCallback onCharacteristicRead获取,获取数据后的处理,可参与上篇文章。

4附录:指令发送相关的工具类BluetoothUtils.java

package com.powerstick.beaglepro.util;

import android.app.Activity;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothGatt;

import android.bluetooth.BluetoothGattCharacteristic;

import android.bluetooth.BluetoothGattService;

import android.bluetooth.BluetoothManager;

import android.content.Context;

import android.content.Intent;

import android.content.pm.PackageManager;

import com.afap.utils.ByteUtils;

import com.powerstick.beaglepro.MyApplication;

import com.powerstick.beaglepro.services.BluetoothLeService;

import com.tencent.bugly.crashreport.BuglyLog;

import java.util.UUID;

public final class BluetoothUtils {

public static final UUID UUID_S_IMMEDIATE = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb");//立即报警

public static final UUID UUID_S_IMMEDIATE_C_ALERT = UUID.fromString("00002a06-0000-1000-8000-00805f9b34fb");//报警级别

public static final UUID UUID_S_BATTERY = UUID.fromString("0000180f-0000-1000-8000-00805f9b34fb");//电池

public static final UUID UUID_S_BATTERY_C_LEVEL = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");//电池level

public static final UUID UUID_S_DEVICEINFO = UUID.fromString("0000180a-0000-1000-8000-00805f9b34fb");//设备信息

public static final UUID UUID_S_DEVICEINFO_C_FIRMWARE = UUID.fromString("00002a28-0000-1000-8000-00805f9b34fb");//固件

public static final UUID UUID_S_KEY = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb");//按键信息

public static final UUID UUID_S_KEY_C_PRESS = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");//按键

public static final UUID UUID_S_EXTRA = UUID.fromString("0000ff00-0000-1000-8000-00805f9b34fb");//扩展服务

public static final UUID UUID_S_EXTRA_C = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");//设备设置

public static final UUID UUID_S_EXTRA_C_LOGIN = UUID.fromString("0000ffc0-0000-1000-8000-00805f9b34fb");//认证

public static final UUID UUID_S_EXTRA_C_UNBIND = UUID.fromString("0000ffc1-0000-1000-8000-00805f9b34fb");//解绑

public static final UUID UUID_S_EXTRA_C_RENAME = UUID.fromString("0000fff3-0000-1000-8000-00805f9b34fb");//重命名

public static final byte[] VALUE_MODE_THETHER = {(byte) 0x01, (byte) 0x00, (byte) 0x10};

public static final byte[] VALUE_MODE_FIND = {(byte) 0x01, (byte) 0x01, (byte) 0x10};

public static final byte[] VALUE_IMMEDIATE_ON = {(byte) 2}; // 立即报警

public static final byte[] VALUE_IMMEDIATE_OFF = {(byte) 0}; // 取消立即报警

public static final byte[] VALUE_TETHER_BEEP_ON = {(byte) 0x02, (byte) 0x01, (byte) 0x20}; //警报打开

public static final byte[] VALUE_TETHER_BEEP_OFF = {(byte) 0x02, (byte) 0x00, (byte) 0x20}; //警报关闭

public static final byte[] VALUE_FIND_LIGHT_ON = {(byte) 0x03, (byte) 0x01, (byte) 0x30}; //指示灯打开

public static final byte[] VALUE_FIND_LIGHT_OFF = {(byte) 0x03, (byte) 0x00, (byte) 0x30}; //指示灯关闭

public static final byte[] VALUE_UNBIND = {(byte) 0x09, (byte) 0x01, (byte) 0x90};// 解绑指令

public final static int REQUEST_ENABLE_BT = 2001;

private final Context mActivity;

private final BluetoothAdapter mBluetoothAdapter;

public BluetoothUtils(final Context activity) {

mActivity = activity;

final BluetoothManager btManager = (BluetoothManager) mActivity.getSystemService(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = btManager.getAdapter();

}

public void askUserToEnableBluetoothIfNeeded() {

if (isBluetoothLeSupported() && (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())) {

final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

((Activity) mActivity).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

}

}

public BluetoothAdapter getBluetoothAdapter() {

return mBluetoothAdapter;

}

public boolean isBluetoothLeSupported() {

return mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);

}

public boolean isBluetoothOn() {

if (mBluetoothAdapter == null) {

return false;

} else {

return mBluetoothAdapter.isEnabled();

}

}

public static BluetoothLeService getBleService() {

return MyApplication.getInstance().mBluetoothLeService;

}

public static boolean sendValueToBle(String mac, UUID serviceId, UUID characteristicId, byte[] value) {

if (getBleService() == null) {

BuglyLog.e("BluetoothUtils", "蓝牙服务为null");

return false;

}

boolean result = false;

BluetoothGattService mService = getBleService().getService(mac, serviceId);

if (mService != null) {

BluetoothGattCharacteristic mCharacteristic = mService.getCharacteristic(characteristicId);

if (mCharacteristic != null) {

mCharacteristic.setValue(value);

BuglyLog.w("BluetoothUtils", ByteUtils.byteArrayToHexString(value));

result = getBleService().writeCharacteristic(mac, mCharacteristic);

} else {

BuglyLog.w("BluetoothUtils", "目标通道为NULL");

}

}

return result;

}

public static void readCharacteristic(String mac, UUID serviceId, UUID characteristicId) {

if (getBleService() == null) {

return;

}

BluetoothGattService mService = getBleService().getService(mac, serviceId);

if (mService != null) {

BluetoothGattCharacteristic mCharacteristic = mService.getCharacteristic(characteristicId);

if (mCharacteristic != null) {

BuglyLog.w("BluetoothUtils", "读取:" + characteristicId.toString());

getBleService().readCharacteristic(mac, mCharacteristic);

} else {

BuglyLog.w("BluetoothUtils", "目标通道为NULL");

}

}

}

public static void readRemoteRssi(String mac) {

if (getBleService() == null) {

return;

}

BluetoothGatt gatt = getBleService().getGatt(mac);

if (gatt != null) {

gatt.readRemoteRssi();

}

}

}

android编写蓝牙工具类,【Android BLE】蓝牙开发「防丢器」的相关知识点(三):手机与设备之间指令传输...相关推荐

  1. Android旋转视频工具类,Android开发实现的IntentUtil跳转多功能工具类【包含视频、音频、图片、摄像头等操作功能】...

    本文实例讲述了Android开发实现的IntentUtil跳转多功能工具类.分享给大家供大家参考,具体如下: 说明:此工具类是本人开发中总结下来的,还有其它的跳转亲给我留言,希望大家一起把这个工具类打 ...

  2. android语音播放工具类,Android开发之MediaPlayer多媒体(音频,视频)播放工具类

    本文实例讲述了Android开发之MediaPlayer多媒体(音频,视频)播放工具类.分享给大家供大家参考,具体如下: package com.android.imooc.chat; import ...

  3. android m权限工具类,android M权限适配,简单工具类

    很简单没什么说的, 因为项目大了,不能轻易引入第三方库,同时确实也没必要为了适配下权限就引入一个库. 这里写了个处理权限的工具类(部分借鉴的,勿喷),不嫌弃的话拿去! 用法: 1.申请权限: Perm ...

  4. android文件读取工具类,Android 下读取Assets Properties操作封装工具类

    Android 下读取Assets Properties操作封装工具类 发布时间:2018-06-03作者:laosun阅读(2081) 为了方便使用,首先创建BaseApplication类,如下所 ...

  5. android gps定位工具类,Android原生GPS和网络定位工具类

    在应用开发中我们常常需要网络获取位置的方法,这里做一个工具类的封装,使用的是Android原生定位,包含两种方式,一种是原生GPS一种是网络定位的封装实现: 工具类代码如下所示: package co ...

  6. android 软键盘工具类,Android开发之弹出软键盘工具类简单示例

    本文实例讲述了Android开发之弹出软键盘工具类.分享给大家供大家参考,具体如下: package com.maobang.imsdk.util; import android.content.Co ...

  7. android rsa加密工具类,android RSA加密

    释放双眼,带上耳机,听听看~! 这个RSA加密其实自己也没有完全弄清楚,只是在网上自己找了一些资料,也是为了记录自己的代码. 概述 RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事 ...

  8. Android生命周期工具类,Android倒计时工具类

    多谢touch_ping 的回应.  原来api有这个类  android.os.CountDownTimer , 具体实现很下面的差不多. import android.content.Contex ...

  9. android 软键盘工具类,Android软键盘管理工具类

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 一个软键盘管理类,单例模式实现的. /** * 类功能描述:软键盘管理界面 * */ public class Inpu ...

  10. android 图片缓存工具类,Android工具类系列-Glide图片缓存与圆角

    Glide的图片缓存和清除图片缓存 public class GlideCacheUtil { private static GlideCacheUtil inst; public static Gl ...

最新文章

  1. 1一10到时的英文单词_10张“牛皮癣”可换1个鸡蛋!徐州一街道全民总动员
  2. vundle按照YouComplete
  3. Asp.Net Core 发布到IIS
  4. SQL约束脚本的用法
  5. GCD API记录(二)
  6. tkintergui-pack布局内容1
  7. 【新星计划】怎么写好技术博客?
  8. 厘米换算英尺英寸(C语言程序设计)
  9. 将外国文献翻译成中文
  10. VUE解决背景图片没有铺满的问题
  11. 真牛皮!wsl安装位置
  12. mongodb异常Prematurely reached end of stream原因分析
  13. 基于PaddleX构建专属行车助手,为交通安全保驾护航
  14. Android----搜索历史(带区分切换用户id)
  15. Focal-UNet
  16. Java设计模式的一些积累
  17. POS58票据热敏打印机,怎么用ESC/POS命令控制打印
  18. Xposed框架动态调试第三方APP—实战演示
  19. 火山安卓RSA加解密操作
  20. Maxcompute 小记1

热门文章

  1. Linux执行fastqc报错Exception in thread “main“ java.awt.HeadlessException: No X11 DISPLAY variable was s
  2. 《别输在不会表达上》— 综合素质提升书籍
  3. Android ExpandableListView实现列表可扩展点击显示内容或隐藏内容
  4. SQL Sever — 导入数据与导出数据到表的方法
  5. 电子书下载:Beginning ASP.NET 2.0 and Databases
  6. 理解JavaScript中原型继承
  7. 网络对抗技术 实验二
  8. Oracle 11.2.0.4.0 Dataguard部署和日常维护(6)-Active Dataguard篇
  9. [置顶] MySQL -- 创建函数(Function
  10. [Postgres] Group and Aggregate Data in Postgres