在建工程抵押如何实现在Android 上面实现GATT Server

蓝牙(10)
http://blog.csdn.net/wave_1102/article/details/
Android4.3 规范了BLE的API,但是直到目前的4.4,还有些功能不完善。
在BLE协议中,有两个角色,周边(Periphery)和中央(Central);周边是数据提供者,中央是数据使用/处理者;在&SDK里面,可以把一个iOS设备作为一个周边,也可以作为一个中央;但是在&SDK里面,直到目前最新的Android4.4.2,Android手机只能作为中央来使用和处理数据;那数据从哪儿来?从BLE设备来,现在的很多可穿戴设备都是用BLE来提供数据的。
一个中央可以同时连接多个周边,但是一个周边某一时刻只能连接一个中央。
大概了解了概念后,看看Android BLE SDK的四个关键类(class):
a)&BluetoothGattServer作为周边来提供数据;BluetoothGattServerCallback返回周边的状态。
b)&BluetoothGatt作为中央来使用和处理数据;BluetoothGattCallback返回中央的状态和周边提供的数据。
因为我们讨论的是Android的BLE SDK,下面所有的BluetoothGattServer代表周边,BluetoothGatt代表中央。
& & & & &&
一.创建一个周边(虽然目前周边API在Android手机上不工作,但还是看看)
&a)先看看周边用到的class,蓝色椭圆
每一个周边BluetoothGattServer,包含多个服务Service,每一个Service包含多个特征Characteristic。
1.new一个特征:character = new BluetoothGattCharacteristic(
UUID.fromString(characteristicUUID),
BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ);
2.new一个服务:service = new BluetoothGattService(UUID.fromString(serviceUUID),
BluetoothGattService.SERVICE_TYPE_PRIMARY);
3.把特征添加到服务:service.addCharacteristic(character);
4.获取BluetoothManager:manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
5.获取/打开周边:BluetoothGattServer&server = manager.openGattServer(this,
new BluetoothGattServerCallback(){...});&
6.把service添加到周边:server.addService(service);
7.开始广播service:Google还没有广播Service的API,等吧!!!!!所以目前我们还不能让一个Android手机作为周边来提供数据。
二.创建一个中央(这次不会让你失望,可以成功创建并且连接到周边的)
a)先看看中央用到的class,蓝色椭圆
为了拿到中央BluetoothGatt,可要爬山涉水十八弯:
1.先拿到BluetoothManager:bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
2.再拿到BluetoothAdapt:btAdapter = bluetoothManager.getAdapter();
3.开始扫描:btAdapter.startLeScan(&BluetoothAdapter.LeScanCallback);
4.从LeScanCallback中得到BluetoothDevice:public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {.....}
5.用BluetoothDevice得到BluetoothGatt:gatt = device.connectGatt(this, true, gattCallback);
终于拿到中央BluetoothGatt了,它有一堆方法(查API吧),调用这些方法,你就可以通过BluetoothGattCallback和周边BluetoothGattServer交互了。
Demo工程下载地址:/detail/jimoduwu/7072515
From:&http://blog.csdn/jimoduwu/article/details/
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:119656次
积分:2780
积分:2780
排名:第12911名
原创:102篇
转载:480篇
(13)(30)(16)(41)(11)(46)(68)(35)(38)(26)(62)(31)(24)(37)(45)(59)(2)[发明专利]安卓设备间的数据传输、智能手表的消息获取方法及系统在审
申请/专利权人:
公开/公告号:CNA
发明/设计人:
公开/公告日:
主分类号:
搜索关键词:
【权利要求书】:
1.一种安卓设备间的数据传输方法,其特征在于,第一安卓Android设备上安装有第一应用,在所述第一应用中实现Android标准接口类NotificationListenerService,第二Android设备上安装有第二应用,在所述第二应用中实现蓝牙低功耗BLE标准接口类BluetoothGattServer,所述方法包括:所述第一应用中的NotificationListenerService获取第一Android设备接收到的数据;以及所述第一应用调用BLE标准接口类BluetoothGatt将获取到的数据提供给与所述第一Android设备实现蓝牙配对的第二Android设备;所述第二应用中的BluetoothGattServer接收所述第一Android设备提供的数据;以及所述第二应用调用BLE标准接口类BluetoothGattServerCallback获取BluetoothGattServer接收到的数据。
2.如权利要求1所述的方法,其特征在于,所述第一应用调用BluetoothGatt将获取到的数据提供给所述第二Android设备时,具体调用BluetoothGatt的writeCharacteristic(BluetoothGattCharacteristic)方法;所述第二应用调用BluetoothGattServerCallback获取BluetoothGattServer接收到的数据时,具体调用BluetoothGattServerCallback的onCharacteristicWriteRequest方法。
3.如权利要求1或2所述的方法,其特征在于,所述第一Android设备与第二Android设备间实现蓝牙配对的方法,具体包括:所述第一Android设备通过Android操作系统中的标准接口类BluetoothAdapter进行扫描,当扫描到已开启蓝牙服务的第二Android设备、以及接收到用户发起的配对指令后,与所述第二Android设备实现蓝牙配对。
4.如权利要求3所述的方法,其特征在于,所述第一Android设备通过BluetoothAdapter进行扫描时,具体使用BluetoothAdapter的startLeScan(LeScanCallback)方法。
5.一种安卓设备间的数据传输系统,其特征在于,包括第一安卓Android设备和第二Android设备,其中:所述第一安卓Android设备上安装有第一应用,在所述第一应用中实现Android标准接口类NotificationListenerService;所述第一Android设备,用于通过所述第一应用中的NotificationListenerService获取本Android设备接收到的数据;以及通过所述第一应用调用BLE标准接口类BluetoothGatt将获取到的数据提供给与本Android设备实现蓝牙配对的第二Android设备;所述第二Android设备上安装有第二应用,在所述第二应用中实现蓝牙低功耗BLE标准接口类BluetoothGattServer;所述第二Android设备,用于通过第二应用中的BluetoothGattServer接收所述第一Android设备提供的数据;以及通过所述第二应用调用BLE标准接口类BluetoothGattServerCallback获取BluetoothGattServer接收到的数据。
6.一种安卓设备,其特征在于,所述安卓Android设备上安装有第一应用,在所述第一应用中实现Android标准接口类NotificationListenerService,其中:所述第一应用,用于通过NotificationListenerService获取本Android设备接收到的数据;以及调用BLE标准接口类BluetoothGatt将获取到的数据提供给与本Android设备实现蓝牙配对的其他Android设备。
7.如权利要求6所述的设备,其特征在于,还包括:配对模块,用于通过Android操作系统中的标准接口类BluetoothAdapter进行扫描,当扫描到已开启蓝牙服务的其他Android设备、以及接收到用户发起的配对指令后,与所述其他Android设备实现蓝牙配对。
8.一种安卓设备,其特征在于,所述安卓Android设备上安装有第二应用模块,在所述第二应用模块中实现蓝牙低功耗BLE标准接口类BluetoothGattServer,其中:所述第二应用,用于通过BluetoothGattServer接收与本Android设备实现蓝牙配对的其他Android设备提供的数据;以及调用BLE标准接口类BluetoothGattServerCallback获取BluetoothGattServer接收到的数据。
9.一种智能手表的消息获取方法,其特征在于,安卓Android智能手机上安装有第一应用,在所述第一应用中实现Android标准接口类NotificationListenerService,Android智能手表上安装有第二应用,在所述第二应用中实现蓝牙低功耗BLE标准接口类BluetoothGattServer,所述方法包括:所述第一应用中的NotificationListenerService获取Android智能手机接收到的通知消息;以及所述第一应用调用BLE标准接口类BluetoothGatt将获取到的通知消息提供给与所述Android智能手机实现蓝牙配对的Android智能手表;所述第二应用中的BluetoothGattServer接收所述Android智能手机提供的数据;以及所述第二应用调用BLE标准接口类BluetoothGattServerCallback获取BluetoothGattServer接收到的数据。
10.一种智能手表的消息获取系统,其特征在于,包括安卓Android智能手机和Android智能手表,其中:所述Android智能手机上安装有第一应用,在所述第一应用中实现Android标准接口类NotificationListenerService;所述Android智能手机,用于通过所述第一应用中的NotificationListenerService获取本Android智能手机接收到的通知消息;以及通过所述第一应用调用BLE标准接口类BluetoothGatt将获取到的通知消息提供给与本Android智能手机实现蓝牙配对的Android智能手表;所述Android智能手表上安装有第二应用,在所述第二应用中实现蓝牙低功耗BLE标准接口类BluetoothGattServer;所述Android智能手表,用于通过第二应用中的BluetoothGattServer接收所述Android智能手机提供的通知消息;以及通过所述第二应用调用BLE标准接口类BluetoothGattServerCallback获取BluetoothGattServer接收到的数据。
友情链接:交换友情链接需要网站权重大于2,网站收录10W以上,如符合条件,请联系QQ:。
行业网站:相关推荐:
400-周一至周五 9:00-18:00
服务热线:400-投诉建议:022-
扫一扫,微信关注高智网
高智&让创新无法想象2000万件&专利数据Android的BLE技术中的GattAttributes是干什么的?最近在研究BLE技术发现了这个东西有一点不明白,有一个GattAttributes的文件/**Thisclassincludesa
Android的BLE技术中的GattAttributes是干什么的?
最近在研究BLE技术发现了这个东西有一点不明白,有一个GattAttributes的文件
This class includes a small subset of standard GATT attributes for demonstration purposes.
public class SampleGattAttributes {
private static HashMap attributes = new HashMap();
public static String HEART_RATE_MEASUREMENT = "0-805f9b34fb";
public static String CLIENT_CHARACTERISTIC_CONFIG = "0-805f9b34fb";
// Sample Services.
attributes.put("00-805f9b34fb", "Heart Rate Service");
attributes.put("00-805f9b34fb", "Device Information Service");
// Sample Characteristics.
attributes.put(HEART_RATE_MEASUREMENT, "Heart Rate Measurement");
attributes.put("0-805f9b34fb", "Manufacturer Name String");
public static String lookup(String uuid, String defaultName) {
String name = attributes.get(uuid);
return name == null ? defaultName :
翻译说是一种协议,请问用来干嘛的?
解决方案二:
解决方案三:
蓝牙的接口协议,是硬件给你的。其实我也不懂,之前研究了一下,这能这样理解了
【云栖快讯】数据库技术天团集体亮相,分享一线生产实践经验,告诉你踩过的坑、走过的路,都是老司机,靠谱!干货分享,不可错过!&&
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率
稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一
6款热门基础云产品6个月免费体验;2款产品1年体验;1款产品2年体验
开发者常用软件,超百款实用软件一站式提供Android BLE开发入门 - 简书
Android BLE开发入门
  BLE 即 Bluetooth Low Energy,蓝牙低功耗技术,是蓝牙4.0引入的新技术。现在越来越多的智能设备使用了BLE,像满大街的智能手环。  Android在4.3(API 18)中引进了对BLE central role的支持,同时提供API供App来扫描设备、查询服务、读写特征值等。
关键术语和概念
Generic Attribute Profile (GATT)—GATT概述(profile)是一个通用的通过BLE连接来发送和接受短的被称为“属性”的数据的规范。所有现在的低功耗应用概述(profile)都基于GATT。
蓝牙标准协会为低功耗设备定义了很多 。概述(profile)是设备在特定应用场景下如何工作的规范。注意设备能实现不止一种概述,例如,一个设备可以包含心律监测和电池电量检测。
Attribute Protocol (ATT)—GATT建立在属性协议(ATT)上。通常他们一起被叫做GATT/ATT。ATT针对在BLE设备上运行做了优化。为了这个目的,它使用尽可能少的字节。每个属性通过一个标准的128位格式的字符串ID作为唯一标识信息的通用唯一识别码(Universally Unique Identifier 即UUID)来唯一的标识。属性被ATT格式化为特征和服务来传输。
Characteristic—一个特征包含一个单一的值(value)和0-n个描述信息块(descriptor)来描述特征的值。特征可以被认为是一种类型(type),类似一个类(class)。
Descriptor—描述信息块定义了特征值(characteristic value)的属性,一个描述信息块可能指定一个可读的描述,一个特征值可接受的范围,或者指定一个特征值的计量单位。
Service—服务是特征的集合。例如,你可以有个叫做“心率检测器”且包含“心律测量方式”特征的服务。你可以在上找到一份现有的基于GATT的概述(profile)和服务列表。
角色和职责
中心 vs. 外围。你必须同时有这两种设备才能建立他们之间的连接。两个中心设备或者两个外围设备都不能建立连接。
GATT服务端 vs. GATT客户端。这取决于他们之间是怎么交流的。
&uses-permission android:name="android.permission.BLUETOOTH"/&
&!--下面的用来扫描设备和修改设置,用了这个必须同时用上面那个--&
&uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/&
检查设备是否支持BLE:
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
整个设备只有一个BluetoothAdapter
获取整个设备只有一个BluetoothAdapter,它代表了设备自己的蓝牙适配器,应用通过这个对象与设备进行交互。// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
开启蓝牙private BluetoothAdapter mBluetoothA
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
查找BLE设备
要查找BLE设备,需要使用方法,它的参数是。你必须实现这个callbak,因为这是扫描结果返回的方式。蓝牙扫描是一个电量敏感的操作,你需要遵守以下指导:
只要找到了要找的设备,停止扫描
不要在循环里扫描,设置一个扫描时间。之前的设备可能已经移动到范围外,持续扫描会耗尽电量。
如何开始和停止扫描:
* Activity for scanning and displaying available BLE devices.
public class DeviceScanActivity extends ListActivity {
private BluetoothAdapter mBluetoothA
private boolean mS
private Handler mH
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
public void run() {
mScanning =
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}, SCAN_PERIOD);
mScanning =
mBluetoothAdapter.startLeScan(mLeScanCallback);
mScanning =
mBluetoothAdapter.stopLeScan(mLeScanCallback);
如果只想扫描指定类型的外围设备,你可以调用,提供一个指定了你app支持的GATT服务的数组对象。
下面是一个用来传送BLE扫描结果的接口的实现:
private LeDeviceListAdapter mLeDeviceListA
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
注意:你只能扫描BLE设备或传统蓝牙设备二者之一,就像 描述的那样。你不能同时扫描两种设备。
连接GATT服务端
与BLE设备交互第一步就是要连接它—更确切的说,是连接到设备上的GATT服务端。要连接到BLE设备的GATT服务端,使用方法。这个方法需要三个参数,一个对象,autoConnect(一个指示是否自动连接到BLE设备--当它一旦可用的时候--的布尔值),和一个 的引用:
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
返回的实例之后你可以用它来管理GATT客户端的操作。调用者(Android app)是GATT客户端。
用于传递结果给用户,例如连接状态,以及任何进一步GATT客户端操作。
在这个例子中,这个BLE APP提供了一个activity(DeviceControlActivity)来连接,显示数据,显示该设备支持的GATT services和characteristics。根据用户的输入,这个activity与一个叫做BluetoothLeService的 通信,它通过Android BLE API实现与BLE设备交互:
// A service that interacts with the BLE device via the Android BLE API.
public class BluetoothLeService extends Service {
private final static String TAG = BluetoothLeService.class.getSimpleName();
private BluetoothManager mBluetoothM
private BluetoothAdapter mBluetoothA
private String mBluetoothDeviceA
private BluetoothGatt mBluetoothG
private int mConnectionState = STATE_DISCONNECTED;
private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2;
public final static String ACTION_GATT_CONNECTED =
"com.example.bluetooth.le.ACTION_GATT_CONNECTED";
public final static String ACTION_GATT_DISCONNECTED =
"com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
public final static String ACTION_GATT_SERVICES_DISCOVERED =
"com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_DATA_AVAILABLE =
"com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
public final static String EXTRA_DATA =
"com.example.bluetooth.le.EXTRA_DATA";
public final static UUID UUID_HEART_RATE_MEASUREMENT =
UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);
// Various callback methods defined by the BLE API.
private final BluetoothGattCallback mGattCallback =
new BluetoothGattCallback() {
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
String intentA
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
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);
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
Log.w(TAG, "onServicesDiscovered received: " + status);
// Result of a characteristic read operation
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
当一个特定的回调被触发的时候,它会调用相应的broadcastUpdate()辅助方法并且传递给它一个action。注意在该部分中的数据解析按照蓝牙心率测量配置文件规格进行。
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
// This is special handling for the Heart Rate Measurement profile. Data
// parsing is carried out as per profile specifications.
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
int flag = characteristic.getProperties();
int format = -1;
if ((flag & 0x01) != 0) {
format = BluetoothGattCharacteristic.FORMAT_UINT16;
Log.d(TAG, "Heart rate format UINT16.");
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Log.d(TAG, "Heart rate format UINT8.");
final int heartRate = characteristic.getIntValue(format, 1);
Log.d(TAG, String.format("Received heart rate: %d", heartRate));
intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length & 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
intent.putExtra(EXTRA_DATA, new String(data) + "\n" +
stringBuilder.toString());
sendBroadcast(intent);
返回到DeviceControlActivity, 这些事件由一个来处理:
// Handles various events fired by the Service.
// ACTION_GATT_CONNECTED: connected to a GATT server.
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
// ACTION_DATA_AVAILABLE: received data from the device. This can be a
// result of read or notification operations.
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected =
updateConnectionState(R.string.connected);
invalidateOptionsMenu();
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected =
updateConnectionState(R.string.disconnected);
invalidateOptionsMenu();
clearUI();
} else if (BluetoothLeService.
ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the
// user interface.
displayGattServices(mBluetoothLeService.getSupportedGattServices());
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
读取BLE属性
一旦你的Android app已经连接到GATT服务端连接且发现services后,就可以读、写那些支持的属性。例如,这段代码遍历服务端的services和 characteristics,并且将它们显示在UI上。
public class DeviceControlActivity extends Activity {
// 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)
String uuid =
String unknownServiceString = getResources().
getString(R.string.unknown_service);
String unknownCharaString = getResources().
getString(R.string.unknown_characteristic);
ArrayList&HashMap&String, String&& gattServiceData =
new ArrayList&HashMap&String, String&&();
ArrayList&ArrayList&HashMap&String, String&&& gattCharacteristicData
= new ArrayList&ArrayList&HashMap&String, String&&&();
mGattCharacteristics =
new ArrayList&ArrayList&BluetoothGattCharacteristic&&();
// Loops through available GATT Services.
for (BluetoothGattService gattService : gattServices) {
HashMap&String, String& currentServiceData =
new HashMap&String, String&();
uuid = gattService.getUuid().toString();
currentServiceData.put(
LIST_NAME, SampleGattAttributes.
lookup(uuid, unknownServiceString));
currentServiceData.put(LIST_UUID, uuid);
gattServiceData.add(currentServiceData);
ArrayList&HashMap&String, String&& gattCharacteristicGroupData =
new ArrayList&HashMap&String, String&&();
List&BluetoothGattCharacteristic& gattCharacteristics =
gattService.getCharacteristics();
ArrayList&BluetoothGattCharacteristic& charas =
new ArrayList&BluetoothGattCharacteristic&();
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic :
gattCharacteristics) {
charas.add(gattCharacteristic);
HashMap&String, String& currentCharaData =
new HashMap&String, String&();
uuid = gattCharacteristic.getUuid().toString();
currentCharaData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid,
unknownCharaString));
currentCharaData.put(LIST_UUID, uuid);
gattCharacteristicGroupData.add(currentCharaData);
mGattCharacteristics.add(charas);
gattCharacteristicData.add(gattCharacteristicGroupData);
接收GATT通知
常见的需求是当设备上的特性改变时通知BLE应用程序。这段代码展示了如何使用
给一个特性设置通知。
private BluetoothGatt mBluetoothG
BluetoothGattCharacter
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
一旦对一个特性启用了通知,当远程蓝牙设备特性发生变化时,回调函数就会被触发:
// Characteristic notification
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
关闭客户端App
一旦你的app完成了对BLE设备的使用,需要调用以便系统能适当地释放资源:
public void close() {
if (mBluetoothGatt == null) {
mBluetoothGatt.close();
mBluetoothGatt =
参考链接:
努力升级技能树

我要回帖

更多关于 如何实现在玻璃上输入 的文章

 

随机推荐