android中BluetoothLE的开发
来源:互联网 发布:奇葩说肖骁 知乎 编辑:程序博客网 时间:2024/06/05 18:36
一、博客的由来
由于公司的事情杂且多,我的博客也是杂(但还不多)。最近手头上的事情不算太多了,就着手写一个基于公司蓝牙协议的SDK,在写SDK的同时,晚上抽时间写写博客了,当作努力专研过蓝牙的记录吧。
二、本篇的内容
BluetoothLE(Bluetooth Low Energy低功耗蓝牙的简称)。开发的前提是android4.3+和bluetooth4.0。其实关于bluetooth4.0我也不太了解,当然了跟今天要说的没关系啦!(以后也会逐步完善)今天只是先讲蓝牙的连接和信息的传输。个人总结蓝牙的连接过程(下文中出现的系统是指android4.3+,设备是指bluetooth4.0)1、得到系统的蓝牙管理器2、得到系统蓝牙的适配器3、扫描蓝牙4、连接蓝牙5、扫描蓝牙的服务6、得到蓝牙服务的特征值7、注册特征值的监听,接收数据
- 列表内容
得到系统的蓝牙管理器
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
得到系统的蓝牙适配器
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
这里的蓝牙管理器和适配器都是系统蓝牙的属性或是代表。
扫描蓝牙
得到蓝牙适配器后就可以开始扫描蓝牙了。
mBluetoothAdapter.startLeScan(mLeScanCallback);
这里有一个扫描蓝牙的回调方法
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { //device代表搜索到的蓝牙设备 //rssi是蓝牙的信号 //scanRecord暂时不知道干啥用的,有知道的可以交流一下 @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { //方法内可以处理自己的业务逻辑,如将设备显示出来…… } };
蓝牙的连接
搜索到蓝牙后,我是将搜到的蓝牙全部放入一个listview中,点击列表获取相应的蓝牙address,由Device.getAddress()方法可以得到蓝牙的地址。下来来就是根据蓝牙的address去连接设备
//根据address得到远程的设备对象final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);//蓝牙连接,并得到一个蓝牙通信协议的通道 BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
BluetoothGatt
google官方文档关于BluetoothGatt的介绍是这样的
Public API for the Bluetooth GATT Profile.
This class provides Bluetooth GATT functionality to enable communication with Bluetooth Smart or Smart Ready devices.
To connect to a remote peripheral device, create a BluetoothGattCallback and call connectGatt(Context, boolean, BluetoothGattCallback) to get a instance of this class. GATT capable devices can be discovered using the Bluetooth device discovery or BLE scan process.
翻译出来大致是这样的: 这个类提供一个蓝牙GATT的功能使得蓝牙在通信时更加便捷的发送和接收数据。 BluetoothGatt 会在连接一个远程的设备时connectGatt(Context, boolean, BluetoothGattCallback) 得到实例。GATT 就是能让设备可以使用蓝牙设备发现或发现BLE扫描过程 GATT:是低功耗蓝牙的一个核心的协议 在之后的通信中,都是基于这个协议完成的。这里顺便介绍一下BluetoothGattCallback这个回调函数。
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { /** * 蓝牙连接状态改变的回调 * @param gatt * @param status * @param newState */ @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { } /** * 扫描蓝牙服务的回调 * @param gatt * @param status */ @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { } /** * 读特征值 * @param gatt * @param characteristic * @param status */ @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { } } /** * 特征值的改变 * @param gatt * @param characteristic */ @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { } };
到目前,蓝牙已经是连上了,回调方法中虽然什么都没有做,但是在后面的蓝牙通信中基本都是使用的这个回调方法。(未完待续)
0 0
- android中BluetoothLE的开发
- android中webservice的开发
- Android开发中常用的开发工具
- android开发中Android模拟器的使用方法
- 【Android开发】Android中LayoutInflater的使用
- android开发中数据库的使用
- Android开发中LogCat工具的使用
- android开发中倒计时功能的实现
- Android 开发中 LogCat 工具的使用
- android开发中代码的调试常识
- Android开发中插入新的Activity
- Android开发中遇到的奇怪问题
- android中基于蓝牙开发的demo
- Android开发中颜色的使用
- Android开发中IntentService的使用
- Android开发中遇到的问题
- Android开发中Dialog对话框的使用
- Android开发中Menu的理解
- 轻松理解java的File,InputStream,InputStreamReader,BufferedReader之间的关系
- 视频播放器原理
- Android性能优化典例
- const auto decltype关键字
- 【给将来学神的算法详解--高精】(2)高精的基础
- android中BluetoothLE的开发
- HDU:Fire Net
- 过度拟合与正规化线性回归
- Educational Codeforces Round 12E. Beautiful Subarrays
- Android15--Android之SQLite数据库的操作
- 安装VMware时遇到Invalid Driver H:\
- 轮播图的简单制作(前端开发)
- 51nod 1094
- Linux产生序列数字