アプリでAndroidデバイスをBluetoothヘッドセットに接続させたい

203 views
Skip to first unread message

rinjintoat...@gmail.com

unread,
May 18, 2017, 9:18:04 AM5/18/17
to 日本Androidの会
AndroidデバイスとBluetoothのヘッドセットを接続させるアプリを作ろうとしています。
デバイスとヘッドセットを接続させて、
Androidの「Settings」アプリ「Bluetooth」画面において、ヘッドセットとの状態が"Connected"になるようにしようとしています。

しかし、現状ではアプリとヘッドセットのconnectはできても、上記画面では状態が変わりません。
どのようにしたらいいのか教えてください。
なお、ペアリングは出来ております。

現状のコード
connectDevice(BluetoothDevice device) {
    // deviceはBluetoothでペアリング済みのヘッドセットオブジェクト
    BluetoothSocket soket = device.createRfcommSocketToServiceRecord(UUID.fromString({ヘッドセットのUUID}));
    socket.connect();
}

Message has been deleted
Message has been deleted

rinjintoat...@gmail.com

unread,
May 18, 2017, 9:50:04 AM5/18/17
to 日本Androidの会
自己解決できましたのでご報告致します。

本件、リフレクションを使用することで実現出来ました。
以下に実現コードを記述します。(若干省略します)

public void start(Context context) {
    ((BluetoothManager) context_.getSystemService(Context.BLUETOOTH_SERVICE)).getProfileProxy(Context, new BluetoothServiceListener(), BluetoothProfile.HEADSET);
    BluetoothDevice device = getDevice() //ペアリングしている目的のヘッドセットデバイスのオブジェクト取得メソッド(省略)
    connectDevice(BluetoothDevice device);
}

void connectDevice(BluetoothDevice device) {
    Method connectMethod = BluetoothHeadset.class.getDeclaredMethod("connect", BluetoothDevice.class);
    connectMethod.setAccessible(true);
    connectMethod.invoke(mProxy, device);
}

BluetoothHeadset mProxy;

class BluetoothServiceListener implements BluetoothProfile.ServiceListener {
    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        mProxy = (BluetoothHeadset)proxy;
    }
}


2017年5月18日木曜日 22時18分04秒 UTC+9 rinjintoat...@gmail.com:
Reply all
Reply to author
Forward
0 new messages