[設定]->[ワイヤレス]->[Bluetooth]を選択して、ペアリングされたBluetoothヘッドセットをプログラムで接続する動作をシミュレートしたいと思います。StackoverflowとGoogleで検索を行いましたが、どちらもAPIレベル11より前に利用できるソリューションがないことを示しています。ただし、AndroidのBluetooth実装のソースコードを調べて解決することに興味があります。問題は、どの特定のソースコードを確認する必要があるのかわからないことです。助言がありますか?どうもありがとう。
4 に答える
何日も苦労した後、私は今それをなんとかすることができました、乾杯:)
- アプリの/srcディレクトリにandroid.bluetooth.IBluetoothA2dp.aidlを追加します。
次のプライベートメソッドをコードに追加します。
private IBluetoothA2dp getIBluetoothA2dp() { IBluetoothA2dp ibta = null; try { Class c2 = Class.forName("android.os.ServiceManager"); Method m2 = c2.getDeclaredMethod("getService", String.class); IBinder b = (IBinder) m2.invoke(null, "bluetooth_a2dp"); Log.d("Felix", "Test2: " + b.getInterfaceDescriptor()); Class c3 = Class.forName("android.bluetooth.IBluetoothA2dp"); Class[] s2 = c3.getDeclaredClasses(); Class c = s2[0]; // printMethods(c); Method m = c.getDeclaredMethod("asInterface", IBinder.class); m.setAccessible(true); ibta = (IBluetoothA2dp) m.invoke(null, b); } catch (Exception e) { Log.e("flowlab", "Erroraco!!! " + e.getMessage()); }
これでテストしてください:
private void testBluetoothA2dp(BluetoothDevice device) { // TODO Auto-generated method stub // TODO Auto-generated method stub IBluetoothA2dp ibta = getIBluetoothA2dp(); try { Log.d("Felix", "Here: " + ibta.getSinkPriority(device)); ibta.connectSink(device); } catch (RemoteException e) { // * TODO Auto-generated catch block e.printStackTrace(); }
}
グーグル、スタックオーバーフローのチェック、Androidソースコードの確認に多くの時間を費やしたため、これらのコードの参照を提供できませんが、ソースを追跡できませんでした。Stackoverflowの皆さんに感謝します:)
OK、Honeycomb以降をサポートするようにこれを更新しました。インターフェイスに新しい関数を追加する必要があります。私はここでそれをしました:
interface IBluetoothA2dp {
boolean connectSink(in BluetoothDevice device); // Pre API 11 only
boolean disconnectSink(in BluetoothDevice device); // Pre API 11 only
boolean connect(in BluetoothDevice device); // API 11 and up only
boolean disconnect(in BluetoothDevice device); // API 11 and up only
boolean suspendSink(in BluetoothDevice device); // all
boolean resumeSink(in BluetoothDevice device); // all
BluetoothDevice[] getConnectedSinks(); // change to Set<> once AIDL supports, pre API 11 only
BluetoothDevice[] getNonDisconnectedSinks(); // change to Set<> once AIDL supports,
int getSinkState(in BluetoothDevice device);
boolean setSinkPriority(in BluetoothDevice device, int priority); // Pre API 11 only
boolean setPriority(in BluetoothDevice device, int priority); // API 11 and up only
int getPriority(in BluetoothDevice device); // API 11 and up only
int getSinkPriority(in BluetoothDevice device); // Pre API 11 only
boolean isA2dpPlaying(in BluetoothDevice device); // API 11 and up only
}
次に、このインターフェースで関数を呼び出す前に、APIバージョンを確認する必要があります。これが私の例です:
if (android.os.Build.VERSION.SDK_INT < 11) {
IBluetoothA2dp ibta = getIBluetoothA2dp();
try {
Log.d(LOG_TAG, "Here: " + ibta.getSinkPriority(device));
if (ibta != null)
ibta.connectSink(device);
} catch (Exception e) {
Log.e(LOG_TAG, "Error " + e.getMessage());
}
} else {
IBluetoothA2dp ibta = getIBluetoothA2dp();
try {
Log.d(LOG_TAG, "Here: " + ibta.getPriority(device));
if (ibta != null)
ibta.connect(device);
} catch (Exception e) {
Log.e(LOG_TAG, "Error " + e.getMessage());
}
}
お役に立てれば。同じアプリを両方のインターフェースで動作させることができました。
Android 4.2でこれを試しましたが、次の行はnullを返します。4.1に取り組んでいましたが、何かアイデアはありますか?
IBinder b = (IBinder) m2.invoke(null, "bluetooth_a2dp");
これは、接続/再接続の問題に関連する種類です(回答はアップルスクリプトです)。
私はちょうどandroidhtcone Vを購入し、アプリPdaNet(私の電話と私のmac os 10.5.8 ppcラップトップの両方にインストールされています)を介してホットスポットとして使用しています。
Wi-FiまたはUSB経由でホットスポットテザリングを機能させることができないようですが、BLUETOOTHではうまく機能します!唯一の問題は、接続が最大2分から40分しか続かないことです(今は記録です)。手動で再接続する必要があります。これには2秒しかかかりませんが、Macのネットワークの側面ができれば便利です。自動再接続します。
私のセルは一定の信号を送信しているので問題ではありません(ただし、通常の接続ではセルから一時的に信号が失われる可能性があります)..問題はラップトップの自動再接続です。私のラップトップとhtconev DOはペアリングされたままで、ラップトップ側には自動再接続がありません。
アップルスクリプトbcsを知っていれば、失われたBluetooth接続を自動再接続するアップルスクリプトを作成できますか?またはウィジェットでこれを実行できますか?もしそうなら、Bluetoothテザリングがうまく機能しているので、日陰で作ってもらいます。
私はこれが私と他の人の両方が同じ答えを探しているのに役立つことを望んでいました..このスレッドを開いたままにしておくと、後でいくつかの可能なアップルスクリプトソリューション(私はすぐに学ぶ必要があります)で戻ることができます..ありがとう-marcus