1. 程式人生 > >四 bluetooth api基本使用

四 bluetooth api基本使用

建立藍芽連線所需的類和介面

對於上層app來說,呼叫相應介面就可以完成需求。
常用步驟有:

  • 設定藍芽
  • 查詢區域性區域內的配對裝置或可用裝置、
  • 連線裝置以及在裝置之間傳輸資料。
  • 使用配置檔案

  • BluetoothAdapter
    表示本地藍芽介面卡(藍芽無線裝置)。 BluetoothAdapter 是所有藍芽互動的入口點。 利用它可以發現其他藍芽裝置,查詢繫結(配對)裝置的列表,使用已知的 MAC 地址例項化 BluetoothDevice,以及建立 BluetoothServerSocket 以偵聽來自其他裝置的通訊。
  • BluetoothDevice
    表示遠端藍芽裝置。利用它可以通過 BluetoothSocket 請求與某個遠端裝置建立連線,或查詢有關該裝置的資訊,例如裝置的名稱、地址、類和繫結狀態等。
  • BluetoothSocket
    表示藍芽套接字介面(與 TCP Socket 相似)。這是允許應用通過 InputStream 和 OutputStream 與其他藍芽裝置交換資料的連線點。
  • BluetoothServerSocket
    表示用於偵聽傳入請求的開放伺服器套接字(類似於 TCP ServerSocket)。 要連線兩臺 Android 裝置,其中一臺裝置必須使用此類開放一個伺服器套接字。 當一臺遠端藍芽裝置向此裝置發出連線請求時, BluetoothServerSocket 將會在接受連線後返回已連線的 BluetoothSocket。
  • BluetoothClass
    描述藍芽裝置的一般特徵和功能。 這是一組只讀屬性,用於定義裝置的主要和次要裝置類及其服務。 不過,它不能可靠地描述裝置支援的所有藍芽配置檔案和服務,而是適合作為裝置型別提示。
  • BluetoothProfile
    表示藍芽配置檔案的介面。 藍芽配置檔案是適用於裝置間藍芽通訊的無線介面規範。 擴音配置檔案便是一個示例。 如需瞭解有關配置檔案的詳細討論,請參閱使用配置檔案
  • BluetoothHeadset
    提供藍芽耳機支援,以便與手機配合使用。 其中包括藍芽耳機和擴音(1.5 版)配置檔案。
  • BluetoothA2dp
    定義高質量音訊如何通過藍芽連線和流式傳輸,從一臺裝置傳輸到另一臺裝置。“A2DP”代表高階音訊分發配置檔案。
  • BluetoothHealth
    表示用於控制藍芽服務的健康裝置配置檔案代理。
  • BluetoothHealthCallback
    用於實現 BluetoothHealth 回撥的抽象類。您必須擴充套件此類並實現回撥方法,以接收關於應用註冊狀態和藍芽通道狀態變化的更新內容。
  • BluetoothHealthAppConfiguration
    表示第三方藍芽健康應用註冊的應用配置,以便與遠端藍芽健康裝置通訊。
  • BluetoothProfile.ServiceListener
    在 BluetoothProfile IPC 客戶端連線到服務(即,執行特定配置檔案的內部服務)或斷開服務連線時向其傳送通知的介面。

設定藍芽

  1. 獲取 BluetoothAdapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
}
  1. 啟用藍芽
if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

查詢裝置

查詢配對的裝置

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());
    }
}

發現裝置

開始發現裝置,只需呼叫 startDiscovery()。

發現裝置時如何註冊以處理廣播
// 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

連線裝置

要在兩臺裝置上的應用之間建立連線,必須同時實現伺服器端和客戶端機制,因為其中一臺裝置必須開放伺服器套接字,而另一臺裝置必須發起連線(使用伺服器裝置的 MAC 地址發起連線)。 當伺服器和客戶端在同一 RFCOMM 通道上分別擁有已連線的 BluetoothSocket 時,二者將被視為彼此連線。

github中由聊天的示例

https://github.com/googlesamples/android-BluetoothAdvertisements/#readme

使用配置檔案

如何連線到 BluetoothHeadset 代理物件,以便能夠控制耳機配置檔案:

BluetoothHeadset mBluetoothHeadset;

// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);

private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = (BluetoothHeadset) proxy;
        }
    }
    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = null;
        }
    }
};

// ... call functions on mBluetoothHeadset

// Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);