已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
蓝牙4.0Ble 开发要点Android 4.3才开始支持BLE API,所以保证在蓝牙4.0在Android 4.3及其以上的系统使用首先要确定他所需要的权限,2个权限如下:BLE分为三部分Service、Characteristic、Descriptor,这三部分都由UUID作为唯一标示符。一个蓝牙4.0的终端可以包含多个Service,一个Service可以包含多个Characteristic,一个Characteristic包含一个Value和多个Descriptor,一个Descriptor包含一个Value。蓝牙使用第一步:判断手机是否支持蓝牙4.0 方法如下if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) / 满足条件,表示手机支持初始化 Bluetooth adapter, 通过蓝牙管理器得到一个参考蓝牙适配器(API必须在以上android4.3或以上和版本)BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);mBluetoothAdapter = bluetoothManager.getAdapter();蓝牙使用第二步:搜索蓝牙设备/开始搜索mBluetoothAdapter.startLeScan(mLeScanCallback);/停止搜索mBluetoothAdapter.stopLeScan(mLeScanCallback);搜索蓝牙的回调方法:/ Device scan callback.private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() Overridepublic void onLeScan(final BluetoothDevice device, final int rssi, final byte scanRecord) runOnUiThread(new Runnable() Overridepublic void run() / 可以获取搜索到的蓝牙的一些信息 ,包括名称,mark地址等等name = device.getName();address = device.getAddress(););第三步:蓝牙连接mBluetoothGatt = device.connectGatt(this, false, mGattCallback);此方法会根据连接的结果给予回调方法;private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) String intentAction;System.out.println(=status: + status+,newState=+newState);/ 蓝牙连接成功if (newState = BluetoothProfile.STATE_CONNECTED) intentAction = ACTION_GATT_CONNECTED;mConnectionState = STATE_CONNECTED;broadcastUpdate(intentAction);Log.i(TAG, Connected to GATT server.);/ Attempts to discover services after successful connection.Log.i(TAG, Attempting to start service discovery:+ mBluetoothGatt.discoverServices();/ 蓝牙连接成功之后,必须调用此方法来发现蓝牙的服务mBluetoothGatt.discoverServices(); / 蓝牙连接失败else if (newState = BluetoothProfile.STATE_DISCONNECTED) intentAction = ACTION_GATT_DISCONNECTED;mConnectionState = STATE_DISCONNECTED;Log.i(TAG, Disconnected from GATT server.);broadcastUpdate(intentAction);/ 发现服务Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) if (status = BluetoothGatt.GATT_SUCCESS) / 发现服务 else / 读取蓝牙数据Overridepublic void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) System.out.println(onCharacteristicRead);if (status = BluetoothGatt.GATT_SUCCESS) broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);Overridepublic void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) System.out.println(onDescriptorWriteonDescriptorWrite = + status+ , descriptor = + descriptor.getUuid().toString();/ 服务状态发生变化Overridepublic void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);if (characteristic.getValue() != null) System.out.println(characteristic.getStringValue(0);System.out.println(-onCharacteristicChanged-);/ 获取蓝牙信号强度Overridepublic void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) System.out.println(rssi = + rssi);/ 写入数据public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) System.out.println(-write success- status: + status);System.out.println(-write success- WriteType: + characteristic.getWriteType();第四步:蓝牙服务读取 读取蓝牙服务,遍历服务,获取到自己需要的服务uuid遍历次服务下的特征值,判断读,写,通知属性,设置通知属性发送数据到蓝牙设备打开通知for (BluetoothGattService gattService : gattServices) uuid_1 = gattService.getUuid().toString();Log.e(info1,uuid_1=+uuid_1);List gattCharacteristics = gattService.getCharacteristics();Map charas = new HashMap();if(Config.uuid.equals(uuid_1)String uuid=null;/ Loops through available Characteristics.for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) uuid = gattCharacteristic.getUuid().toString();Log.e(info1,uuid=+uuid);if(Config.uuid_write.equals(uuid)final int prop = gattCharacteristic.getProperties();/ 具有可写属性 if (prop & BluetoothGattCharacteristic.PROPERTY_WRITE) 0) Log.e(info,uuid_write 可读);/ 具有通知属性 if (prop & BluetoothGattCharacteristic.PROPERTY_NOTIFY) 0) Log.e(info,uuid_write 通知属性);if (prop & BluetoothGattCharacteristic.PROPERTY_WRITE) 0) Log.e(info,uuid_write 可写);charas.put(uuid, gattCharacteristic);if(Config.uuid_read.equals(uuid)final int prop = gattCharacteristic.getProperties();/ 具有可写属性 if (prop & BluetoothGattCharacteristic.PROPERTY_WRITE) 0) Log.e(info,uuid_read 可写);/ 具有可读属性 if (prop & BluetoothGattCharacteristic.PROPERTY_READ) 0) Log.e(info,uuid_read 可读);/ 具有通知属性 if (prop & BluetoothGattCharacteristic.PROPERTY_NOTIFY) 0) Log.e(info,uuid_read 通知属性);MyApplication.getInstance().bindService.setCharacteristicNotification(gattCharacteristic, true);charas.put(uuid, gattCharacteristic);i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 30698-2026电子商务供应商评价准则优质制造商
- 2026届山东省德州市高三高考模拟历史试题(含答案)
- 山东省济宁市兖州区2025-2026学年高二下学期期中考试语文试卷
- 2026年法律基础知识竞赛试题及答案
- 2026年全国广播电视播音员主持人资格考试(广播电视播音主持业务)考前冲刺试题及答案
- 2026年云南省政府采购代理机构从业人员考试练习题及答案
- 施工电梯月度安全隐患排查维护管理规程
- 新形势下养老产品质检技术服务行业顺势崛起战略制定与实施分析研究报告
- 数字化社会工作企业数字化转型与智慧升级战略分析报告
- 2025-2030年艾灸智能温控仪企业制定与实施新质生产力战略分析研究报告
- 高考考务人员培训系统考试试题答案
- 2026上海市大数据中心招聘10名笔试参考题库及答案解析
- (二模)青岛市2026年高三年级第二次适应性检测语文试题(含答案)
- 国药集团2026届春季校园招聘笔试历年备考题库附带答案详解
- 天津师范大学与韩国世翰大学入学综合素质题目
- 计算机辅助设计AutoCAD绘图-课程教案
- Unit6Craftsmanship+Listening+an课件-中职高教版(2021)基础模块2
- 影视美学-课件
- 中文版-ASTM-A123-A123M-02-铁和钢制品镀锌层
- 血液透析室(中心)的人员配置及职责
- GB/T 12642-2013工业机器人性能规范及其试验方法
评论
0/150
提交评论