簡體   English   中英

如何將應用程序連接到藍牙耳機

[英]How to connect an app to a Bluetooth headset

我正在嘗試將我的應用程序連接到藍牙耳機,但我收到了mProfileListener錯誤。 我不知道在哪里以及如何聲明mProfileListener 這是我的代碼:

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


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

   private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
       public void onServiceConnected(int profile, BluetoothProfile proxy) {
           if (profile == BluetoothProfile.HEADSET) {
               mBluetoothHeadset = (BluetoothHeadset) proxy;
               Log.d("TAGP","BLuetooth Headset: "+mBluetoothHeadset);
               Log.d("TAGP ","Proxy: "+proxy);
           }
       }
       public void onServiceDisconnected(int profile) {
           if (profile == BluetoothProfile.HEADSET) {
               mBluetoothHeadset = null;
           }
       }
   };

   // ... call functions on mBluetoothHeadset

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

}

您可以在類范圍中聲明mProfileListener實例,如下所示:

private BluetoothProfile.ServiceListener mProfileListener;

然后在你的“連接”藍牙堆棧方法中,你可以像你的代碼一樣初始化這個mProfileListener實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM