该博客只是记录,不会详细说明(本人技术有限不会说)
一:声明蓝牙权限和定位权限

<!--蓝牙权限-->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!--定位权限-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

二:java代码(该代码是连接蓝牙波特率为115200用)

 //蓝牙private BluetoothAdapter bluetoothAdapter;private BluetoothDevice bluetoothDevice;private BluetoothGatt bluetoothGatt;private BluetoothGattCharacteristic bluetoothGattCharacteristic;private Bluetooth bluetooth = new Bluetooth();@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.testdemo);bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(BluetoothDevice.ACTION_FOUND);intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);registerReceiver(bluetooth,intentFilter);   //注册广播bluetoothAdapter.startDiscovery();  //启动扫描蓝牙任务bluetoothAdapter.startLeScan(mLeScanCallback);  //开始扫描蓝牙设备}private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {@Overridepublic void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {runOnUiThread(new Runnable() {@Overridepublic void run() {String deviceName =device.getName();if (deviceName != null && !"".equals(deviceName)) {if (deviceName.equals(NAME)){bluetoothAdapter.stopLeScan(mLeScanCallback);   //停止扫描蓝牙bluetoothDevice = device;connect(bluetoothDevice);   //开始连接Bluetooth_scanning.setVisibility(View.GONE);}}}});}};//蓝牙连接private void connect(BluetoothDevice device){if (device != null) {//第二个参数 是否重连bluetoothGatt = device.connectGatt(Demo.this, true, bluetoothGattCallback);}}//蓝牙连接的回调private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {//蓝牙连接成功后第一个回调的方法@Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {super.onConnectionStateChange(gatt, status, newState);if (newState == BluetoothProfile.STATE_CONNECTED) {//蓝牙连接成功gatt.discoverServices();    //开始扫描蓝牙服务,扫描到设备服务后会回调onServicesDiscovered方法} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {   //蓝牙断开连接if (bluetoothGatt != null) {//重新连接//关闭当前新的连接gatt.close();bluetoothGattCharacteristic = null;connect(bluetoothDevice);}}}@Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) {  //此方法需先调用gatt.discoverServices();才会回调super.onServicesDiscovered(gatt, status);//发现服务if (status == BluetoothGatt.GATT_SUCCESS) {//蓝牙模块提供的负责通信UUID字符串BluetoothGattService service = gatt.getService( SERVICE_UUID);if(service != null) {//写数据的服务和特征BluetoothGattCharacteristic write = service.getCharacteristic(UUID);//读取数据的服务和特征BluetoothGattCharacteristic  read = service.getCharacteristic(UUID);if(read != null) {//订阅读取通知gatt.setCharacteristicNotification(read, true);BluetoothGattDescriptor descriptor = read.getDescriptor(READ_UUID);descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);gatt.writeDescriptor(descriptor);}}} else {Log.v(TAG,"没有发现蓝牙服务");}}@Overridepublic void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {super.onCharacteristicWrite(gatt, characteristic, status);}@Overridepublic void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {super.onCharacteristicRead(gatt, characteristic, status);}//接收数据@Overridepublic void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {super.onCharacteristicChanged(gatt, characteristic);}};

Android 蓝牙连接(简单简单版)相关推荐

  1. android连接蓝牙耳机,蓝牙耳机厂家:Android蓝牙连接的一些心得

    蓝牙耳机厂家最近做一个项目,主要是给蓝牙发送指令的,boss要求能够最快速度的搜索到蓝牙,并且发送数据. 刚开始也遇到很多133,各种断开连接的问题.android蓝牙搜索有两种方式,一种startL ...

  2. android蓝牙连接133问题的解决办法---(连接篇)

    上一篇文章介绍了"蓝牙扫描",今天来说一下android蓝牙连接过程中133的问题: 我们经常在网上看到一些答案说需要释放gatt资源,这种方式可以在一定程度上减少出现133的概率 ...

  3. Android蓝牙连接问题总结

    最近开始接触Android的蓝牙设备问题,严格意义上来说,也算是第二次接触蓝牙机制了,之前对于蓝牙设备的整个过程,也不是太了解,只是接触了一些自己需要的部分.而这次应该算是比较深入的了解了蓝牙机制的部 ...

  4. android 蓝牙 连接失败,Android蓝牙连接 – 服务发现失败

    我正在尝试创建一个基本的蓝牙应用程序,用于测试设备. 我从developer.android获得了代码. 这是链接: http : //developer.android.com/guide/topi ...

  5. android蓝牙连接通信的实现

    看了很多的博客文章的技术,今天也写一写自己的第一篇博客文章,我只说说如何用,具体实现的原理,原谅我不太清楚,但对于大多数人来说,知道怎么做出来就很不错了哈哈!不多说了,开始吧. 首先要知道几个类,Bl ...

  6. Android 蓝牙连接打印机打印网络图片

    实现蓝牙连接打印机打印网络图片 经过自己一下午加一个小时的时间整理出来,希望能帮助到各位码兄弟! 主要分为以下几步: 将网络图片URL转为bitmap :其中需要进行网络请求,不可在主线程中进行,需另 ...

  7. android蓝牙连接取消后怎么重新连上,重新启动后接收蓝牙连接更改

    我试图与接收器一起检查与不同设备的蓝牙连接,然后将其记录在logcat中.它适用于正常情况,但在重新启动时会失败.重新启动后接收蓝牙连接更改 这是一个正常的工作流程: 手机上 切换蓝牙开/关 重启手机 ...

  8. Android 蓝牙连接

    一.概述 蓝牙是一种无线技术标准,可实现固定设备.移动设备和楼宇个人域网之间的短距离数据交换.最多可以同时和7个其它蓝牙设备建立连接,进行通信.蓝牙可分为两大类:传统蓝牙(蓝牙3.0规范之前),低功耗 ...

  9. DCloud UniAPP Android 蓝牙连接ESCPOS打印机

    1.蓝牙打印引用文件 btprinter.js import context from "@/common/context.js" //上下文处理 用来保存绑定的蓝牙设备地址fun ...

最新文章

  1. spacemacs各种问题修复方法
  2. 程序员怎样获取更多的劳动收入
  3. SQL中的CASE使用方法
  4. C++:map和pair
  5. 《Dotnet9》系列-Google ProtoBuf在C#中的简单应用
  6. gradle 编译java配置文件_java – 如何在编译时使gradle使用正确的JDK?
  7. pcb封装lib文件转pads_想做PCB达人?掌握这些PCB主流软件很关键!
  8. 拷贝构造函数——防篡改
  9. selenium模拟点击的几种方法探讨
  10. lc滤波器是利用电感的感抗_电感器在电路中的应用特性
  11. 基于Matlab的车牌号识别
  12. 计算机应用教程 卢湘鸿答案,计算机应用教程(Windows 2000环境)习题解答与实验指导...
  13. java 异或表示状态
  14. c语言十进制转二进制过程,C语言十进制转二进制代码实例
  15. 室内定位中非视距的识别和抑制算法研究综述(部分)
  16. CNN 入门讲解:什么是标准化?
  17. 什么软件可以截取电视剧视频片段?这3款软件极易上手!
  18. 油溶性Cu,Mn共掺杂量子点光转换材料
  19. HC-05,HC-06AT指令
  20. AGV的关键技术与细节

热门文章

  1. python获取计算机时间_python怎么获取系统当前的时间
  2. HTML 基础【2】 -- 表格标签 / 表单标签
  3. 小米平板4-点击usb传输文件电脑还是找不到平板盘符(需要调出开发者选项及步骤)...
  4. 游戏行业人才结构分析
  5. 银杏节画出属于你的银杏
  6. 终止进程:killall 、kill 、pkill
  7. linux vtune 生成文字报告,文字报告太枯燥,教你一招,只需十分钟,一屏可视化!...
  8. 进程的创建、杀死、与资源回收
  9. 改变QLabel背景颜色
  10. 预埋单交易系统-系统功能需求说明书