Android Bluetooth开发1
来源:互联网 发布:ecshop php 5.5 编辑:程序博客网 时间:2024/05/17 23:47
1、获取本地蓝牙适配器
BluetoothAdapter mAdapter= BluetoothAdapter.getDefaultAdapter();
2、打开蓝牙
if(!mAdapter.isEnabled()){
//弹出对话框提示用户是后打开
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, REQUEST_ENABLE);
//不做提示,强行打开
// mAdapter.enable();
}
3、搜索设备
1)第一步,mAdapter.startDiscovery()
2)定义BroadcastReceiver,
BroadcastReceiver 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);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
Log.v(TAG, "find device:" + device.getName()
+ device.getAddress());
}
}
//搜索完成
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
setTitle("搜索完成");
}
}
}
};
这样,没当查找到新设备或是搜索完成,相应的操作都在上段代码的两个if里执行了,不过前提是你要先注册
BroadcastReceiver,具体代码如下
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
(这段代码,一般写在onCreate()里..)
1)服务器端:
//UUID格式一般是"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"可到
//http://www.uuidgenerator.com 申请
BluetoothServerSocket serverSocket = mAdapter. listenUsingRfcommWithServiceRecord(serverSocketName,UUID);
serverSocket.accept();
2)客户端:
//在BroadcastReceiver获取了BLuetoothDevice
BluetoothSocket clienSocket=dcvice. createRfcommSocketToServiceRecord(UUID);
clienSocket.connect();
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
2)写出、读入
BluetoothAdapter mAdapter= BluetoothAdapter.getDefaultAdapter();
2、打开蓝牙
if(!mAdapter.isEnabled()){
//弹出对话框提示用户是后打开
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, REQUEST_ENABLE);
//不做提示,强行打开
// mAdapter.enable();
}
3、搜索设备
1)第一步,mAdapter.startDiscovery()
2)定义BroadcastReceiver,
BroadcastReceiver 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);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
Log.v(TAG, "find device:" + device.getName()
+ device.getAddress());
}
}
//搜索完成
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
setTitle("搜索完成");
}
}
}
};
这样,没当查找到新设备或是搜索完成,相应的操作都在上段代码的两个if里执行了,不过前提是你要先注册
BroadcastReceiver,具体代码如下
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
(这段代码,一般写在onCreate()里..)
4、建立连接
首先Android sdk(2.0以上版本)支持的蓝牙连接是通过BluetoothSocket建立连接,服务器端(BluetoothServerSocket)和客户端(BluetoothSocket)需指定同样的UUID,才能建立连接,因为建立连接的方法会阻塞线程,所以服务器端和客户端都应启动新线程连接
1)服务器端:
//UUID格式一般是"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"可到
//http://www.uuidgenerator.com 申请
BluetoothServerSocket serverSocket = mAdapter. listenUsingRfcommWithServiceRecord(serverSocketName,UUID);
serverSocket.accept();
2)客户端:
//在BroadcastReceiver获取了BLuetoothDevice
BluetoothSocket clienSocket=dcvice. createRfcommSocketToServiceRecord(UUID);
clienSocket.connect();
5、数据传递
通过以上操作,就已经建立的BluetoothSocket连接了,数据传递是通过流的形式
1)获取流inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
2)写出、读入
1 0
- Android Bluetooth开发1
- android bluetooth开发基础-1
- Android Bluetooth蓝牙开发:发现Bluetooth蓝牙设备(1)
- android bluetooth开发
- Android Bluetooth开发
- BlueTooth Android开发基础
- Android bluetooth 开发
- android 开发之 Bluetooth
- Android bluetooth开发
- Android BlueTooth开发
- Android bluetooth开发实践
- android Bluetooth蓝牙开发
- android bluetooth应用开发详解
- android bluetooth开发基础-0
- android bluetooth开发基础-0
- android Bluetooth开发基本流程
- android Bluetooth 开发1之布局和权限
- 【Android 应用开发】Android之Bluetooth编程
- C++关键字总结explict/auto/decltype/typedef等
- 分布式缓存-Memcached
- $ is not defined 的问题
- js代码库
- 我的保研经历——中国科学院计算技术研究所(更新中。。)
- Android Bluetooth开发1
- 传统 vs 敏捷 ?第三方业务风控服务企业项目管理分析
- 快递查询 (快递100)
- 设计模式之见解一
- Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录?
- android app防被杀策略
- 盒子模型
- EventBus简单实用
- DOM事件