安卓蓝牙技术开发笔记

安卓蓝牙技术开发笔记
课前准备
第一章节
蓝牙简介
无线传输方案
WIFI
工作频段: 2.4G/5G
有效传输距离 10 米至数公里
传输速度:
802.11 协议族
802.11b 11Mpbs
802.11g 54Mpbs
802.11n 600Mbps
802.11as 1.3Gbps
常用场景,只能家居,无人控制
功耗较高
NFC
近场通讯技术
能在短距离内与兼容设备进行识别和数据交换
短距高频的无线电技术,在 13.56MHz 频率运行于 20cm 距离内
速度在 424kbit/s 一下,不需要电池 功耗很低
应用场景:手机支付,门禁,信用卡, AndroidBeam 分享照片和视频
BlueTooth
是一种无线技术标准,可实现 固定设备、移动设备和楼与个人局域网之间的短距离数
据交换
工作频段 2.4G, 理论传输速度 24Mbps 传输距离一般在 10m
最新的标准是 4.0 功耗低
应用场景:
蓝牙耳机、蓝牙音响、无线鼠标、个人健康设备、无线传输数据、车载设备通信
等。
总结
网络类型
NFC
蓝牙
WIFI
连接方式
点对点 单点对多点 单点对多点
使用距离 <=0.02m <=10m <10m-1000m
速度 <500Kpbs <24MKbps <1.3Gbps
功耗 很低
中低
课程基础阶段
第一节 添加权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
蓝牙在设备上的可用性
BluetoothAdapter 本机的蓝牙适配器
BluetoothDevice 远程的蓝牙适配器
支持蓝牙设备
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null){
//Device does not support Bluetooth;
}
蓝牙开关
isEnabled();
打开关闭蓝牙
人工交互方式:
Intent enableBtlntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
// 收听结果
onActivityResult()
RESULT_OK
RESULT_CANCELED
自动方式
enable
disable 监听操作结果
// 监听广播
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
// 广播内
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1);
switch (state){
case BluetoothAdapter.STATE_OFF: // 蓝牙关闭
showToast("STATE_OFF");
break;
case BluetoothAdapter.STATE_ON: // 蓝牙打开
showToast("STATE_ON");
break;
case BluetoothAdapter.STATE_TURNING_ON: // 蓝牙正在打开
showToast("STATE_TURNING_ON");
break;
case BluetoothAdapter.STATE_TURNING_OFF: // 蓝牙正在关闭
showToast("STATE_TURNING_OFF");
break;
default: // 位置状态
showToast("Unkown STATE");
break;
}
第二节
查找设备与设备可见
// 查找设备
startDiscovery()
// 启用设备可见性
public void enableVisiby(Context context){
Intent discpverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
// 设置设备 5 分钟 300s 之内可以显示
discpverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
context.startActivity(discpverableIntent);
}
获取查找结果
获取查找结果:
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver,filter); //don`t forget to unregiste during onDestroy
BluetoothDevice device = intent.getParceableExtra(BluetoothDevice.EXTRA_DEVICE); 绑定设备
BluetoothDevice device
device.createBond();
//android api 19 以上才允许程序绑定设备 android SDK 4.4 以下不允许程序绑定设备
绑定设备需要这个权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
小实例
第三节
通用 Socket 连接流程
创建连接之服务器端
步骤:
1. 通过 listenUsingRfcommWithServiceRecord 创建一个 BluetoothServerSocket
2. 监听网络 accept
3. 处理网络 socket
4. 关闭连接
创建连接之客户端
步骤:
1. 通过 createRfcommSocketToServiceRecord 创建一个 BluetoothSocket
2. 连接服务端 connet
3. 处理数据
4. 关闭连接
数据通讯实例
第四节
蓝牙规范介绍
什么是蓝牙规范 (Bluetooth Profile), 又叫配置文件
Profile 定义了一种基于蓝牙的应用,每个 Profile 规范主要包括针对开发者的接口,消息的格
式和标准,以及如何使用蓝牙协议栈。
UUID
在蓝牙中,每个 Profile 对应于一个 UUID 。每次通信的双方必须使用同一个 UUID
常见 Profile 介绍
A2DP 全名: Advanced Audio Distribution Profile 蓝牙音频传输模型,通常应用方式 蓝牙立
体耳机 + 手机。大部分的 API 都是非公开的,不好使用
BluetoothHeadset
最常用的 Profile 带电话功能的蓝牙耳机
BluetoothHealth
支持带蓝牙的健康设备
蓝牙耳机连接实例
蓝牙聊天项目
软件设计 代码讲解
知识点复习
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_35652070

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值