月度归档:2015年11月

搞定Android Bluetooth Low Energy-01

在智能穿戴设备风起云涌的时代,搞定BLE还是可以谋得比较好的待遇的(虽然穿戴设备玩坏了众筹,智能穿戴也玩坏了硬件)。
国内的智能设备举例:土曼手表WeLoop小米智能设备WeCoachbonginwatchticwear

Bluetooth Low Energy简介

Android 4.3(API 18)才正式支持BLE,也称蓝牙4.0。Android提供了相应的API,可以扫描设备、连接设备,设备通讯。对比传统的蓝牙,BLE的设计能够显著减低功耗。手机完全可以一直开着蓝牙。

BLE协议栈
bluetooth-protocol-stack
Host:属于软件部分
Controller:属于硬件部分
PHY : 物理层 负责2.4 GHz信号传输
LL : 链路层 controls the RF state of the transceiver, determining whether it’s advertising, scanning, initiating, connected, or standing by.
HCI :负责Host和Controller之间的通信,类似硬件驱动层。
L2CAP:逻辑控制和适配层 负责数据包分段和重组,以及QoS等。
ATT : enables a device to reveal certain of its attributes to another device. The ATT block sets up peer-to-peer communication between an attribute server and a client to be able to exchange this information over a dedicated L2CAP channel.
SM : 安全管理(密码、加密啥的)
GATT:defines the sub-procedures for using ATT and specifies the structure of Bluetooth profiles. All communications between Bluetooth devices are handled through GATT sub-procedures.
GAP :provides the interface between the application and Bluetooth profiles and handles device discovery, connection, and services, including security procedures.
以上诸多翻译不能(老古董定的协议),目前App开发核心理解GAP、GATT和ATT。

Generic Access Profile(GAP)

GAP主要完成蓝牙设备发现、蓝牙设备连接、蓝牙服务发现,主要的两个角色:Central devices and Peripheral devices

  • Peripheral devices

Peripherals devices发送广播包,广播包可以包含一些有用的信息: 设备名称、设备mac地址(iOS不能获取蓝牙设备mac地址)等

  • Central devices

Central devices, 扫描和监听Peripherals devices的广播包如下图,扫描到目标设备后, Central device可以发起连接Peripheral device请求advertising_and_discovery
当peripheral and a central device连接成功后, 就可以使用GATT services 和 characteristics 通讯了.

Attribute Protocol(ATT)

The sole building block of ATT is the attribute. An attribute is composed by three elements:

  1. a 16-bit handle;
  2. an UUID which defines the attribute type;
  3. a value of a certaing length.
Handle UUID Value
0x0105 0x2902 0x0000

Most of the ATT protocol is pure client-server: client takes the initiative, server answers. But ATT has notification and indication capabilities, in which the server takes the initiative of notifying a client that an attribute value has changed, saving the client from having to poll the attribute.

Generic Attribute Profile(GATT)

GATT is a base profile for all top-level LE profiles. It defines how a bunch of ATT attributes are grouped together into meaningful services.

GATT两个角色

Client

The GATT client corresponds to the ATT client discussed in Attribute Protocol (ATT). It sends requests to a server and receives responses (and server-initiated updates) from it. The GATT client does not know anything in advance about the server’s attributes, so it must first inquire about the presence and nature of those attributes by performing service discovery. After completing service discovery, it can then start reading and writing attributes found in the server, as well as receiving server-initiated updates.

Server

The GATT server corresponds to the ATT server discussed in Attribute Protocol (ATT). It receives requests from a client and sends responses back. It also sends server-initiated updates when configured to do so, and it is the role responsible for storing and making the user data available to the client, organized in attributes. Every BLE device sold must include at least a basic GATT server that can respond to client requests, even if only to return an error response.
It is worth mentioning once more that GATT roles are both completely independent of GAP roles (see Roles) and also concurrently compatible with each other. That means that both a GAP central and a GAP peripheral can act as a GATT client or server, or even act as both at the same time.


GATT 结构图

Services

Service is collections of characteristics and relationships to other services that encapsulate the behavior of part of a device.

Characteristics

A characteristic is a value used in a service along with properties and configuration information about how the value is accessed and information about how the value is displayed or represented.

Descriptors

Characteristic descriptors are used to contain related information about the characteristic Value.

All the Information of services, characteristics and descriptors are combined together and made a Profile. Created profile data is then converted into HEX format file which is non readable by humans for security aspects. This File is used as Input for configure GATT Layer.

参考文献

使用gradle发布Android library

使用Android Studio时可以很方便的引入Library

dependencies {
    compile 'com.android.support:recyclerview-v7:23.1.1'
}

Gradle通过ArtifactId:GroupId:Version从MVN仓库中下载的。

这里介绍通过Sonatype发布library到Maven中央仓库

第一步:注册账号

注册地址:issues.sonatype

第二步:create a new issue

填写的内容参考下图:groupId需要符合规范:choosing-your-coordinates
jira_ticket
然后等待1-2个工作日,会回复,如果通过的话:

Configuration has been prepared, now you can:

please comment on this ticket when you promoted your first release, thanks

第三步:发布snapshots包

参考:gradle-mvn-push

VERSION_NAME=1.0.0-SNAPSHOT

$ gradle uploadArchives
第四步:生成GPG KEY

这个设置是为了release包可以同步到mvn仓库
参考:simple library publishing with gradle

$gpg2 --new-key
Enter your email (same as the account you registered with sonatype.org) and other details gpg prompts for. Once complete gpg will print the new key’s details.
$ gpg2 --list-secret-keys

/home/xxxx/.gnupg/secring.gpg
------------------------------
sec   1024D/C6EED57A 2015-01-13
uid                  Juven Xu (Juven Xu works at Sonatype)
ssb   2048g/D704745C 2010-01-13
The line starting with pub shows the length (1204D), the keyid (C6EED57A)
第五步:发布release包

每次发布新的release版本都需要下面操作(Close->Release)

VERSION_NAME=1.0.0

$ gradle uploadArchives

comment on this ticket when you promoted your first release
如果不能发布release版本,参照(先Close然后在Release一下):
I can see the staging repository named comgithubcaptain-miao-1002 here:
https://oss.sonatype.org/#stagingRepositories
You should be able to close that staging repository when logged in to oss.sonatype.org using the same credentials you use to access this JIRA.

第六步:用户名/密码设置

可以在本机的Gradle配置文件中,避免提交Github上。
如果~/.gradle/gradle.properties 文件不存在,就创建一个,配置内容:

signing.keyId=YourKeyId
signing.password=YourPublicKeyPassword
signing.secretKeyRingFile=PathToYourKeyRingFile

ossrhUsername=your-jira-id
ossrhPassword=your-jira-password

//补充@2015-11-28

JitPack一步发布你的Github库

JitPack:Easy to use package repository for GitHub
JitPack实际上是一个自定义的Maven仓库,只需要输入Github项目地址就可发布项目,参看官网JitPack