Android藍芽開發淺析

來源:互聯網
上載者:User

由於近期正在開發一個通過藍芽進行資料傳遞的模組,在參考了有關資料,並詳細閱讀了Android的官方文檔後,總結了Android中藍芽模組的使用。

1. 使用藍芽的響應許可權
[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) { } 
    } 


用戶端的實現 www.2cto.com
通過搜尋得到伺服器端的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) { } 
    } 



摘自 Crazy Programmer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.