android蓝牙4.0开发教程

RF/无线

1773人已加入

描述

  蓝牙我们应该很早就听过,最常见的就是原来我们偶尔通过手机上的蓝牙来传输文件。貌似在低功耗蓝牙出现之前,蓝牙我们使用的并不多,蓝牙的产品貌似也不是很多。2010年6月30号蓝牙技术联盟推出了低功耗蓝牙,经过几年的发展,市场上基于低功耗系列的硬件产品越来越多,开发硬件,软件的厂商,公司越来越多。

  蓝牙发展至今经历了8个版本的更新。1.1、1.2、2.0、2.1、3.0、4.0、4.1、4.2。那么在1.x~3.0之间的我们称之为传统蓝牙,4.x开始的蓝牙我们称之为低功耗蓝牙也就是蓝牙ble,当然4.x版本的蓝牙也是向下兼容的。android手机必须系统版本4.3及以上才支持BLE API。低功耗蓝牙较传统蓝牙,传输速度更快,覆盖范围更广,安全性更高,延迟更短,耗电极低等等优点。这也是为什么近年来智能穿戴的东西越来越多,越来越火。还有传统蓝牙与低功耗蓝牙通信方式也有所不同,传统的一般通过socket方式,而低功耗蓝牙是通过Gatt协议来实现。若是之前没做过传统蓝牙开发,也是可以直接上手低功耗蓝牙开发的。因为它们在通信协议上都有所改变,关联不大。当然有兴趣的可以去下载些传统蓝牙开发的demo看看,在看看低功耗蓝牙的demo。两者的不同之处自然容易看出来。好了,我们下面开始讲低功耗蓝牙开发吧。低功耗蓝牙也叫BLE,下面都称之为BLE。

  BLE分为三部分:Service,Characteristic,Descriptor。这三部分都用UUID作为唯一标识符。UUID为这种格式:0000ffe1-0000-1000-8000-00805f9b34fb。比如有3个Service,那么就有三个不同的UUID与Service对应。这些UUID都写在硬件里,我们通过BLE提供的API可以读取到。

  一个BLE终端可以包含多个Service, 一个Service可以包含多个Characteristic,一个Characteristic包含一个value和多个Descriptor,一个Descriptor包含一个Value。Characteristic是比较重要的,是手机与BLE终端交换数据的关键,读取设置数据等操作都是操作Characteristic的相关属性。

  在很多方面,蓝牙是一种能够发送或接受两个不同的设备之间传输的数据。 Android平台包含了蓝牙框架,使设备以无线方式与其他蓝牙设备进行数据交换的支持。

  Android提供蓝牙API来执行这些不同的操作。

  扫描其他蓝牙设备

  获取配对设备列表

  连接到通过服务发现其他设备

  Android提供BluetoothAdapter类蓝牙通信。通过调用创建的对象的静态方法getDefaultAdapter()。其语法如下给出。

  private BluetoothAdapter BA;

  BA = BluetoothAdapter.getDefaultAdapter();

  为了使用设备的蓝牙,调用下列蓝牙ACTION_REQUEST_ENABLE的意图。其语法如下:

  Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

  startActivityForResult(turnOn, 0);

  开发过Android 蓝牙4.0 BLE的同学都知道,Android的蓝牙开发有非常多的坑,其中不同机型之间的兼容性就是一个很令人头疼的问题,很多问题究其原因是在手机端和智能设备之间发送请求指令和回调时,其方式是异步请求的,即请求完立即结束,等待回调,而回调又在不同的线程中,因此当交互比较频繁并且之间有依赖关系的时候,很容易错误。很多Android手机针对这种情况在底层实现了对蓝牙请求的同步,即有一个requestQueue,使得一个蓝牙请求能够在上一个蓝牙请求的reponse结束之后再发送,保证了顺序执行和正确性。但是有些Android手机没有提供这样的requestQueue,使得错误率大大增加,往往这些问题还不容易发现,从而造成了Android蓝牙开发的兼容性问题。所以,要想做好兼容性,我们必须在app端的应用层就应该提供一套完整的requestQueue请求同步策略。

  对于之前没接触过蓝牙开发,现在手上又有个蓝牙BLE项目需要做的人,先看下这些概念还是很重要的。因为我之前就是这样,之前没有接触过蓝牙方面的开发,然后来了个蓝牙的项目,于是就到网上百度了一番,于是有点茫然,产生了几点疑惑:

  1:发现蓝牙有传统蓝牙和低功耗蓝牙(ble)之分。那么什么是传统蓝牙,什么又是低功耗蓝牙?之前又没做过蓝牙开发,我该用哪种方式去开发我这个项目?用最新的 方式的话,传统方式蓝牙开发我是不是该要先了解?

  2:蓝牙到底有哪些版本?哪些版本称为传统蓝牙?哪些版本称为低功耗蓝牙?

  3:传统蓝牙和低功耗蓝牙有什么区别?为什么低功耗蓝牙的出现使得智能能穿戴越来越流行?

  下面详细讲解Android蓝牙模块的使用方法。

  1、使用蓝牙的响应权限

  XML/HTML代码

  《uses-permission android:name=“android.permission.BLUETOOTH” /》

  《uses-permission android:name=“android.permission.BLUETOOTH_ADMIN” /》

  2、配置本机蓝牙模块

  在这里首先要了解对蓝牙操作一个核心类BluetoothAdapter。

  Java代码

  BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

  //直接打开系统的蓝牙设置面板

  Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

  startActivityForResult(intent, 0x1);

  //直接打开蓝牙

  adapter.enable();

  //关闭蓝牙

  adapter.disable();

  //打开本机的蓝牙发现功能(默认打开120秒,可以将时间最多延长至300秒)

  discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);//设置持续时间(最多300秒)Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

  3、搜索蓝牙设备

  使用BluetoothAdapter的startDiscovery()方法来搜索蓝牙设备。

  startDiscovery()方法是一个异步方法,调用后会立即返回。该方法会进行对其他蓝牙设备的搜索,该过程会持续12秒。该方法调用后,搜索过程实际上是在一个System Service中进行的,所以可以调用cancelDiscovery()方法来停止搜索(该方法可以在未执行discovery请求时调用)。

  请求Discovery后,系统开始搜索蓝牙设备,在这个过程中,系统会发送以下三个广播:

  ACTION_DISCOVERY_START:开始搜索

  ACTION_DISCOVERY_FINISHED:搜索结束

  ACTION_FOUND:找到设备,这个Intent中包含两个extra fields:EXTRA_DEVICE和EXTRA_CLASS,分别包含BluetooDevice和BluetoothClass。

  我们可以自己注册相应的BroadcastReceiver来接收响应的广播,以便实现某些功能。

  Java代码

  // 创建一个接收ACTION_FOUND广播的BroadcastReceiver

  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

  public void onReceive(Context context, Intent intent) {

  String action = intent.getAction();

  // 发现设备

  if (BluetoothDevice.ACTION_FOUND.equals(action)) {

  // 从Intent中获取设备对象

  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

  // 将设备名称和地址放入array adapter,以便在ListView中显示

  mArrayAdapter.add(device.getName() + “\n” + device.getAddress());

  }

  }

  };

  // 注册BroadcastReceiver

  IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

  registerReceiver(mReceiver, filter); // 不要忘了之后解除绑定
 

  4、蓝牙Socket通信

  如果打算建议两个蓝牙设备之间的连接,则必须实现服务器端与客户端的机制。当两个设备在同一个RFCOMM channel下分别拥有一个连接的BluetoothSocket,这两个设备才可以说是建立了连接。

  服务器设备与客户端设备获取BluetoothSocket的途径是不同的。服务器设备是通过accepted一个incoming connection来获取的,而客户端设备则是通过打开一个到服务器的RFCOMM channel来获取的。

  服务器端的实现

  通过调用BluetoothAdapter的listenUsingRfcommWithServiceRecord(String, UUID)方法来获取BluetoothServerSocket(UUID用于客户端与服务器端之间的配对)。

  调用BluetoothServerSocket的accept()方法监听连接请求,如果收到请求,则返回一个BluetoothSocket实例(此方法为block方法,应置于新线程中)。

  如果不想在accept其他的连接,则调用BluetoothServerSocket的close()方法释放资源(调用该方法后,之前获得的BluetoothSocket实例并没有close。但由于RFCOMM一个时刻只允许在一条channel中有一个连接,则一般在accept一个连接后,便close掉BluetoothServerSocket)。

  Java代码

  private class AcceptThread extends Thread {

  private final BluetoothServerSocket mmServerSocket;

  public AcceptThread() {

  // Use a temporary object that is later assigned to mmServerSocket,

  // because mmServerSocket is final

  BluetoothServerSocket tmp = null;

  try {

  // MY_UUID is the app‘s UUID string, also used by the client code

  tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);

  } catch (IOException e) { }

  mmServerSocket = tmp;

  }

  public void run() {

  BluetoothSocket socket = null;

  // Keep listening until exception occurs or a socket is returned

  while (true) {

  try {

  socket = mmServerSocket.accept();

  } catch (IOException e) {

  break;

  }

  // If a connection was accepted

  if (socket != null) {

  // Do work to manage the connection (in a separate thread)

  manageConnectedSocket(socket);

  mmServerSocket.close();

  break;

  }

  }

  }

  /** Will cancel the listening socket, and cause the thread to finish */

  public void cancel() {

  try {

  mmServerSocket.close();

  } catch (IOException e) { }

  }

  }

  客户端的实现

  通过搜索得到服务器端的BluetoothService。

  调用BluetoothService的listenUsingRfcommWithServiceRecord(String, UUID)方法获取BluetoothSocket(该UUID应该同于服务器端的UUID)。

  调用BluetoothSocket的connect()方法(该方法为block方法),如果UUID同服务器端的UUID匹配,并且连接被服务器端accept,则connect()方法返回。

  注意:在调用connect()方法之前,应当确定当前没有搜索设备,否则连接会变得非常慢并且容易失败。

  Java代码

  private class ConnectThread extends Thread {

  private final BluetoothSocket mmSocket;

  private final BluetoothDevice mmDevice;

  public ConnectThread(BluetoothDevice device) {

  // Use a temporary object that is later assigned to mmSocket,

  // because mmSocket is final

  BluetoothSocket tmp = null;

  mmDevice = device;

  // Get a BluetoothSocket to connect with the given BluetoothDevice

  try {

  // MY_UUID is the app’s UUID string, also used by the server code

  tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

  } catch (IOException e) { }

  mmSocket = tmp;

  }

  public void run() {

  // Cancel discovery because it will slow down the connection

  mBluetoothAdapter.cancelDiscovery();

  try {

  // Connect the device through the socket. This will block

  // until it succeeds or throws an exception

  mmSocket.connect();

  } catch (IOException connectException) {

  // Unable to connect; close the socket and get out

  try {

  mmSocket.close();

  } catch (IOException closeException) { }

  return;

  }

  // Do work to manage the connection (in a separate thread)

  manageConnectedSocket(mmSocket);

  }

  /** Will cancel an in-progress connection, and close the socket */

  public void cancel() {

  try {

  mmSocket.close();

  } catch (IOException e) { }

  }

  }

  连接管理(数据通信)

  分别通过BluetoothSocket的getInputStream()和getOutputStream()方法获取InputStream和OutputStream。

  使用read(bytes[])和write(bytes[])方法分别进行读写操作。

  注意:read(bytes[])方法会一直block,知道从流中读取到信息,而write(bytes[])方法并不是经常的block(比如在另一设备没有及时read或者中间缓冲区已满的情况下,write方法会block)。

  Java代码

  private class ConnectedThread extends Thread {

  private final BluetoothSocket mmSocket;

  private final InputStream mmInStream;

  private final OutputStream mmOutStream;

  public ConnectedThread(BluetoothSocket socket) {

  mmSocket = socket;

  InputStream tmpIn = null;

  OutputStream tmpOut = null;

  // Get the input and output streams, using temp objects because

  // member streams are final

  try {

  tmpIn = socket.getInputStream();

  tmpOut = socket.getOutputStream();

  } catch (IOException e) { }

  mmInStream = tmpIn;

  mmOutStream = tmpOut;

  }

  public void run() {

  byte[] buffer = new byte[1024]; // buffer store for the stream

  int bytes; // bytes returned from read()

  // Keep listening to the InputStream until an exception occurs

  while (true) {

  try {

  // Read from the InputStream

  bytes = mmInStream.read(buffer);

  // Send the obtained bytes to the UI Activity

  mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)

  .sendToTarget();

  } catch (IOException e) {

  break;

  }

  }

  }

  /* Call this from the main Activity to send data to the remote device */

  public void write(byte[] bytes) {

  try {

  mmOutStream.write(bytes);

  } catch (IOException e) { }

  }

  /* Call this from the main Activity to shutdown the connection */

  public void cancel() {

  try {

  mmSocket.close();

  } catch (IOException e) { }

  }

  }

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分