上周和这周两周时间了都没有时间写博客,因为在忙手上蓝牙的事情,这两周的心血下来也就写了一个demo,学习时间告一段落,今天就把学到的东西跟大家分享一下:
首先,这个project的指导文件是android developer上提供的guide,通过这个api guide一步一步来进行操作。
demo见附件(android Studio下的工程),简单讲解一下demo和bluetooth的api guide。
1,蓝牙的功能:
- Scan for other Bluetooth devices
- Query the local Bluetooth adapter for paired Bluetooth devices
- Establish RFCOMM channels
- Connect to other devices through service discovery
- Transfer data to and from other devices
- Manage multiple connections
2,了解一些蓝牙基本的类
3,在androidmanifest中添加蓝牙许可
4,配置蓝牙,在demo中,第一个键“开启蓝牙状态”就是注册监听蓝牙状态的broadcast,如果蓝牙的状态发生变化,会到此broadcast中来执行
5,第二个键“检查蓝牙设备”的功能是先检查此android设备是否支持蓝牙,如果支持,查看蓝牙的状态,如果蓝牙是关闭模式,则弹出对话框让用户选择打开蓝牙。
6,第三个键是开启发现侦测,点击时会注册监听本机蓝牙discovery属性变化的broadcast,如果蓝牙的dicovery属性发生变化,则会在此broadcast中来执行。
7,第四个键是“设置此被发现”,点击时系统会询问用户是否设置此蓝牙被发现,即被搜索,如果用户选择是,则会打开20分钟的搜索时间。
8,第五个键是注册监听广播,这个广播的用途是如果蓝牙在搜索周围蓝牙设备时,如果搜索到了设备,则在此广播中进行操作。
9,第六个键是“开始扫描蓝牙”,扫描到的设备会在第五个键注册的broadcast中进行处理。
10,第七个键是“显示扫描蓝牙”,开始扫描蓝牙之后,扫描到的蓝牙设备信息会在蓝牙发现监听广播中去处理,即将搜索到的蓝牙装置的信息,如macAddress,name和RSSI信息保存在一个list对象数组当中,此键按下时会生成一个新的activity来将扫描到的设备显示在一个listview当中。在这个activity中还会启动一个timer来每8s更新一下搜索到的蓝牙设备信息,包括蓝牙,蓝牙的名称,mac地址和信号强度。
11,第八个键是显示配对设备,里边包括的是以前已经配对过的设备信息,也是一个listActivity。
12,最后一个键开启连接监听,是新生成一个socket来监听有没有别的设备来主动连接自己,一直找到连接自己的设备为止。使用方法是先按“开启连接监听”按钮,然后重新准备一台设备,安装此app,打开后,初始化蓝牙设备,搜索蓝牙装置,然后在搜索到的listActivity中点击刚才被动监听的设备,点击此Item项后,此设备会新生成一个通道,这个通道被刚才的蓝牙设备监听到后就会自动连接,连接后两个设备都会问是否要匹配,两边都点击匹配,则两个android app都会跳到一个对话框页面,两个app之间就可以通过蓝牙进行交流了。
13,这时,如果退出这个界面,在主界面中点击显示配对设备按钮,则相互连接的设备就会出现在对象的listView当中。
14,两者进行通信:
第一个按钮是开始接受信息,按了之后会创建一个通道等待输入流的到来。
第二个按钮是清除,会自动清除listview中的聊天记录和对话框中的聊天语句。
第三个按钮是发送消息,会将你在editText中输入的字符以流的形式写入到socket当中去,然后会被接受方接受,将记录添加到listview当中去。
第四个按钮是断开连接,点击后会将两个设备创建的信息通道断开,如果要传输数据,就得重新连接了。
相关推荐
Getting Started with Bluetooth Low Energy由飞思卡尔工程师推荐
Getting started with bluetooth low energy 低功耗蓝牙技术参考,比较通俗易懂
Getting Started with Bluetooth Low Energy.pdf 低功耗蓝牙的开发
With Bluetooth Low Energy (BLE), smart devices are about to become even smarter. This practical guide demonstrates how this exciting wireless technology helps developers build mobile apps that share ...
Programming the Intel Edison: Getting Started with Processing and Python lays out the Edison’s powerful features and teaches the basics of Internet-enabled embedded programming. Discover how to ...
适合需要蓝牙入门的同学,对蓝牙技术的发展过程,以及蓝牙的主要功能做了介绍
Getting Started with Bluetooth Low Energy.pdf RTL8762 CC254X 小蜜蜂BLE开发板简介v.pdf TI官方BLE教程.pdf xRTL8762AK_Datasheet_1.0 (DRAFT).pdf 低功耗蓝牙4.0(BLE)主从通信模块及主透传协议.pdf 蓝牙4.0寻物...
Chapter 1 Getting Started with iPad Programming. 3 Chapter 2 Write Your First Hello World! Application . 23 Chapter 3 Views, Outlets, and Actions. 45 Chapter 4 View Controllers. 77 Chapter 5 Keyboard ...
Getting started with STM32CubeWB for STM32WB Series.pdf Getting started with STM32CubeWB for STM32WBseries.docx p-nucleo-wb55.pdf STM32Cube MCU package examples for STM32WB Series.docx STM32Cube MCU ...
Chapter 1 Getting Started Chapter 2 Designing a Successful Application Chapter 3 Application Testing Chapter 4 Inter-/Intra-Process Communication Chapter 5 Graphics Chapter 6 Graphical User Interface ...
CHAPTER 2 Getting Started 17 CHAPTER 3 Creating Applications and Activities 49 CHAPTER 4 Creating User Interfaces 85 CHAPTER 5 Intents, Broadcast Receivers, Adapters, and the Internet 137 ...
│ UI+EEPROM Getting Started.txt │ ├─PC Chat Tool │ BT Chat v005.exe │ ├─UI │ └─BM77SPP UI v301.107(FW v1.42 RC) │ └─BM77SPP03 UI v301.107 │ BM77SPP03 UI v301.107 default.txt │ BM77...
Getting started with Arduino—no experience required! Writing programs for Arduino Sensing and responding to events Robots, flying vehicles, Twitter machines, LCD displays, and more! Purchase of the ...
* Getting Started with Arduino TIAN * Arduino TIAN Sketch Programming * Arduino TIAN Linux Programming * Working with Bluetooth on Arduino TIAN * Building IoT Program for Arduino TIAN * Energy Saving ...
Mobile developers have always had to think for the future, and right now that means getting started with Google Glass.This book is incredibly hands-on with many exciting projects. You will learn the ...
CHAPTER 2 Getting Started ............17 CHAPTER 3 Creating Applications and Activities ........ 49 CHAPTER 4 Creating User Interfaces .......... . . 85 CHAPTER 5 Intents, Broadcast Receivers, ...