Android蓝牙操作

http://zhouyunan2010.iteye.com/blog/1186021

蓝牙是一种支持设备短距离传输数据的无线技术。android在2.0以后提供了这方面的支持。 
从查找蓝牙设备到能够相互通信要经过几个基本步骤(本机做为服务器): 
1.设置权限 
在manifest中配置 
Xml代码   收藏代码
  1. <uses-permission android:name="android.permission.BLUETOOTH"/>  
  2. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>  


2.启动蓝牙 
首先要查看本机是否支持蓝牙,获取BluetoothAdapter蓝牙适配器对象 
Java代码   收藏代码
  1. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
  2. if(mBluetoothAdapter == null){  
  3.         //表明此手机不支持蓝牙  
  4.         return;  
  5. }  
  6. if(!mBluetoothAdapter.isEnabled()){ //蓝牙未开启,则开启蓝牙  
  7.             Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  8.             startActivityForResult(enableIntent, REQUEST_ENABLE_BT);  
  9. }  
  10. //......  
  11. public void onActivityResult(int requestCode, int resultCode, Intent data){  
  12.        if(requestCode == REQUEST_ENABLE_BT){  
  13.               if(requestCode == RESULT_OK){  
  14.                    //蓝牙已经开启   
  15.               }  
  16.        }  
  17. }  


3。发现蓝牙设备 
这里可以细分为几个方面 
(1)使本机蓝牙处于可见(即处于易被搜索到状态),便于其他设备发现本机蓝牙 
Java代码   收藏代码
  1. //使本机蓝牙在300秒内可被搜索  
  2. private void ensureDiscoverable() {  
  3.         if (mBluetoothAdapter.getScanMode() !=  
  4.             BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {  
  5.             Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  6.             discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);  
  7.             startActivity(discoverableIntent);  
  8.         }  
  9. }  

(2)查找已经配对的蓝牙设备,即以前已经配对过的设备 
Java代码   收藏代码
  1. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();  
  2. if (pairedDevices.size() > 0) {  
  3.     findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);  
  4.     for (BluetoothDevice device : pairedDevices) {  
  5.         //device.getName() +" "+ device.getAddress());  
  6.     }  
  7. else {  
  8.     mPairedDevicesArrayAdapter.add("没有找到已匹对的设备");  
  9. }  

(3)通过mBluetoothAdapter.startDiscovery();搜索设备,要获得此搜索的结果需要注册 
一个BroadcastReceiver来获取。先注册再获取信息,然后处理 
Java代码   收藏代码
  1. //注册,当一个设备被发现时调用onReceive  
  2. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
  3.         this.registerReceiver(mReceiver, filter);  
  4.   
  5. //当搜索结束后调用onReceive  
  6. filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);  
  7.         this.registerReceiver(mReceiver, filter);  
  8. //.......  
  9. private BroadcastReceiver mReceiver = new BroadcastReceiver() {  
  10.         @Override  
  11.         public void onReceive(Context context, Intent intent) {  
  12.             String action = intent.getAction();  
  13.             if(BluetoothDevice.ACTION_FOUND.equals(action)){  
  14.                  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  15.                   // 已经配对的则跳过  
  16.                  if (device.getBondState() != BluetoothDevice.BOND_BONDED) {  
  17.                       mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());  //保存设备地址与名字  
  18.                  }  
  19.             }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {  //搜索结束  
  20.                 if (mNewDevicesArrayAdapter.getCount() == 0) {  
  21.                     mNewDevicesArrayAdapter.add("没有搜索到设备");  
  22.                 }  
  23.             }  
  24.   
  25.         }  
  26. };  


4.建立连接 
查找到设备 后,则需要建立本机与其他设备之间的连接。 
一般用本机搜索其他蓝牙设备时,本机可以作为一个服务端,接收其他设备的连接。 
启动一个服务器端的线程,死循环等待客户端的连接,这与ServerSocket极为相似。 
这个线程在准备连接之前启动 
Java代码   收藏代码
  1. //UUID可以看做一个端口号  
  2. private static final UUID MY_UUID =  
  3.         UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");  
  4.    //像一个服务器一样时刻监听是否有连接建立  
  5.     private class AcceptThread extends Thread{  
  6.         private BluetoothServerSocket serverSocket;  
  7.           
  8.         public AcceptThread(boolean secure){  
  9.             BluetoothServerSocket temp = null;  
  10.             try {  
  11.                 temp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(  
  12.                             NAME_INSECURE, MY_UUID);  
  13.             } catch (IOException e) {  
  14.                   Log.e("app""listen() failed", e);  
  15.             }  
  16.             serverSocket = temp;  
  17.         }  
  18.           
  19.         public void run(){  
  20.             BluetoothSocket socket=null;  
  21.             while(true){  
  22.                 try {  
  23.                     socket = serverSocket.accept();  
  24.                 } catch (IOException e) {  
  25.                      Log.e("app""accept() failed", e);  
  26.                      break;  
  27.                 }  
  28.             }  
  29.             if(socket!=null){  
  30.                 //此时可以新建一个数据交换线程,把此socket传进去  
  31.             }  
  32.         }  
  33.           
  34.         //取消监听  
  35.         public void cancel(){     
  36.             try {  
  37.                 serverSocket.close();  
  38.             } catch (IOException e) {  
  39.                 Log.e("app""Socket Type" + socketType + "close() of server failed", e);  
  40.             }  
  41.         }  
  42.   
  43. }  


搜索到设备后可以获取设备的地址,通过此地址获取一个BluetoothDeviced对象,可以看做客户端,通过此对象device.createRfcommSocketToServiceRecord(MY_UUID);同一个UUID可与服务器建立连接获取另一个socket对象,由此服务端与客户端各有一个socket对象,此时 
他们可以互相交换数据了。 
创立客户端socket可建立线程 
Java代码   收藏代码
  1.  //另一个设备去连接本机,相当于客户端  
  2.  private class ConnectThread extends Thread{  
  3.     private BluetoothSocket socket;  
  4.     private BluetoothDevice device;  
  5.     public ConnectThread(BluetoothDevice device,boolean secure){  
  6.         this.device = device;  
  7.         BluetoothSocket tmp = null;  
  8.         try {  
  9.     tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);  
  10. catch (IOException e) {  
  11.      Log.e("app""create() failed", e);  
  12. }  
  13.     }  
  14.       
  15.     public void run(){  
  16.         mBluetoothAdapter.cancelDiscovery();    //取消设备查找  
  17.         try {  
  18.     socket.connect();  
  19. catch (IOException e) {  
  20.     try {  
  21.         socket.close();  
  22.     } catch (IOException e1) {  
  23.          Log.e("app""unable to close() "+  
  24.                           " socket during connection failure", e1);  
  25.     }  
  26.     connetionFailed();  //连接失败  
  27.     return;  
  28. }  
  29.       //此时可以新建一个数据交换线程,把此socket传进去  
  30.     }  
  31.       
  32.       public void cancel() {  
  33.            try {  
  34.                socket.close();  
  35.            } catch (IOException e) {  
  36.                Log.e("app""close() of connect  socket failed", e);  
  37.            }  
  38.        }  
  39.  }  


5.建立数据通信线程,进行读取数据 
Java代码   收藏代码
  1. //建立连接后,进行数据通信的线程  
  2.     private class ConnectedThread extends Thread{  
  3.         private BluetoothSocket socket;  
  4.         private InputStream inStream;  
  5.         private OutputStream outStream;  
  6.           
  7.         public ConnectedThread(BluetoothSocket socket){  
  8.               
  9.             this.socket = socket;  
  10.             try {  
  11.                 //获得输入输出流  
  12.                 inStream = socket.getInputStream();  
  13.                 outStream = socket.getOutputStream();  
  14.             } catch (IOException e) {  
  15.                 Log.e("app""temp sockets not created", e);  
  16.             }  
  17.         }  
  18.           
  19.         public void run(){  
  20.             byte[] buff = new byte[1024];  
  21.             int len=0;  
  22.             //读数据需不断监听,写不需要  
  23.             while(true){  
  24.                 try {  
  25.                     len = inStream.read(buff);  
  26.                     //把读取到的数据发送给UI进行显示  
  27.                     Message msg = handler.obtainMessage(BluetoothChat.MESSAGE_READ,  
  28.                             len, -1, buff);  
  29.                     msg.sendToTarget();  
  30.                 } catch (IOException e) {  
  31.                     Log.e("app""disconnected", e);  
  32.                     connectionLost();   //失去连接  
  33.                     start();    //重新启动服务器  
  34.                     break;  
  35.                 }  
  36.             }  
  37.         }  
  38.           
  39.           
  40.         public void write(byte[] buffer) {  
  41.             try {  
  42.                 outStream.write(buffer);  
  43.   
  44.                 // Share the sent message back to the UI Activity  
  45.                 handler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)  
  46.                         .sendToTarget();  
  47.             } catch (IOException e) {  
  48.                 Log.e("app""Exception during write", e);  
  49.             }  
  50.         }  
  51.   
  52.         public void cancel() {  
  53.             try {  
  54.                 socket.close();  
  55.             } catch (IOException e) {  
  56.                 Log.e("app""close() of connect socket failed", e);  
  57.             }  
  58.         }  
  59.     }  


到这里,蓝牙通信的基本操作已经全部完成。 

http://blog.youkuaiyun.com/pwei007/article/details/6015907
2010-11-17 19:56  4062人阅读  评论(1)  收藏  举报

Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输。

本文档描述了怎样利用android平台提供的蓝牙API去实现蓝牙设备之间的通信,蓝牙设备之间的通信主要包括了四个步骤:设置蓝牙设备、寻找局域网内可能或者匹配的设备、连接设备和设备之间的数据传输。以下是建立蓝牙连接的所需要的一些基本类:

  BluetoothAdapter类:代表了一个本地的蓝牙适配器。他是所有蓝牙交互的的入口点。利用它你可以发现其他蓝牙设备,查询绑定了的设备,使用已知的MAC地址实例化一个蓝牙设备和建立一个BluetoothServerSocket(作为服务器端)来监听来自其他设备的连接。

  BluetoothDevice类:代表了一个远端的蓝牙设备,使用它请求远端蓝牙设备连接或者获取远端蓝牙设备的名称、地址、种类和绑定状态。(其信息是封装在bluetoothsocket中)。

  Bluetoothsocket类:代表了一个蓝牙套接字的接口(类似于tcp中的套接字),他是应用程序通过输入、输出流与其他蓝牙设备通信的连接点。

  Blueboothserversocket类:代表打开服务连接来监听可能到来的连接请求(属于server端),为了连接两个蓝牙设备必须有一个设备作为服务器打开一个服务套接字。当远端设备发起连接连接请求的时候,并且已经连接到了的时候,Blueboothserversocket类将会返回一个bluetoothsocket

  Bluetoothclass类:描述了一个蓝牙设备的一般特点和能力。他的只读属性集定义了设备的主、次设备类和一些相关服务。然而,他并没有准确的描述所有该设备所支持的蓝牙文件和服务,而是作为对设备种类来说的一个小小暗示。

下面说说具体的编程实现:

必须确定你的设备支持蓝牙,并保证他可以用。如果你的设备支持蓝牙,将它使能。当然,有两种方法,一种是在你的系统设置里开启蓝牙,另外一中是在你的应用程序里启动蓝牙功能,第一种方法就不讲了,具体讲一个第二种方法:

 首先通过调用静态方法getDefaultAdapter()获取蓝牙适配器bluetoothadapter,以后你就可以使用该对象了。如果返回为空,the story is over

 EgBluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    
// Device does not support Bluetooth
}

其次,调用isEnabled()来查询当前蓝牙设备的状态,如果返回为false,则表示蓝牙设备没有开启,接下来你需要封装一个ACTION_REQUEST_ENABLE请求到intent里面,调用startActivityForResult()方法使能蓝牙设备,例如:

if (!mBluetoothAdapter.isEnabled()) {
             
Intent enableBtIntent = newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult
(enableBtIntent, REQUEST_ENABLE_BT);
}

 

至此,如不出意外,恭喜你的蓝牙设备已经开启了,接下来需要查找周边可能存在的蓝牙设备了。

查找设备:

  使用bluetoothadapter类里的方法,你可以查找远端设备(不过蓝牙查找的范围好像是在十米以内吧)或者查询在你手机上已经匹配(或者说绑定)的其他手机了。当然需要确定对方蓝牙设备已经开启或者已经开启了“被发现使能“功能(对方设备是可以被发现的是你能够发起连接的前提条件)。如果该设备是可以被发现的,会反馈回来一些对方的设备信息,比如名字、MAC地址等,利用这些信息,你的设备就可以选择去向对方初始化一个连接。

如果你是第一次与该设备连接,那么一个配对的请求就会自动的显示给用户。当设备配对好之后,他的一些基本信息(主要是名字和MAC)被保存下来并可以使用蓝牙的API来读取。使用已知的MAC地址就可以对远端的蓝牙设备发起连接请求。

  匹配好的设备和连接上的设备的不同点:匹配好只是说明对方设备发现了你的存在,并拥有一个共同的识别码,并且可以连接。连接上:表示当前设备共享一个RFCOMM信道并且两者之间可以交换数据。也就是是说蓝牙设备在建立RFCOMM信道之前,必须是已经配对好了的。

 怎么查询匹配好的设备:

 在建立连接之前你必须先查询配对好了的蓝牙设备集(你周围的蓝牙设备可能不止一个),以便你选取哪一个设备进行通信,例如你可以你可以查询所有配对的蓝牙设备,并使用一个数组适配器将其打印显示出来:

  Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    
// Loop through paired devices
    
for (BluetoothDevice device : pairedDevices) {
        
// Add the name and address to an array adapter to show in a ListView
        mArrayAdapter
.add(device.getName() + "/n" + device.getAddress());
    
}

建立一个蓝牙连接只需要MAC地址就已经足够了。

扫描设备:

   扫描设备,只需要简单的调用startDiscovery()方法,这个扫描的过程大概持续是12秒,应用程序为了ACTION_FOUND动作需要注册一个BroadcastReceiver来接受设备扫描到的信息。对于每一个设备,系统都会广播ACTION_FOUND动作。例如:

   // Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
public void onReceive(Context context, Intent intent) {
        
String action = intent.getAction();
        
// When discovery finds a device
        
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            
// Get the BluetoothDevice object from the Intent
            
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            
// Add the name and address to an array adapter to show in a ListView
            mArrayAdapter
.add(device.getName() + "/n" + device.getAddress());
        
}
    
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver
(mReceiver, filter); // Don't forget to unregister during onDestroy

注意:扫描的过程是一个很耗费资源的过程,一旦你找到你需要的设备之后,在发起连接请求之前,确保你的程序调用cancelDiscovery()方法停止扫描。显然,如果你已经连接上一个设备,启动扫描会减少你的通信带宽。

  使能被发现:Enabling discoverability

  如果你想使你的设备能够被其他设备发现,将ACTION_REQUEST_DISCOVERABLE动作封装在intent中并调用startActivityForResult(Intent, int)方法就可以了。他将在不使你应用程序退出的情况下使你的设备能够被发现。缺省情况下的使能时间是120秒,当然你可以可以通过添加EXTRA_DISCOVERABLE_DURATION字段来改变使能时间(最大不超过300秒,这是出于对你设备上的信息安全考虑)。例如:

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent
.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity
(discoverableIntent);

运行该段代码之后,系统会弹出一个对话框来提示你启动设备使能被发现(次过程中如果你的蓝牙功能没有开启,系统会帮你开启),并且如果你准备对该远端设备发现一个连接,你不需要开启使能设备被发现功能,以为该功能只是在你的应用程序作为服务器端的时候才需要。

连接设备:

  在你的应用程序中,想建立两个蓝牙设备之间的连接,你必须实现客户端和服务器端的代码(因为任何一个设备都必须可以作为服务端或者客户端)。一个开启服务来监听,一个发起连接请求(使用服务器端设备的MAC地址)。当他们都拥有一个蓝牙套接字在同一RFECOMM信道上的时候,可以认为他们之间已经连接上了。服务端和客户端通过不同的方式或其他们的蓝牙套接字。当一个连接监听到的时候,服务端获取到蓝牙套接字。当客户可打开一个FRCOMM信道给服务器端的时候,客户端获取到蓝牙套接字。

 注意:在此过程中,如果两个蓝牙设备还没有配对好的,android系统会通过一个通知或者对话框的形式来通知用户。RFCOMM连接请求会在用户选择之前阻塞。如下图:

服务端的连接:

 当你想要连接两台设备时,一个必须作为服务端(通过持有一个打开的bluetoothserversocket),目的是监听外来连接请求,当监听到以后提供一个连接上的bluetoothsocket给客户端,当客户端从bluetoothserversocket得到bluetoothsocket以后就可以销毁bluetoothserversocket,除非你还想监听更多的连接请求。

  建立服务套接字和监听连接的基本步骤:

 首先通过调用listenUsingRfcommWithServiceRecord(String, UUID)方法来获取bluetoothserversocket对象,参数string代表了该服务的名称,UUID代表了和客户端连接的一个标识(128位格式的字符串ID,相当于pin码),UUID必须双方匹配才可以建立连接。其次调用accept()方法来监听可能到来的连接请求,当监听到以后,返回一个连接上的蓝牙套接字bluetoothsocket。最后,在监听到一个连接以后,需要调用close()方法来关闭监听程序。(一般蓝牙设备之间是点对点的传输)

注意:accept()方法不应该放在主Acitvity里面,因为他是一种阻塞调用(在没有监听到连接请求之间程序就一直停在那里)。解决方法是新建一个线程来管理。例如:

 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 
= mAdapter.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) { }
    
}
}

客户端的连接:

  为了初始化一个与远端设备的连接,需要先获取代表该设备的一个bluetoothdevice对象。通过bluetoothdevice对象来获取bluetoothsocket并初始化连接:

 具体步骤:

  使用bluetoothdevice对象里的方法createRfcommSocketToServiceRecord(UUID)来获取bluetoothsocketUUID就是匹配码。然后,调用connect()方法来。如果远端设备接收了该连接,他们将在通信过程中共享RFFCOMM信道,并且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
        mAdapter
.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) { }
    
}

 注意:conncet()方法也是阻塞调用,一般建立一个独立的线程中来调用该方法。在设备discover过程中不应该发起连接connect(),这样会明显减慢速度以至于连接失败。且数据传输完成只有调用close()方法来关闭连接,这样可以节省系统内部资源。

管理连接(主要涉及数据的传输):

 当设备连接上以后,每个设备都拥有各自的bluetoothsocket。现在你就可以实现设备之间数据的共享了。

1.                                首先通过调用getInputStream()getOutputStream()方法来获取输入输出流。然后通过调用read(byte[]) 和 write(byte[]).方法来读取或者写数据。

2.                               实现细节:以为读取和写操作都是阻塞调用,需要建立一个专用现成来管理。

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

分享到: 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值