因为项目需要,需要将本地数据库的数据,通过蓝牙传输到另一台设备上。然后百度了蛮久,看了蛮多的,觉得有必要自己整理一下,蓝牙的传输功能。

首先,我们都知道的,蓝牙连接数需要先配对的,两台手机配对后才可进行蓝牙处理。对于蓝牙配对的方法网上一大堆,我也就不具体说了,大概记录一下。基本的我们可以通过手机原有的蓝牙功能,进行搜索配对后在自身的APK中,直接启动蓝牙,然后查询已匹配设备就行了。因为项目目前没有要求实现匹配功能,我也就能省则省了...不过还是总体介绍一下吧。

在安卓源码中,首先要知道有提供了一个蓝牙类:BluetoothAdapter , 我们需要先初始化这个类,才能huod获得蓝牙的相关数据。 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  接着我们需要先注册广播,广播的注册有静态注册和动态注册,我就不详说了,我这用的是动态注册:

  //注册蓝牙广播IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);getActivity().registerReceiver(receiver, filter);filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);getActivity().registerReceiver(receiver, filter);

然后再实现广播的处理:

public class BluetoothReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action.equals(BluetoothDevice.ACTION_FOUND)) {//未匹配BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {//搜索完成}}}

基本就是这样,里面的参数判断的话,蓝牙有各种不同的状态,可以根据搜索到的设备进行检查,然后区分当前蓝牙处于什么状态。而我因为业务原因,暂时也不需要去监听匹配,所以,我用的是以下这个方法:

   Set<BluetoothDevice> bondedDevices = mBluetoothAdapter.getBondedDevices();for (BluetoothDevice bondedDevice : bondedDevices) {bluetoothDevices.add(bondedDevice);}

当蓝牙开启后,通过此方法,获得已匹配的设备。

另外补充一个开启蓝牙的方法:此方法为了让其他设备可以搜索到本机,里面的300表示暴露时间秒

 //开启显示,让本机可以被搜索到if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);startActivity(discoverableIntent);} else {showToast("蓝牙已开启");}

好了,以上是一些基本的蓝牙功能,到哪里都可以找到一大堆的。下面开始记录关键,蓝牙数据传输

既然要传输数据,和给api通讯一样,肯定是要有一个服务端,一个客户端的。而服务端是需要在开启状态,监听客户端发送的数据。而服务属于阻塞操作,所以要开启一个线程,再来开启一个服务:

 BluetoothServerSocket mmServerSocket=mBluetoothAdapter.listenUsingRfcommWithServiceRecord("Name", UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));BluetoothSocket socket = mmServerSocket.accept();

通过以上方法来开启一个服务,这里“Name”,UUID,服务端和客户端都必须一样,这是识别用的。另外要注意mmServerSocket 是否为 null ,若未null 则也是无法开启的。另外在之后若出现无法连接的情况,这里我们可以查下缓一缓UUID的值,具体这个坑什么原因我也说不清,反正换一换呗。

当以上的方法调用了之后,服务器基本就算开启了的。只要没抛异常的话。

然后我们需要一个输入流,可以写循环,也可以不写循环。循环的话就通过传输过来的数据,做个标记结束它。这些都是一些废话,就直接写个流吧...


InputStream mmInStream = socket.getInputStream();bytes = mmInStream.read(buffer);String data = new String(buffer, 0, bytes, "UTF-8");Message msg = Message.obtain();msg.what = Constants.MESSAGE_READ;msg.obj = data;mHandler.sendMessage(msg);

这里注意,我们在之前开启服务端的时候,要获得一个蓝牙的操作类:BluetoothSocket ,这个类是用来操作流,服务的。ranho然后通过handler 将接收的数据回传到主线程。服务端到这里就完了。

接着写客户端的:

客户端这边就轻松多了,既然服务端已经开启,我们就进行连接,通过BluetoothAdapter 我们已经得到了已经匹配的设备,然后选择你要连接的蓝牙设备,不过记得先关闭蓝牙搜索功能:

mBluetoothAdapter.cancelDiscovery();
BluetoothSocket mmSocket= device.createRfcommSocketToServiceRecord(UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));mmSocket.connect();

然后用要连接的设备device,输入服务端的UUID,接着连接。当然,以上的操作也是需要在子线程中进行的。

连接结束后,可以写个方法通知服务端,谁谁谁连接了。当然我们也要写一个输出流,用于一会输出数据到服务端去。

最后,最最重要的,用完了li流一定要记得关闭。

为了以后方便,我也做了一个功能类,之后需要用时就直接调用。这里就贡献给大家分享一下吧!


public class BluetoothChatService {private static final String TAG = "BluetoothChatService";private static final String NAME_SECURE = "BluetoothChatSecure";private static final String NAME_INSECURE = "BluetoothChatInsecure";private static final UUID MY_UUID_SECURE =UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");public final BluetoothAdapter mAdapter;public final Handler mHandler;public AcceptThread mSecureAcceptThread;public AcceptThread mInsecureAcceptThread;public ConnectThread mConnectThread;public ConnectedThread mConnectedThread;private int mState;public static final int STATE_NONE = 0;public static final int STATE_LISTEN = 1;public static final int STATE_CONNECTING = 2;public static final int STATE_CONNECTED = 3;public BluetoothChatService(Context context, Handler handler) {mAdapter = BluetoothAdapter.getDefaultAdapter();mState = STATE_NONE;mHandler = handler;}private synchronized void setState(int state) {Log.d(TAG, "setState() " + mState + " -> " + state);mState = state;mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();}public synchronized int getState() {return mState;}public synchronized void start() {Log.d(TAG, "start");if (mConnectThread != null) {mConnectThread.cancel();mConnectThread = null;}if (mConnectedThread != null) {mConnectedThread.cancel();mConnectedThread = null;}setState(STATE_LISTEN);if (mSecureAcceptThread == null) {mSecureAcceptThread = new AcceptThread();mSecureAcceptThread.start();}}public synchronized void connect(BluetoothDevice device) {Log.d(TAG, "connect to: " + device);if (mState == STATE_CONNECTING) {if (mConnectThread != null) {mConnectThread.cancel();mConnectThread = null;}}if (mConnectedThread != null) {mConnectedThread.cancel();mConnectedThread = null;}mConnectThread = new ConnectThread(device);mConnectThread.start();setState(STATE_CONNECTING);}public synchronized void connected(BluetoothSocket socket, BluetoothDevicedevice) {if (mConnectThread != null) {mConnectThread.cancel();mConnectThread = null;}if (mConnectedThread != null) {mConnectedThread.cancel();mConnectedThread = null;}if (mSecureAcceptThread != null) {mSecureAcceptThread.cancel();mSecureAcceptThread = null;}if (mInsecureAcceptThread != null) {mInsecureAcceptThread.cancel();mInsecureAcceptThread = null;}mConnectedThread = new ConnectedThread(socket);mConnectedThread.start();Message msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME);Bundle bundle = new Bundle();bundle.putString(Constants.DEVICE_NAME, device.getName());msg.setData(bundle);mHandler.sendMessage(msg);setState(STATE_CONNECTED);}public synchronized void stop() {Log.d(TAG, "stop");if (mConnectThread != null) {mConnectThread.cancel();mConnectThread = null;}if (mConnectedThread != null) {mConnectedThread.cancel();mConnectedThread = null;}if (mSecureAcceptThread != null) {mSecureAcceptThread.cancel();mSecureAcceptThread = null;}if (mInsecureAcceptThread != null) {mInsecureAcceptThread.cancel();mInsecureAcceptThread = null;}setState(STATE_NONE);}public void write(byte[] out) {ConnectedThread r;synchronized (this) {if (mState != STATE_CONNECTED)return;r = mConnectedThread;}r.write(out);}private void connectionFailed() {Message msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST);Bundle bundle = new Bundle();bundle.putString(Constants.TOAST, "无法连接设备!");msg.setData(bundle);mHandler.sendMessage(msg);BluetoothChatService.this.start();}private void connectionLost() {Message msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST);Bundle bundle = new Bundle();bundle.putString(Constants.TOAST, "设备连接已断开");msg.setData(bundle);mHandler.sendMessage(msg);BluetoothChatService.this.start();}private class AcceptThread extends Thread {// The local server socketprivate final BluetoothServerSocket mmServerSocket;private String mSocketType;public AcceptThread() {BluetoothServerSocket tmp = null;try {tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID_SECURE);} catch (IOException e) {// Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);}mmServerSocket = tmp;}public void run() {BluetoothSocket socket = null;while (mState != STATE_CONNECTED) {try {socket = mmServerSocket.accept();} catch (IOException e) {//  Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);break;}if (socket != null) {synchronized (BluetoothChatService.this) {switch (mState) {case STATE_LISTEN:case STATE_CONNECTING:// Situation normal. Start the connected thread.connected(socket, socket.getRemoteDevice());break;case STATE_NONE:case STATE_CONNECTED:try {socket.close();} catch (IOException e) {//Log.e(TAG, "Could not close unwanted socket", e);}break;}}}}}public void cancel() {// Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);try {mmServerSocket.close();} catch (IOException e) {// Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);}}}private class ConnectThread extends Thread {private final BluetoothSocket mmSocket;private final BluetoothDevice mmDevice;public ConnectThread(BluetoothDevice device) {mmDevice = device;BluetoothSocket tmp = null;try {tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);} catch (IOException e) {//  Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);}mmSocket = tmp;}public void run() {// Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);mAdapter.cancelDiscovery();try {mmSocket.connect();} catch (IOException e) {try {mmSocket.close();} catch (IOException e2) {//   Log.e(TAG, "unable to close() " + mSocketType +//         " socket during connection failure", e2);}connectionFailed();return;}synchronized (BluetoothChatService.this) {mConnectThread = null;}connected(mmSocket, mmDevice);}public void cancel() {try {mmSocket.close();} catch (IOException e) {//  Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);}}}private class ConnectedThread extends Thread {private final BluetoothSocket mmSocket;private final InputStream mmInStream;private final OutputStream mmOutStream;public ConnectedThread(BluetoothSocket socket) {// Log.d(TAG, "create ConnectedThread: " + socketType);mmSocket = socket;InputStream tmpIn = null;OutputStream tmpOut = null;try {tmpIn = socket.getInputStream();tmpOut = socket.getOutputStream();} catch (IOException e) {//  Log.e(TAG, "temp sockets not created", e);}mmInStream = tmpIn;mmOutStream = tmpOut;}public void run() {synchronized (BluetoothChatService.class) {byte[] buffer = new byte[1024 * 4];int bytes;while (mState == STATE_CONNECTED) {try {bytes = mmInStream.read(buffer);String data = new String(buffer, 0, bytes, "UTF-8");Message msg = Message.obtain();msg.what = Constants.MESSAGE_READ;msg.obj = data;mHandler.sendMessage(msg);} catch (IOException e) {connectionLost();BluetoothChatService.this.start();break;}}}}public void write(byte[] buffer) {try {mmOutStream.write(buffer);mmOutStream.flush();mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();} catch (IOException e) {//Log.e(TAG, "Exception during write", e);}}public void cancel() {try {mmSocket.close();} catch (IOException e) {//    Log.e(TAG, "close() of connect socket failed", e);}}}
}
/*** @desc ${BluetoothService 中使用的常量}*/
public class Constants {public static final int MESSAGE_STATE_CHANGE = 1;  //状态改变public static final int MESSAGE_READ = 2; //读取public static final int MESSAGE_WRITE = 3;   //写出public static final int MESSAGE_DEVICE_NAME = 4;  //设备 名称public static final int MESSAGE_TOAST = 5;  //连接失败public static final String DEVICE_NAME = "device_name";public static final String TOAST = "toast";}
 private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case Constants.MESSAGE_TOAST:String statu = msg.getData().getString(Constants.TOAST);showToast(statu + "");break;case Constants.MESSAGE_DEVICE_NAME:String string = msg.getData().getString(Constants.DEVICE_NAME);showToast("已连接设备" + string);break;case Constants.MESSAGE_READ: //读取接收数据break;case Constants.MESSAGE_WRITE: //发送端,正在发送的数据break;}}};private void iniData(){
省******
******
******略BluetoothChatService chatService = new BluetoothChatService(getActivity(), mHandler);Listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long        id) {// 判断当前是否还是正在搜索周边设备,如果是则暂停搜索if (mBluetoothAdapter.isDiscovering()) {mBluetoothAdapter.cancelDiscovery();}BluetoothDevice bluetoothDevice = bluetoothDevices.get(position);//客户端连接chatService.connect(bluetoothDevice);}});//如果要开启服务端直接调用以下语句
//chatService.start();}/*** 准备开始发送数据*/private void snedInfo() {if (chatService.getState() != BluetoothChatService.STATE_CONNECTED) {showToast("连接失败");return;}String toSendJson = new Gson().toJson(“Bean数据”);if (toSendJson.length() > 0) {byte[] send = new byte[0];try {send = toSendJson.getBytes();} catch (Exception e) {e.printStackTrace();}chatService.write(send);}}});}

以上就是我目前做的蓝牙传输功能了,先这样吧...然后再慢慢优化改进的。各种状态也都有了的,自己慢慢调整一下吧..

android 蓝牙之数据传输相关推荐

  1. Android 蓝牙开发模块详解 (含 demo)

    文章目录 1.简介 2.基本功能简介 1).设置权限 2)开启蓝牙 3).发现蓝牙设备 4.建立连接 5.交换数据 6.建立数据通信线程 6.实例练习 6.1.代码结构 6.2 . xml 文档 6. ...

  2. Android 蓝牙技术 实现终端间数据传输

    蓝牙技术在智能硬件方面有很多用武之地,今天我就为大家分享一下蓝牙技术在Android系统下的使用方法技巧.蓝牙是一种短距离的无线通信技术标准,蓝牙协议分为4层,即核心协议层.电缆替代协议层.电话控制协 ...

  3. Android蓝牙语音传输,数据传输

    需求:开发一个app将手机麦克风的语音数据实时发送给蓝牙音箱设备(耳机也可以),实现扩音的目的.也有单独数据传输的部分. 在网上找了很多,没有找到一个合适的demo,弄了几天终于弄出来了!下面把这个过 ...

  4. android蓝牙传输数据_Android的蓝牙数据传输

    android蓝牙传输数据 要开发通过蓝牙(BT)进行数据传输的Android应用程序,请从逻辑上从Android开发人员的蓝牙页面开始,该页面详细描述了所有必需的步骤:设备发现,配对,客户端/服务器 ...

  5. android蓝牙聊天设备,Android蓝牙开发——实现蓝牙聊天

    最近课上刚好需要做一个课程设计关于蓝牙的就挑选了个蓝牙聊天室,其实关键还是在于对蓝牙API的了解 一.蓝牙API 与蓝牙开发主要的相关类是以下四个 BluetoothAdapter 字面上则理解为蓝牙 ...

  6. 深入了解Android蓝牙Bluetooth——《基础篇》

    深入了解Android蓝牙Bluetooth--<基础篇> 什么是蓝牙?   也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的 ...

  7. ym——物联网入口之中的一个Android蓝牙4.0

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 假设还有同学不知道蓝牙4.0能够做什么请查看Android+蓝牙 4.0 将带来什么? ...

  8. android蓝牙历史

    经典蓝牙与低功耗蓝牙的区别 规范 作为一种通用的无线通信技术,规范自然是蓝牙技术的核心.蓝牙规范可分为两个层次,如图1所示: Paste_Image.png 图1 蓝牙规范的层次结构 由图1可知蓝牙规 ...

  9. 【转载】Android蓝牙自动配对Demo

    注:新版本安卓需增加权限 <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION ...

最新文章

  1. 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)...
  2. hibernate教程--持久化类状态
  3. angular 示例项目
  4. 电子计算机之父冯.诺依曼的主要贡献,冯•诺依曼的贡献有哪些?
  5. shell脚本给mysql创建表_shell脚本:实现MySQL创建数据库和删除数据库的脚本
  6. python输出玫瑰花_如何用python画玫瑰花
  7. 06.Qt菜单栏工具栏学习(一)
  8. 用实例的方式去理解storm的并发度
  9. Java项目上出现红色感叹号
  10. Leetcode-树
  11. openoffice转换pdf 异常问题查找处理 errorCode 525
  12. 多项式曲线,分段曲线,曲线参数化,平面曲线,插值方法的样条曲线
  13. 远程设备运维云平台软件与常规组态软件的区别(V1.1)?
  14. 32bit 天堂2脚本修改资料大全【客户端+服务端】
  15. 邮件传输协议 SMTP 、POP3 、IMAP 和 Exchange 比较及联系?
  16. 零电压开关(ZVS)电路原理与设计(整理)
  17. 剁馅机器人图片_河南大妈开店十年卖饺子, 剁馅用上机器人, 莲菜大肉馅还真好吃...
  18. 宏观经济学-大题资源整理
  19. 机器学习实践:非监督学习-8
  20. python人脸识别门禁系统_树莓派人脸识别门禁系统图文教程

热门文章

  1. SD卡启动第一篇 (SD卡分区)
  2. python 操作excle和word
  3. Redis集群--Cluster--故障转移的过程(原理)
  4. Spark RDD使用详解--RDD原理
  5. Chocolatey安装与常用命令
  6. 【ACM】算法题-阿尔法GO的缺陷(深度搜索解法)(C++)
  7. spark java文档中文版_spark api之一:Spark官方文档 - 中文翻译
  8. 【HTTP】10分钟带你快速了解HTTP中常见的状态码(内附大量实例)
  9. 那些让你惊掉下巴到肚皮上的python冷知识(二)
  10. Sherlock:具有后端选择的分布式锁