2015-03-03 94 views
0

我試圖將我的應用程序連接到藍牙耳機,但出現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); 

} 

回答

0

您可以如下聲明在類範圍內的mProfileListener實例:

private BluetoothProfile.ServiceListener mProfileListener; 

然後藍牙堆棧的「連接」的方法中,你可以inititalize這個mProfileListener實例就像你的代碼一樣。

相關問題