当前位置:
文档之家› Bluetooth程序设计(三)低功耗蓝牙
Bluetooth程序设计(三)低功耗蓝牙
BluetoothGattServer作为周边来提供数据; BluetoothGattServerCallback返回周边的状态。
BluetoothGatt作为中央来使用和处理数据;BluetoothGattCallback返 回中央的状态和周边提供的数据。
第8讲 Bluetooth程序设计(三)
//连接成功,开始搜索服务,一定要调用此方法,否则获取不到服务 } };
// 这里有9个要实现的方法,看情况要实现那些,用到那些就实现那些 }
7.执行BluetoothGatt.discoverServices()后,这个方法是异步操作,在回调函数 onServicesDiscovered中得到status,通过判断status是否等于 BluetoothGatt.GATT_SUCCESS来判断查找Service是否成功。
具体编程实现
1. 启动蓝牙功能
2. 查找设备
3. 查询匹配好的设备 4. 扫描设备
5. 使能被发现
6. 连接设备
7. 服务端的连接
8. 客户端的连接
9. 管理连接
10. 权限设置
创建BLE Central(Android手机目前功能)
创建BLE Central
1.先拿到BluetoothManager:bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
5.用BluetoothDevice得到BluetoothGatt gatt = device.connectGatt(this, true, gattCallback);
创建BLE Central
6. 在回调函数private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
// 在下面的回调方法中可以查询连接是否成功 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){
if (newState == BluetoothProfile.STATE_CONNECTED) { gatt.discoverServices();
手机短距离通信技术
第8讲 Bluetooth程序设计(三)
1、检查上次作业 2、检查预习题目 3、上次课程的总结 4、BLE的应用实现 5、应用示例 6、本次课的作业 7、下次课的预习内容
1、上次课的作业
编写一个蓝牙发现、扫描以及连接建立软件。
第8讲 Bluetooth程序设计(三)
1、检查上次作业 2、检查预习题目 3、上次课程的总结 4、BLE的应用实现 5、应用示例 6、本次课的作业 7、下次课的预习内容
4、BLE的应用实现
蓝牙设备之间的通信主要包括了四个步骤:
设置蓝牙设备
寻找局域网内可能或者匹配的设备
连接设备
设备之间的数据传输
};
BluetoothGattCallBack
1.notification对应onCharacteristicChanged; gatt.setCharacteristicNotification(characteristic, true); 2. readCharacteristic对应onCharacteristicRead; 3. writeCharacteristic对应onCharacteristicWrite; 4.连接蓝牙或者断开蓝牙对应onConnectionStateChange; 5. readDescriptor对应onDescriptorRead; 6. writeDescriptor对应onDescriptorWrite; 7. readRemoteRssi对应onReadRemoteRssi; 8. executeReliableWrite对应onReliableWriteCompleted; 9. discoverServices对应onServicesDiscovered。
characteristic。 setCharacteristicNotification(characteristic, enabled)
:设置当指定characteristic值变化时,发出通知。 getServices() :获取远程设备所支持的services。
BluetoothGattCallBack
Attribute Protocol (ATT)
GATT是基于ATT Protocol的。ATT每个属性都 有一个唯一的UUID,属性将以 characteristics 和services的形式传输。
Characteristic
为一个数据类型,它包括一个value和0至多个对 次value的描述(Descriptor)。
BluetoothAdapter类:代表了一个本地的蓝牙适配器。它是所有蓝牙交 互的入口点。利用它你可以发现其他蓝牙设备,查询绑定了的设备,使 用已知的MAC地址实例化一个蓝牙设备和建立一个 BluetoothServerSocket(作为服务器端)来监听来自其他设备的连接。
BluetoothDevice类:代表了一个远端的蓝牙设备,使用它请求远端蓝 牙设备连接或者获取远端蓝牙设备的名称、地址、种类和绑定状态(其 信息是封装在BluetoothSocket中)。
协议栈名词说明
1、profile 可以理解为一种规范,一个标准的通信 协议,它存在于从机中。蓝牙组织规定了一些标 准的profile
2、service 服务,在ble从机中,通过有多个服务 ,例如电量信息服务、系统信息服务等
3、characteristic 特征值,ble主从机的通信均是 通过characteristic来实现
BluetoothGattCallBack:用于已经连接上设备,对设备的某 些操作后返回的结果。
private BluetoothGattCallback gattCallback = new BluetoothGattCallback() { // 这里有9个要实现的方法,看情况要实现那些,用到那
Descriptor
对Characteristic描述,如范围、计量单位等。
Service
Characteristic集合。例如一个service叫做 “Heart Rate Monitor”,它可能包含多个 Characteristics,其中可能包含一个叫做 “heart rate measurement"的Characteristic 。
11. 然后通过BluetoothGatt.readCharacteristic(characteristic) 、 BluetoothGatt.writeCharacteristic(characteristic)、 BluetoothGatt.setCharacteristicNotification(characteristic, enabled ) 对特定characteristic进行读、写以及订阅。
些就实现那些
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){};
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){};
1、检查上次作业 2、检查预习题目 3、上次课程的总结 4、BLE的应用实现 5、应用示例 6、本次课的作业 7、下次课的预习内容
BLE概念
BLE指的是低功耗蓝牙(Bluetooth Low Energy )的缩写。 BLE是蓝牙4.0的一个子集, 关于蓝牙4.0描述如下:
创建BLE Central
4.从LeScanCallback中得到BluetoothDevice:public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
Android5.0之后: 从LeCallback中,通过onScanResult()把每次搜索到的设备添加到本地, 并获得BluetoothDevice。 leCallback = new ScanCallback() { onScanResult(int callbackType, ScanResult result) { … BluetoothDevice device = result.getDevice(); …}
创建BLE Central
12.以上方法对应的处理状态均在其回调方法 onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)、onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)、 onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)中处理。
创建BLE Central
8.如果成功了,则通过BluetoothGatt.getService来获取 BluetoothGattService
9.通过BluetoothGattService.getCharacteristic获取 BluetoothGattCharacteristic