java安卓ble蓝牙连接订阅功能的实现以及使用的完整代码

时间: 2024-02-17 22:01:34 浏览: 23
好的,这是一个基于Java的Android BLE蓝牙连接订阅功能的示例代码: ``` public class MainActivity extends AppCompatActivity { private BluetoothManager bluetoothManager; private BluetoothAdapter bluetoothAdapter; private BluetoothGatt bluetoothGatt; private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if(newState == BluetoothProfile.STATE_CONNECTED) { Log.d("BLE", "Connected to GATT server."); gatt.discoverServices(); } else if(newState == BluetoothProfile.STATE_DISCONNECTED) { Log.d("BLE", "Disconnected from GATT server."); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if(status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService service = gatt.getService(UUID.fromString("service_uuid")); BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString("characteristic_uuid")); boolean result = gatt.setCharacteristicNotification(characteristic, true); if(result) { Log.d("BLE", "Characteristic notification set successfully."); } else { Log.d("BLE", "Characteristic notification set failed."); } BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("descriptor_uuid")); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); byte[] data = characteristic.getValue(); String message = new String(data); Log.d("BLE", "Received message: " + message); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice("device_address"); bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallback); } @Override protected void onDestroy() { super.onDestroy(); if(bluetoothGatt != null) { bluetoothGatt.disconnect(); bluetoothGatt.close(); } } } ``` 在这个示例代码中,我们首先获取了系统的BluetoothManager和BluetoothAdapter对象,然后通过BluetoothAdapter获取了要连接的远程设备的BluetoothDevice对象。接下来,我们通过BluetoothDevice对象调用connectGatt()方法建立与设备的BLE连接,并传入一个BluetoothGattCallback对象作为回调函数。 在BluetoothGattCallback中,我们实现了三个回调函数:onConnectionStateChange()、onServicesDiscovered()和onCharacteristicChanged()。其中,onConnectionStateChange()用于处理BLE连接状态的变化,当连接建立成功时,我们调用discoverServices()方法发现设备的服务和特征。在onServicesDiscovered()回调中,我们首先获取指定UUID的服务和特征,然后设置特征的通知使得设备能够向我们发送数据。最后,我们将特征的描述符设置为ENABLE_NOTIFICATION_VALUE,并发送给设备。当设备有数据发送过来时,onCharacteristicChanged()回调函数将被调用,我们可以在其中获取到设备发送的数据并进行处理。 需要注意的是,这个示例代码中的UUID、设备地址等信息需要根据实际情况进行修改。另外,由于BLE的连接和数据传输是异步进行的,因此需要注意回调函数的执行顺序和时机,以保证代码的正确性。 希望这个示例代码能够对你有所帮助!

相关推荐

最新推荐

recommend-type

Android蓝牙库FastBle的基础入门使用

主要给大家介绍了关于Android蓝牙库FastBle的基础入门使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

android Ble 蓝牙4.0 GATT 错误代码

android Ble 蓝牙4.0 GATT 错误代码对照 133 129
recommend-type

BLE蓝牙-4.0-学习笔记

蓝牙4.0 BLE center与peripheral建立连接绑定过程 2 蓝牙4.0 BLE peripheral 广播设置 7 蓝牙4.0 BLE 数据传输 (一) 11 蓝牙4.0 BLE 数据传输 (二) 12 蓝牙4.0 BLE 数据传输(三) 16 蓝牙4.0 BLE 数据传输(四...
recommend-type

由浅入深,蓝牙4.0/BLE协议栈开发攻略大全(1)

本文将结合TI推出的CC254x SoC 系列,讲解从环境的搭建到蓝牙4.0协议栈的开发来深入学习蓝牙4.0的开发过程。
recommend-type

在BLE蓝牙中一次写入超过20字节数据包的方法和技巧.docx

本文比较详细地描述了在低功耗蓝牙模块中如何一次免分包发送超过20字节数据包的方法和技巧,共享了关键方法和代码,该方法和技巧通过作者在真机上运行测试证实完全可行。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。