andriod bluetooth gatt 详细讲解
来源:互联网 发布:萨伏伊别墅尺寸数据 编辑:程序博客网 时间:2024/05/18 20:52
在BLE协议中,有两个角色,周边(Periphery)和中央(Central);周边是数据提供者,中央是数据使用/处理者;在iOS SDK里面,可以把一个iOS设备作为一个周边,也可以作为一个中央
一个中央可以同时连接多个周边,但是一个周边某一时刻只能连接一个中央。
大概了解了概念后,看看Android BLE SDK的四个关键类(class):
a) BluetoothGattServer作为周边来提供数据;BluetoothGattServerCallback返回周边的状态。
b) BluetoothGatt作为中央来使用和处理数据;BluetoothGattCallback返回中央的状态和周边提供的数据。
因为我们讨论的是Android的BLE SDK,下面所有的BluetoothGattServer代表周边,BluetoothGatt代表中央。
因为我们讨论的是Android的BLE SDK,下面所有的BluetoothGattServer代表周边,BluetoothGatt代表中央。
一、创建一个周边(虽然目前周边API在Android手机上不工作,但还是看看)
a)先看看周边用到的class,蓝色椭圆
b)说明:
每一个周边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,蓝色椭圆
b)说明:
为了拿到中央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交互了。
三、吐槽:
BluetoothAdapter.LeScanCallback是接口,但是BluetoothGattServerCallback和BluetoothGattCallback是抽象类,这两个抽象类让人很不爽,不知道google为什么要把他们搞成抽象类,完全可以搞成接口的嘛,或者又有抽象类又有接口也行啊,就像Runable和Thread一样多好。这两个抽象类对于有代码洁癖的人简直就是一种折磨,在方法参数里面new,还要实现父类方法,是在受不了。
Demo工程下载地址:http://download.csdn.net/detail/jimoduwu/7072515
- andriod bluetooth gatt 详细讲解
- Bluetooth: ATT and GATT
- Bluetooth GATT介绍
- Bluetooth: ATT and GATT
- Bluetooth profile: ATT/GATT
- Bluetooth GATT介绍
- Bluetooth profile: ATT/GATT
- Android as Bluetooth Low Energy Peripheral (GATT server).
- Android as Bluetooth Low Energy Peripheral (GATT server).
- Bluetooth GATT(通用属性),ATT和Profile(配置文件)
- Bluetooth profile: ATT/GATT(襄坤在线)
- GATT
- GATT
- GATT
- Bluetooth 4.0之Android 讲解
- Bluetooth 4.0之Android 讲解
- Android Bluetooth蓝牙技术基础讲解
- Bluetooth 4.0之Android 讲解
- 用QTextBrowser实现的Web浏览器
- iOS软件开发NO.1
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- 面向问题,而非面向对象
- 现代控制理论课件资料与说明
- andriod bluetooth gatt 详细讲解
- 即时刷新父窗体数据(调用父窗体的方法)
- 快速排序递归与非递归实现
- 牛客网一站通Offer考试第一题
- 每天一篇python:执行shell命令
- 细读百度地图点聚合源码(上)
- 设计模式学习--命令模式
- 学习排序 Learning to Rank 小结
- 二叉树递归、非递归求深度