关于android:Android实例-android-蓝牙开发浅析

  1. 应用蓝牙的响应权限

    <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); // 不要忘了之后解除绑定   
    
  3. 蓝牙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) { }    
    }     
} 

4.连贯治理(数据通信)
别离通过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) { }    
   
    }    
   
}    

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理