Android -- 蓝牙开发 <一>
2016-09-10 17:45
225 查看
关于Android蓝牙的开发
关于蓝牙,官方文档上面提到可以用来:
1.扫描其他的蓝牙设备
2.查询当地的蓝牙适配器,对蓝牙进行配对
3.建立RFCOMN(串行仿真协议)通道
4.通过服务发现连接其他设备
5.和其他设备进行数据传输
6.管理多个连接
蓝牙基础:
BlutoothAdapter(蓝牙适配器) : 所有蓝牙交互的入口点
BluetoothDevice(蓝牙设备) :代表远程蓝牙设备 可以用来连接和查询一些蓝牙设备信息
下来来创建一个蓝牙连接程序
第一步:蓝牙权限(连接蓝牙是需要权限的)
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
第二步:设置蓝牙
获得蓝牙适配器 并且开启蓝牙
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//判断设备是否支持蓝牙
if(mBluetoothAdapter == null){
//不支持蓝牙设备
}else{
//支持蓝牙设备
//如果蓝牙没有开启打开蓝牙
if(!mBluetoothAdapter.isEnabled()){
//通过意图 添加蓝牙适配器动作请求打开
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult();
//暴力打开蓝牙 mBluetoothAdapter.enabled();
}
}
第三步:发现蓝牙设备
通过蓝牙适配器可以搜索到其他远程设备 以及是否配对
查询配对设备
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
//如果存在配对设备
if(pairedDevices.size()>0){
//遍历已经配对的蓝牙设备
for(BlutoothDevice device : pairedDevices){
// 蓝牙名称和蓝牙地址
//device.getName() + "\n" + device.getAddress();
}
}
通过广播接收器来搜索蓝牙设备
需要注意的是:被搜索的蓝牙要是能够被发现的
因此需要给被搜索的蓝牙设置显示时间
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
既然要被搜索的蓝牙已经能搜到了 那么接下来我们就开始搜索吧
//开始搜索蓝牙设备
mBluetoothAdapter.startDiscovery();
//通过广播接收器进行检测
BroadcasrReceiver mReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
//通过意图得到动作
String action = intent.getAction();
//当动作是蓝牙发现设备的动作时
if(BluetoothDevice.ACTION_FOUND.equals(action)){
//通过意图获得额外的包(额外的蓝牙设备)
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//蓝牙名称和地址
//device.getName() + "\n" + device.getAddress();
}
}
};
还需要注意: 广播接收器是需要注释的:
//创建意图过滤器对象
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//注册广播
registerReceiver(mReceiver,filter);
关于蓝牙,官方文档上面提到可以用来:
1.扫描其他的蓝牙设备
2.查询当地的蓝牙适配器,对蓝牙进行配对
3.建立RFCOMN(串行仿真协议)通道
4.通过服务发现连接其他设备
5.和其他设备进行数据传输
6.管理多个连接
蓝牙基础:
BlutoothAdapter(蓝牙适配器) : 所有蓝牙交互的入口点
BluetoothDevice(蓝牙设备) :代表远程蓝牙设备 可以用来连接和查询一些蓝牙设备信息
下来来创建一个蓝牙连接程序
第一步:蓝牙权限(连接蓝牙是需要权限的)
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
第二步:设置蓝牙
获得蓝牙适配器 并且开启蓝牙
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//判断设备是否支持蓝牙
if(mBluetoothAdapter == null){
//不支持蓝牙设备
}else{
//支持蓝牙设备
//如果蓝牙没有开启打开蓝牙
if(!mBluetoothAdapter.isEnabled()){
//通过意图 添加蓝牙适配器动作请求打开
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult();
//暴力打开蓝牙 mBluetoothAdapter.enabled();
}
}
第三步:发现蓝牙设备
通过蓝牙适配器可以搜索到其他远程设备 以及是否配对
查询配对设备
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
//如果存在配对设备
if(pairedDevices.size()>0){
//遍历已经配对的蓝牙设备
for(BlutoothDevice device : pairedDevices){
// 蓝牙名称和蓝牙地址
//device.getName() + "\n" + device.getAddress();
}
}
通过广播接收器来搜索蓝牙设备
需要注意的是:被搜索的蓝牙要是能够被发现的
因此需要给被搜索的蓝牙设置显示时间
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
既然要被搜索的蓝牙已经能搜到了 那么接下来我们就开始搜索吧
//开始搜索蓝牙设备
mBluetoothAdapter.startDiscovery();
//通过广播接收器进行检测
BroadcasrReceiver mReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
//通过意图得到动作
String action = intent.getAction();
//当动作是蓝牙发现设备的动作时
if(BluetoothDevice.ACTION_FOUND.equals(action)){
//通过意图获得额外的包(额外的蓝牙设备)
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//蓝牙名称和地址
//device.getName() + "\n" + device.getAddress();
}
}
};
还需要注意: 广播接收器是需要注释的:
//创建意图过滤器对象
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//注册广播
registerReceiver(mReceiver,filter);
相关文章推荐
- 【Android开发学习45】使用google语音识别引擎(Google Speech API)<一>
- 关于Android开发中的<include/>
- Android JNI/NDK开发之基本姿势<一>
- Android开发问题积累 <加载在线Gif><WebView无法加载网页图片>
- <<Android开发艺术探索>>读书笔记--IntentFilter
- 【Android开发学习45】使用google语音识别引擎(Google Speech API)<一>
- <转>Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android开发收集<一>
- <转>Android开发使输入框点击弹出日期选择对话框的方法
- Android JNI/NDK开发之基本姿势<一>
- (转)Android开发工具——ADB(Android Debug Bridge) <一>概览
- <一>iOS 开发Push的个人见解 图文并茂 详细到我写的想吐!!!!!!!!!
- <7.22>Android开发 补充知识点
- Android React-Native系列之<一>零基础搭建React-Native开发环境
- android ContentProvider结合SQLiteHelper实现数据的共享<一>
- <2014 05 14> Android平台下2D/3D开发攻略
- Android开发<一> Ubuntu 12.04 安装jdk1.8.0_25
- 项目开发小计 <一> android开发小坑, 版本兼容
- RxAndroid 操作符使用<一>
- android studio开发<一> 按钮事件