1

ほぼ2か月間間違った道を進んだ後、私は自分の間違いが何であるかを知りました。今、私は答えを見つけることができない新しい問題のペースを調整しています:ヘッドセットに接続しようとしているときにこの機能を使用する:

   mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
    final 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;
             }
       }
    };

mBluetoothHeadsetオブジェクトを初期化できません。何らかの理由で、デバッガーがonServiceConnected関数にステップインしません。

どんな助けでもありがたいです...本当に1つのシャイが必要です

詳細:Androidを再起動した後、Bluetoothを有効にする必要がありました'コードで解決:これは関数コードです:Log( "PM.CheckForHeadSet"、 "In");

    if (mBluetoothAdapter == null) {
        Log("PM.CheckForHeadSet","BlueTooth adapter not found");
        return "Error Bluetooth adapter";
    }
    switch (mBluetoothAdapter.getState()){
        case BluetoothAdapter.STATE_OFF: 
            Log("PM.CheckForHeadSet.getState"," STATE_OFF");
            mBluetoothAdapter.enable();
            break;
        case BluetoothAdapter.STATE_TURNING_ON:
            Log("PM.CheckForHeadSet.getState","STATE_TURNING_ON");
            break;
        case BluetoothAdapter.STATE_ON:
            Log("PM.CheckForHeadSet.getState","STATE_ON");
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:
            Log("PM.CheckForHeadSet.getState","STATE_TURNING_OFF");
            break;
    }
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

    // If there are paired devices, add each one to the ArrayAdapter
    if (pairedDevices.size() == 1) {
        for (BluetoothDevice device : pairedDevices) 
            if(device.getBondState() == BluetoothDevice.BOND_BONDED){
                Log("PM.CheckForHeadSet Connected to:",device.getName());
            }
    }
    Log("PM.CheckForHeadSet ServiceListener:","In");
    final 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;        }
            }};
    if(mBluetoothHeadset == null)
        Log("PM.CheckForHeadSet","mBluetoothHeadset = null");
    else
        Log("PM.CheckForHeadSet","mBluetoothHeadset = " + mBluetoothHeadset.toString());
    if(context == null)
        Log("PM.CheckForHeadSet","context = null");
    else
        Log("PM.CheckForHeadSet","context = " + context.toString());

    if(mProfileListener == null)
        Log("PM.CheckForHeadSet","mProfileListener = null");
    else
        Log("PM.CheckForHeadSet","mProfileListener = " + mProfileListener.toString());

    if(mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET) == true)
         Log("PM.CheckForHeadSet.getProfileProxy","true");
    else        
        Log("PM.CheckForHeadSet.getProfileProxy","false"); 
    Log("PM.CheckForHeadSet","Out");
    return "Set Headset";

GetProfileProxyを新しいProfileListenerの上に配置した場合(docomantaionの例のように|)、mProfileListener変数はまだnullであり、getProfileProxyはfalseを返します

そしてこれはlogcatです:

03-12 10:09:49.906: D/SpySitter(4205): PM.CheckForHeadSet-In
03-12 10:09:50.968: D/dalvikvm(4205): threadid=1: still suspended after undo (sc=1 dc=1)
03-12 10:09:59.453: D/SpySitter(4205): PM.CheckForHeadSet.getState-STATE_ON
03-12 10:10:02.640: D/SpySitter(4205): PM.CheckForHeadSet Connected to:-Motorola H790
03-12 10:10:04.226: D/SpySitter(4205): PM.CheckForHeadSet ServiceListener:-In
03-12 10:10:13.945: D/SpySitter(4205): PM.CheckForHeadSet-mBluetoothHeadset = null
03-12 10:10:17.984: D/SpySitter(4205): PM.CheckForHeadSet-context = android.app.Application@408472a0
03-12 10:10:21.820: D/SpySitter(4205): PM.CheckForHeadSet-mProfileListener = com.example.HelloForm.Tools$1@40894d00
03-12 10:10:28.796: D/SpySitter(4205): PM.CheckForHeadSet.getProfileProxy-true
03-12 10:10:31.226: D/SpySitter(4205): PM.CheckForHeadSet-Out
4

1 に答える 1

1

getProxyProfile は 3 つのケースで false を返します (これは android-15 ソースによるため、android-11 では若干異なる場合があります)。

  • 渡されたコンテキストが null です
  • 渡された BluetoothProfile.ServiceListener が null です
  • 渡された int が BluetoothProfile.HEADSET、BluetoothProfile.A2DP、BluetoothProfile.INPUT_DEVICE、BluetoothProfile.PAN、BluetoothProfile.HEALTH のいずれでもない

falsegetProxyProfile の呼び出しにブレークポイントを設定して、これらのケースのどれが戻り値を引き起こしているかを突き止めることをお勧めします。

編集:詳細情報

インターフェイス メソッドを呼び出すには、Bluetooth がサポートされ、有効になっている必要があります。Bluetooth がサポートされていない場合は、getDefaultAdapter()を返しnullます。アダプターがある場合は、アダプターで呼び出しisEnabled()て Bluetooth が有効になっているかどうかを確認できます。これは非常に簡単なので、すでに実行している可能性があります。

onServiceConnectedはクラス内のandroid.content.ServiceConnectionメンバー ( mServiceConnection)によって呼び出され、オブジェクトの作成時に呼び出しBluetoothHeadsetによって Android OS によって呼び出されます。このサービスのバインディングが失敗した場合、logcat メッセージ ログに次のようなエラーが記録されます。bindServiceBluetoothHeadset

       "Could not bind to Bluetooth Headset Service"

「BluetoothHeadset」のタグが付いています。このエラーが表示されない場合は、内部で正常に動作している可能性があります。

最後に、Android は一度に 1 つの接続されたヘッドセットのみをサポートすることです。このコードを実行するときは、ヘッドセットが接続されていないことを確認してください。同様に、多くのデバイスは一度に 1 つの Bluetooth 接続しかサポートしていません。それがデバイスに当てはまる場合は、このコードを実行するときにデバイスがまだ接続されていないことを確認する必要があります。

これで、Bluetooth 接続について私が知っていることはほとんどなくなり、簡単に判断できます。問題の解決に役立つことを願っています。

于 2012-03-07T14:38:16.127 に答える