分享Android 蓝牙4.0(ble)开发的解决方案

最近,随着智能穿戴式设备、智能医疗以及智能家居的普及,蓝牙开发在移动开中显得非常的重要。由于公司需要,研究了一下,蓝牙4.0在Android中的应用。

以下是我的一些总结。

1.先介绍一下关于蓝牙4.0中的一些名词吧:   
(1)、GATT(Gneric Attibute  Profile)

通过ble连接,读写属性类小数据Profile通用的规范。现在所有的ble应用Profile  都是基于GATT
(2)、ATT(Attribute Protocal)
GATT是基于ATT Potocal的ATT针对BLE设备专门做的具体就是传输过程中使用尽量少的数据,每个属性都有个唯一的UUID,属性chartcteristics and Service的形式传输。

(3)、Service是Characteristic的集合。
(4)、Characteristic 特征类型。

比如,有个蓝牙ble的血压计。他可能包括多个Servvice,每个Service有包括多个Characteristic

注意:蓝牙ble只能支持Android 4.3以上的系统 SDK>=18

2.以下是开发的步骤:
2.1首先获取BluetoothManager

代码如下:

BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

2.2获取BluetoothAdapter

代码如下:

BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

2.3创建BluetoothAdapter.LeScanCallback

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { 

  @Override
  public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) { 

   runOnUiThread(new Runnable() {
    @Override
    public void run() {
     try {
      String struuid = NumberUtils.bytes2HexString(NumberUtils.reverseBytes(scanRecord)).replace("-", "").toLowerCase();
      if (device!=null && struuid.contains(DEVICE_UUID_PREFIX.toLowerCase())) {
       mBluetoothDevices.add(device);
      }
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   });
  }
 }; 

2.4.开始搜索设备。

代码如下:

mBluetoothAdapter.startLeScan(mLeScanCallback);

2.5.BluetoothDevice  描述了一个蓝牙设备 提供了getAddress()设备Mac地址,getName()设备的名称。
2.6开始连接设备

 /**
  * Connects to the GATT server hosted on the Bluetooth LE device.
  *
  * @param address
  *   The device address of the destination device.
  *
  * @return Return true if the connection is initiated successfully. The
  *   connection result is reported asynchronously through the
  *   {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
  *   callback.
  */
 public boolean connect(final String address) {
  if (mBluetoothAdapter == null || address == null) {
   Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
   return false;
  } 

  // Previously connected device. Try to reconnect. (先前连接的设备。 尝试重新连接)
  if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
   Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
   if (mBluetoothGatt.connect()) {
    mConnectionState = STATE_CONNECTING;
    return true;
   } else {
    return false;
   }
  } 

  final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
  if (device == null) {
   Log.w(TAG, "Device not found. Unable to connect.");
   return false;
  }
  // We want to directly connect to the device, so we are setting the
  // autoConnect
  // parameter to false.
  mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
  Log.d(TAG, "Trying to create a new connection.");
  mBluetoothDeviceAddress = address;
  mConnectionState = STATE_CONNECTING;
  return true;
 }

2.7连接到设备之后获取设备的服务(Service)和服务对应的Characteristic。

// Demonstrates how to iterate through the supported GATT
// Services/Characteristics.
// In this sample, we populate the data structure that is bound to the
// ExpandableListView
// on the UI.
private void displayGattServices(List<BluetoothGattService> gattServices) {
 if (gattServices == null)
  return;
 String uuid = null;
 ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<>();
 ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData = new ArrayList<>(); 

 mGattCharacteristics = new ArrayList<>(); 

 // Loops through available GATT Services.
 for (BluetoothGattService gattService : gattServices) {
  HashMap<String, String> currentServiceData = new HashMap<>();
  uuid = gattService.getUuid().toString();
  if (uuid.contains("ba11f08c-5f14-0b0d-1080")) {//服务的uuid
   //System.out.println("this gattService UUID is:" + gattService.getUuid().toString());
   currentServiceData.put(LIST_NAME, "Service_OX100");
   currentServiceData.put(LIST_UUID, uuid);
   gattServiceData.add(currentServiceData);
   ArrayList<HashMap<String, String>> gattCharacteristicGroupData = new ArrayList<>();
   List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
   ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<>(); 

   // Loops through available Characteristics.
   for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
    charas.add(gattCharacteristic);
    HashMap<String, String> currentCharaData = new HashMap<>();
    uuid = gattCharacteristic.getUuid().toString();
    if (uuid.toLowerCase().contains("cd01")) {
     currentCharaData.put(LIST_NAME, "cd01");
    } else if (uuid.toLowerCase().contains("cd02")) {
     currentCharaData.put(LIST_NAME, "cd02");
    } else if (uuid.toLowerCase().contains("cd03")) {
     currentCharaData.put(LIST_NAME, "cd03");
    } else if (uuid.toLowerCase().contains("cd04")) {
     currentCharaData.put(LIST_NAME, "cd04");
    } else {
     currentCharaData.put(LIST_NAME, "write");
    } 

    currentCharaData.put(LIST_UUID, uuid);
    gattCharacteristicGroupData.add(currentCharaData);
   } 

   mGattCharacteristics.add(charas); 

   gattCharacteristicData.add(gattCharacteristicGroupData); 

   mCharacteristicCD01 = gattService.getCharacteristic(UUID.fromString("0000cd01-0000-1000-8000-00805f9b34fb"));
   mCharacteristicCD02 = gattService.getCharacteristic(UUID.fromString("0000cd02-0000-1000-8000-00805f9b34fb"));
   mCharacteristicCD03 = gattService.getCharacteristic(UUID.fromString("0000cd03-0000-1000-8000-00805f9b34fb"));
   mCharacteristicCD04 = gattService.getCharacteristic(UUID.fromString("0000cd04-0000-1000-8000-00805f9b34fb"));
   mCharacteristicWrite = gattService.getCharacteristic(UUID.fromString("0000cd20-0000-1000-8000-00805f9b34fb")); 

   //System.out.println("=======================Set Notification==========================");
   // 开始顺序监听,第一个:CD01
   mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD01, true);
   mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD02, true);
   mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD03, true);
   mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD04, true);
  }
 }
}

2.8获取到特征之后,找到服务中可以向下位机写指令的特征,向该特征写入指令。

public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) { 

  if (mBluetoothAdapter == null || mBluetoothGatt == null) {
   Log.w(TAG, "BluetoothAdapter not initialized");
   return;
  } 

  mBluetoothGatt.writeCharacteristic(characteristic); 

 }

2.9写入成功之后,开始读取设备返回来的数据。

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
  @Override
  public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   String intentAction;
   //System.out.println("=======status:" + status);
   if (newState == BluetoothProfile.STATE_CONNECTED) {
    intentAction = ACTION_GATT_CONNECTED;
    mConnectionState = STATE_CONNECTED;
    broadcastUpdate(intentAction);
    Log.i(TAG, "Connected to GATT server.");
    // Attempts to discover services after successful connection.
    Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); 

   } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
    intentAction = ACTION_GATT_DISCONNECTED;
    mConnectionState = STATE_DISCONNECTED;
    Log.i(TAG, "Disconnected from GATT server.");
    broadcastUpdate(intentAction);
   }
  } 

  @Override
  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   if (status == BluetoothGatt.GATT_SUCCESS) {
    broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
   } else {
    Log.w(TAG, "onServicesDiscovered received: " + status);
   }
  }
  //从特征中读取数据
  @Override
  public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
   //System.out.println("onCharacteristicRead");
   if (status == BluetoothGatt.GATT_SUCCESS) {
    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
   }
  }
  //向特征中写入数据
  @Override
  public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
   //System.out.println("--------write success----- status:" + status);
  } 

  /*
   * when connected successfully will callback this method this method can
   * dealwith send password or data analyze 

   *当连接成功将回调该方法
   */
  @Override
  public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
   broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
   if (characteristic.getValue() != null) { 

    //System.out.println(characteristic.getStringValue(0));
   }
   //System.out.println("--------onCharacteristicChanged-----");
  } 

  @Override
  public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { 

   //System.out.println("onDescriptorWriteonDescriptorWrite = " + status + ", descriptor =" + descriptor.getUuid().toString()); 

   UUID uuid = descriptor.getCharacteristic().getUuid();
   if (uuid.equals(UUID.fromString("0000cd01-0000-1000-8000-00805f9b34fb"))) {
    broadcastUpdate(ACTION_CD01NOTIDIED);
   } else if (uuid.equals(UUID.fromString("0000cd02-0000-1000-8000-00805f9b34fb"))) {
    broadcastUpdate(ACTION_CD02NOTIDIED);
   } else if (uuid.equals(UUID.fromString("0000cd03-0000-1000-8000-00805f9b34fb"))) {
    broadcastUpdate(ACTION_CD03NOTIDIED);
   } else if (uuid.equals(UUID.fromString("0000cd04-0000-1000-8000-00805f9b34fb"))) {
    broadcastUpdate(ACTION_CD04NOTIDIED);
   }
  } 

  @Override
  public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
   //System.out.println("rssi = " + rssi);
  }
 }; 

 ----------------------------------------------
  //从特征中读取数据
  @Override
  public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
   //System.out.println("onCharacteristicRead");
   if (status == BluetoothGatt.GATT_SUCCESS) {
    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
   }
  }

2.10、断开连接

/**
  * Disconnects an existing connection or cancel a pending connection. The
  * disconnection result is reported asynchronously through the
  * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
  * callback.
  */
 public void disconnect() {
  if (mBluetoothAdapter == null || mBluetoothGatt == null) {
   Log.w(TAG, "BluetoothAdapter not initialized");
   return;
  }
  mBluetoothGatt.disconnect();
 }

2.11、数据的转换方法

// byte转十六进制字符串
 public static String bytes2HexString(byte[] bytes) {
  String ret = "";
  for (byte aByte : bytes) {
   String hex = Integer.toHexString(aByte & 0xFF);
   if (hex.length() == 1) {
    hex = '0' + hex;
   }
   ret += hex.toUpperCase(Locale.CHINA);
  }
  return ret;
 }
/**
  * 将16进制的字符串转换为字节数组
  *
  * @param message
  * @return 字节数组
  */
 public static byte[] getHexBytes(String message) {
  int len = message.length() / 2;
  char[] chars = message.toCharArray();
  String[] hexStr = new String[len];
  byte[] bytes = new byte[len];
  for (int i = 0, j = 0; j < len; i += 2, j++) {
   hexStr[j] = "" + chars[i] + chars[i + 1];
   bytes[j] = (byte) Integer.parseInt(hexStr[j], 16);
  }
  return bytes;
 } 

大概整体就是如上的步骤,但是也是要具体根据厂家的协议来实现通信的过程。

就拿一个我们项目中的demo说一下。
一个蓝牙ble的血压计。 上位机---手机  下位机 -- 血压计
1.血压计与手机连接蓝牙之后。
2.上位机主动向下位机发送一个身份验证指令,下位机收到指令后开始给上位做应答,
3.应答成功,下位机会将测量的血压数据传送到上位机。
4.最后断开连接。

希望本文对大家学习Android蓝牙技术有所帮助。

(0)

相关推荐

  • Android单片机与蓝牙模块通信实例代码

    啦啦毕业了,毕业前要写毕业设计,需要写一个简单的蓝牙APP进行交互,通过参考网上资料,问题顺利搞定,下面小编把具体实现思路分享给大家,供大家参考. 1.Android蓝牙编程 蓝牙3.0及以下版本编程需要使用UUID,UUID是通用唯一识别码(Universally Unique Identifier),这是一个软件构建的标准,也是被开源基金会组织应用在分布式计算环境领域的一部分.在蓝牙3.0及下一版本中,UUID被用于唯一标识一个服务,比如文件传输服务,串口服务.打印机服务等,如下: #蓝牙串

  • 客户端实现蓝牙接收(C#)知识总结

    在实现蓝牙接收时,网上的资料很多,使用起来也很简单,但是我觉得还是有必要把这些知识总结下来.蓝牙开发需要用到一个第三方的库InTheHand.Net.Personal.dll,其中关键的两个类是 BluetoothClient 和 BluetoothListener,首先开启一个子线程来不断的接收数据,使用很简单,直接上代码: 复制代码 代码如下: using InTheHand.Net.Sockets; using System.Threading; public MainWindow() {

  • Android手机通过蓝牙连接佳博打印机的实例代码

    所使用的打印机为佳博打印机,支持蓝牙.wifi.usb我所使用的是通过蓝牙来连接. 在网上找到一个佳博官方针对安卓开发的App源码,但是各种的跳转,没有看太懂,所以又去问度娘,找到了一个不错的文章 Android对于蓝牙开发从2.0版本的sdk才开始支持,而且模拟器不支持,测试至少需要两部手机,所以制约了很多技术人员的开发. 1. 首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限 // 管理蓝牙设备的权限 <uses-permissionandroid:name="

  • Android Bluetooth蓝牙技术使用流程详解

    在上篇文章给大家介绍了Android Bluetooth蓝牙技术初体验相关内容,感兴趣的朋友可以点击了解详情. 一:蓝牙设备之间的通信主要包括了四个步骤 设置蓝牙设备 寻找局域网内可能或者匹配的设备 连接设备 设备之间的数据传输 二:具体编程实现 1. 启动蓝牙功能 首先通过调用静态方法getDefaultAdapter()获取蓝牙适配器BluetoothAdapter,如果返回为空,则无法继续执行了.例如: BluetoothAdapter mBluetoothAdapter = Blueto

  • android实现蓝牙文件发送的实例代码,支持多种机型

    最近项目上需要实现蓝牙传输apk的一个功能,能够搜索周围的蓝牙手机并分享文件.从需求上讲android手机自带的蓝牙传输模块就可以满足需要了,实现也很简单.不过让人头疼的是,虽然说一般的主流机型都配置有蓝牙模块,但是android机型碎片化太严重,不同android版本手机蓝牙功能也不一样.4.0.3以下版本和以上版本使用的蓝牙包是不同的,分别是"com.android.bluetooth"和"com.mediatek.bluetooth".还有一些厂商对蓝牙模块进

  • Windows系统中使用C#编写蓝牙通信程序的简单实例

    现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用USB蓝牙方便的连接手机等蓝牙设备进行通信. 操作蓝牙要使用类库InTheHand.Net.Personal 首先在项目中引用该类库: static void Main(string[] args) { BluetoothRadio bluetoothRadio = BluetoothRadio.PrimaryRadio; if (bluetoothRadio == null) { Console.WriteLine("没有找

  • Android系统中的蓝牙连接程序编写实例教程

    Bluetooth结构 1.JAVA层 frameworks/base/core/java/android/bluetooth/ 包含了bluetooth的JAVA类. 2.JNI层 frameworks/base/core/jni/android_bluetooth_开头的文件 定义了bluez通过JNI到上层的接口. frameworks/base/core/jni/android_server_bluetoothservice.cpp 调用硬件适配层的接口system/bluetooth/

  • Android编程之蓝牙测试实例

    本文实例讲述了Android编程之蓝牙测试.分享给大家供大家参考.具体分析如下: 一.软件平台: win7 + eclipse + sdk 二.设计思路: 配合倒计时定时器实现蓝牙打开,可见,扫描三个功能 三.源代码: main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re

  • 微信小程序--Ble蓝牙

    有一段时间没有.没有写关于小程序的文章了.3月28日,微信的api又一次新的更新.期待已久的蓝牙api更新.就开始撸一番. 源码地址 1.简述 蓝牙适配器接口是基础库版本 1.1.0 开始支持. iOS 微信客户端 6.5.6 版本开始支持,Android 客户端暂不支持 蓝牙总共增加了18个api接口. 2.Api分类 搜索类 连接类 通信类 3.API的具体使用 详细见官网: https://mp.weixin.qq.com/debug/wxadoc/dev/api/bluetooth.ht

  • Android蓝牙开发深入解析

    1. 使用蓝牙的响应权限 复制代码 代码如下: <uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 2. 配置本机蓝牙模块 在这里首先要了解对蓝牙操作一个核心类BluetoothAdapter 复制代码 代码如下: Bluetoot

随机推荐