欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 蓝牙开发实例解析

程序员文章站 2024-03-05 23:44:07
在使用手机时,蓝牙通信给我们带来很多方便。那么在android手机中怎样进行蓝牙开发呢?本文以实例的方式讲解android蓝牙开发的知识。   &nb...

在使用手机时,蓝牙通信给我们带来很多方便。那么在android手机中怎样进行蓝牙开发呢?本文以实例的方式讲解android蓝牙开发的知识。

       1、使用蓝牙的响应权限

xml/html代码

<uses-permission android:name="android.permission.bluetooth"/>  
<uses-permission android:name="android.permission.bluetooth_admin"/> 

        2、配置本机蓝牙模块

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

bluetoothadapter adapter = bluetoothadapter.getdefaultadapter();  
  
//直接打开系统的蓝牙设置面板  
  
intent intent = new intent(bluetoothadapter.action_request_enable);  
  
startactivityforresult(intent, 0x1);  
  
//直接打开蓝牙  
  
adapter.enable();  
  
//关闭蓝牙  
  
adapter.disable();  
  
//打开本机的蓝牙发现功能(默认打开120秒,可以将时间最多延长至300秒)  
  
intent discoveryintent = new intent(bluetoothadapter.action_request_discoverable);  
  
discoverableintent.putextra(bluetoothadapter.extra_discoverable_duration, 300);//设置持续时间(最多300秒) 

         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来接收响应的广播,以便实现某些功能。

// 创建一个接收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)。

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()方法之前,应当确定当前没有搜索设备,否则连接会变得非常慢并且容易失败。

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) { }  
  }  
} 

       5、连接管理(数据通信)

       分别通过bluetoothsocket的getinputstream()和getoutputstream()方法获取inputstream和outputstream。

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

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

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) { }  
  
 }  
  
}  

 以上就android 蓝牙的开发简单示例代码,后续继续整理相关资料,谢谢大家对本站的支持!