Android 蓝牙开发(二)开启蓝牙,获取状态,发现设备。
2015-09-23 16:47
615 查看
(1)获取蓝牙相关权限。
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
(2)获取蓝牙状态。
获取蓝牙适配器:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
查看当前设备是否支持蓝牙:
if(bluetoothAdapter == null){
//("本机不支持蓝牙");
return;
}
查看蓝牙是否已经打开:
if(!bluetoothAdapter.isEnabled()){
//("本机蓝牙未打开");
}
(3)打开蓝牙。
启用Intent,让系统开启蓝牙:
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent , REQUEST_ENABLE_BLUETOOTH);
在ActivityResult中处理结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == REQUEST_ENABLE_BLUETOOTH){
//("蓝牙打开成功 ");
}
}(4)让本机蓝牙可以被其他蓝牙设备发现:
if(bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE){
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION , 300);
startActivity(discoverableIntent);
//("本机蓝牙 300 秒内可见");
}(5)查看已经配对过的蓝牙设备。
使用 adapter.startDiscovery() 方法来搜索设备,通过注册蓝牙相关广播监听,来获取和处理搜索结果。
相关监听广播有:IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);//发现蓝牙设备后的广播
IntentFilter startedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//开始搜索的广播
IntentFilter finishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//搜索结束后的广播
当搜索到附近可用蓝牙设备后,通过:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);获取设备相关信息,如:
device.getName() //设备名称
device.getAddress() //地址:如E3:34:23:D3:E2:98
device.getBondState() // 设备的绑定状态
device.getType() //设备的类型
device.getBluetoothClass() // BluetoothClass
device.getUuids() //uuid组
本文 Github 地址: https://github.com/huntervwang/BluetoothDemo
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
(2)获取蓝牙状态。
获取蓝牙适配器:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
查看当前设备是否支持蓝牙:
if(bluetoothAdapter == null){
//("本机不支持蓝牙");
return;
}
查看蓝牙是否已经打开:
if(!bluetoothAdapter.isEnabled()){
//("本机蓝牙未打开");
}
(3)打开蓝牙。
启用Intent,让系统开启蓝牙:
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent , REQUEST_ENABLE_BLUETOOTH);
在ActivityResult中处理结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == REQUEST_ENABLE_BLUETOOTH){
//("蓝牙打开成功 ");
}
}(4)让本机蓝牙可以被其他蓝牙设备发现:
if(bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE){
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION , 300);
startActivity(discoverableIntent);
//("本机蓝牙 300 秒内可见");
}(5)查看已经配对过的蓝牙设备。
Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices(); if(devices.size() > 0){ //处理 }(6)开启发现周边蓝牙设备。
使用 adapter.startDiscovery() 方法来搜索设备,通过注册蓝牙相关广播监听,来获取和处理搜索结果。
相关监听广播有:IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);//发现蓝牙设备后的广播
IntentFilter startedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//开始搜索的广播
IntentFilter finishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//搜索结束后的广播
当搜索到附近可用蓝牙设备后,通过:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);获取设备相关信息,如:
device.getName() //设备名称
device.getAddress() //地址:如E3:34:23:D3:E2:98
device.getBondState() // 设备的绑定状态
device.getType() //设备的类型
device.getBluetoothClass() // BluetoothClass
device.getUuids() //uuid组
本文 Github 地址: https://github.com/huntervwang/BluetoothDemo
相关文章推荐
- Android快捷开关实现(转)
- Android读写文件操作
- Android.mk文件编写规则
- android sqlite 数据库 CURD 操作
- Android studio 安装 环境搭建
- Android 代码实现logcat输出到文件
- Android 项目部署之Nexus私服搭建和应用
- Android studio创建模拟器报错
- Android 应用程序之间内容分享详解(二)
- Android服务的方法回传调用
- Android 应用程序之间内容分享详解(一)
- Android软键盘弹出将底部栏顶上去并不会挤压界面
- Android应用如何支持屏幕多尺寸多分辨率问题
- Android利用Filter过滤数据
- 在Android中查看和管理sqlite数据库
- 解决Android单个dex文件不能超过65536个方法问题
- Android基础入门教程——2.4.8 ListView Item多布局的实现
- android通过USB的MTP模式下,禁止用户在根目录有任何操作(例如删除文件夹)
- Android 自定义过滤搜索框 filterable
- Android SDK 源码关联