###前提・実現したいこと
Android Studioを使ってBluetooth通信を行うアプリを開発しようとしており、
サンプルコードのBluetoothChatで検証を行っております。
通信相手が既にペアリングされている場合と、ペアリングされていない場合が想定されており、
既にペアリングされているデバイスと接続する際は、
ペア設定要求を出さずに接続したいと考えています。
###発生している問題・エラーメッセージ
サンプルコードでは既にペアリングされているデバイスでも、
まだペアリングされていないデバイスでも、
接続(connect)を行おうとするとペア設定要求が出てしまいます。
###該当のソースコード
public void run() { Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType); setName("ConnectThread" + mSocketType); // Always cancel discovery because it will slow down a connection mAdapter.cancelDiscovery(); // Make a connection to the BluetoothSocket try { // This is a blocking call and will only return on a // successful connection or an exception mmSocket.connect(); } catch (IOException e) { // Close the socket try { mmSocket.close(); } catch (IOException e2) { Log.e(TAG, "unable to close() " + mSocketType + " socket during connection failure", e2); } connectionFailed(); return; } // Reset the ConnectThread because we're done synchronized (BluetoothChatService.this) { mConnectThread = null; } // Start the connected thread connected(mmSocket, mmDevice, mSocketType); }
###補足情報(言語/FW/ツール等のバージョンなど)
Android Studio 2.1.3
Bluetooth Ver2.1+EDR
2016/09/12追記:
どのBluetooth通信の解説を見ても
BluetoothSocket.connect()
を使用しています。
しかし、これを呼び出してしまうとペア設定の要求が出てしまいます。
現状、これ以外にソケットの接続を行う方法を見つけられていません。
参考にしたサイトの一例ですが、
http://www.bright-sys.co.jp/blog/android-using-bluetooth-spp/
http://www.limy.org/program/android/bluetooth.html
http://blog.livedoor.jp/sce_info3-craft/archives/8337697.html
https://sites.google.com/a/gclue.jp/fab-zang-docs/sumafo-lian-xie/02-arduino-android
ペアリング済みであったら、別の通信方法があるのか、
もしくはペア設定の要求を出さずに接続する方法があるのか、
ご存じの方がいらっしゃいましたら、ご教示下さい。
2016/09/14追記:
上記事象はSecureメソッドを使用した場合に発生する。
Insecureメソッドならば、ペア設定要求は出ない。
ただし、ペアリングしていなくてもペア設定要求は出ず、
そのまま接続が確立してしまう。
また、ペアリングされている場合は、
接続時にペアリングが解除されてしまう。
通信相手がSSPを使用していることが判明。
これによりセキュリティレベルが上がっていることが原因か。
よろしくお願いします。
あなたの回答
tips
プレビュー