5

OS 2.0-2.3で現在接続されている(ペアリングされているだけでなく)BTヘッドセットを確認する必要があります。このような機能は、Bluetoothヘッドセットクラスが導入されたAPIバージョン11まで存在しません。ただし、以前のAPIにはBluetoothHeadsetというクラスがすでに存在していましたが、一般公開されていませんでした。そのドキュメントは次のとおりです:http ://www.kiwidoc.com/java/l/x/android/android/9/p/android.bluetooth/c/BluetoothHeadset 。そのため、リフレクションを使用して「isConnected」メソッドを呼び出そうとしましたが、リフレクションがかなりひどく、エラーが発生しますjava.lang.IllegalArgumentException: object is not an instance of the class

を使用してペアリングされたデバイスのリストを取得し、それぞれでこのメソッドBluetoothDevice.getBondedDevices()を使用しようとしています。isConnected()コードは次のとおりです。

public boolean isBtDevConnected(BluetoothDevice btDev){
    boolean connected  = false;
    try {
        Class<?> BTHeadset = Class.forName("android.bluetooth.BluetoothHeadset");
        Method isConnected = BTHeadset.getMethod("isConnected", new Class[] {BluetoothDevice.class});
                connected = isConnected.invoke(BTHeadset, new Object[] {btDev});
            }
        }
    } catch (Exception e) {
        WriteToLog(e);
    }
    return connected;
}

メソッドを呼び出す行で例外が発生しますが、何が間違っているのかわかりません。

4

1 に答える 1

0

BluetoothHeadset は、IPC を介して Bluetooth ヘッドセット サービスを制御するためのプロキシ オブジェクトです。

getProfileProxy(Context, BluetoothProfile.ServiceListener, int) を使用して BluetoothHeadset プロキシ オブジェクトを取得します。closeProfileProxy(int, BluetoothProfile) を使用して、サービス接続を閉じます。

Android は、一度に 1 つの接続された Bluetooth ヘッドセットのみをサポートします。各メソッドは、適切な権限で保護されています。

ソース: http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html

于 2013-02-03T12:47:53.840 に答える