Android 蓝牙和BLE应用开发经验参考
传统蓝牙和BLE的区别
| 技术规范 |
经典蓝牙(2.1 &3.0) |
低功耗蓝牙(4.0) |
| 无线电频率 |
2.4GHz |
2.4GHz |
| 距离 |
10米/100米 |
30米 |
| 数据速率 |
1-3Mbps |
1Mbps |
| 应用吞吐量 |
0.7-2.1Mbps |
0.2Mbps |
| 发送数据的总时间 |
100ms |
<6ms |
| 耗电量 |
1 |
0.01至0.5 |
| 最大操作电流 |
<30mA |
<15mA(最高运行时为15 mA) |
| 主要用途 |
手机,游戏机,耳机,立体声音频流,汽车和PC等 |
手机,游戏机,PC,表,体育和健身,医疗保健,汽车,家用电子,自动化和工业等 |
蓝牙的应用开发参考
蓝牙基础
l 蓝牙是一种无线技术标准,可实现固定设备、移动设备和楼宇个人域网之间的短距离数据交换(使用2.4—2.485GHz的ISM波段的UHF无线电波)。蓝牙技术最初由电信巨头爱立信公司于1994年创制,当时是作为RS232数据线的替代方案。蓝牙可连接多个设备,克服了数据同步的难题。
l 如今蓝牙由蓝牙技术联盟(Bluetooth SpecialInterest Group,简称SIG)管理
l 所有的蓝牙标准版本都支持向下兼容
l 蓝牙最新的版本号是蓝牙4.2,还有未发布的蓝牙5.0
l 一个蓝牙主设备最能多和7个蓝牙从设备进行通信
l 蓝牙的数据传输速率在1Mbps以内
l 蓝牙的理论最大通信距离是100米,传输距离越大,功耗越高,现在市面上的流行蓝牙设备的传输距离大约在30米以内,传输距离在10米左右的蓝牙设备最多。
l 蓝牙使用的频率是2.4GHZ
蓝牙应用场景
l 蓝牙应用在手机上
l 蓝牙应用在PC上,现在很多PC都带有蓝牙模块
l 蓝牙应用于其它数字设备,如数字照相机、数字摄像机等
l 篮牙技术构成的电子钱包和电子锁,这方面现在慢慢被主流的NFC所替代
l 篮牙技术在嵌入式设备上的应用如蓝牙音箱,蓝牙耳机,微波炉、洗衣机、电冰箱、空调机等
蓝牙开发基础
常用的蓝牙术语
l 扫描
l 绑定
l 配对
l 连接
蓝牙的通讯模型
蓝牙和普通的网络通信一样,是基于socket进行通讯的,采用的是典型的C/S模型
常用的蓝牙协议(Profile)
| Profile名称 |
主要用途 |
| SPP即Serial Port Profile串口通讯协议 |
主要用于蓝牙基础数据流的传输 |
| A2DP即Advanced Audio Distribution Profile 蓝牙音频传输模型协议 |
主要用来播放高品质的音乐,主要应用场景是蓝牙音箱,蓝牙耳机 |
| AVRCP即Audio Video Remote Control Profile音频/视频远程控制协议 |
主要用于蓝牙设备的远程控件,比如控制蓝牙耳机的播放,暂停,继续等 |
| HFP即Hands Free Profile 免提协议 |
主要用于车载蓝牙中,可以实现免提功能,可以控制电话的接听,挂断,拒接,音频拨号等 |
| HDP即Health Device Profile 健康设备协议 |
主要用于蓝牙血压计,蓝牙体重称等 |
| OPP即Object Push Profile 对象推送协议 |
主要用于手机与手机或者手机与电脑之间通过蓝牙进行文件操作,比如通过蓝牙发送文件 |
蓝牙开发核心类
| 类名 |
解释 |
| BluetoothAdapter |
蓝牙适配器类 |
| BluetoothDevice |
蓝牙设备信息类 |
| BluetoothSocket |
蓝牙客户端Socket类 |
| BluetoothServerSocket |
蓝牙服务端Socket类 |
| BluetoothHeadset |
蓝牙HFP协议支持类 |
| BluetoothA2dp |
蓝牙A2DP协议支持类 |
| BluetoothHealth |
蓝牙HDP协议支持类 |
蓝牙开发示例
public static String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";//SPP Profile UUID,这个UUID是蓝牙SIG组织定义的官方ID
BluetoothSocketclientSocket = null;
BluetoothServerSocket serverSocket = null;
InputStreamis;
OutputStream os;
Android蓝牙API历史
Google从Android 3.0开始提供传统蓝牙相关的开发API,蓝牙API在android.bluetooth包下面
蓝牙权限声明
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/>
打开蓝牙
通过代码打开
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter(); if(adapter.isEnabled()) { adapter.disable();//关闭设备蓝牙 } else{ adapter.enable();//打开设备蓝牙 }
通过系统蓝牙对话框打开
public static void openBluetoothWithDialog(Activity activity, int seconds) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
activity.startActivityForResult(intent, REQUEST_ENABLE);
}
注册蓝牙状态监听器
public void registerBluetoothReceiver() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); // filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); // filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); mActivity.registerReceiver(receiver, filter); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); deviceList.add(device); createBond(device); } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if(device.getBondState()==BluetoothDevice.BOND_BONDED){ try { clientSocket = BluetoothHelper.connect(device); remoteDevice = clientSocket.getRemoteDevice();

最低0.47元/天 解锁文章
1157

被折叠的 条评论
为什么被折叠?



