前提・実現したいこと
タブレット(セントラル)・ウォッチ(ペリフェラル)間でBLE通信を行うプログラムを作成しています。
発生している問題・エラーメッセージ
ペリフェラル側で終了処理として、
BluetoothGattServer.cancelConnectionを呼び出していますが、
コールバックされるはずのBluetoothGattServerCallbackの
onConnectionStateChangeが呼び出されません。
そのためか分かりませんが、再度接続するために同じUUIDのサービスを作成すると、
作成してすぐに自動的にセントラルと接続されます。
(このとき、セントラル側では接続処理を行っておりません。)
セントラル側で、BluetoothDevice.connectGattの引数である
自動接続(autoconnect)はfalseにしているため、自動で接続されることに違和感があります。
海外質問サイトで、「BluetoothGattServer.cancelConnection」がうまく機能していないという
記載を見つけましたが、ほかの方法があるのでしょうか。
解決方法としては、一度ペリフェラルのBluetoothをOFF/ONとすることで
接続がリセットされます。
けれどコーディングで接続解除できるようにしたいです。
ペリフェラル側
java
1 /** 2 * アドバタイズ終了 3 */ 4 private void stop() { 5 6 if (mBtGattServer != null) { 7 Log.d(LOG_TAG_DATA, "gattServer終了処理"); 8 if (mDevice != null) { 9 mBtGattServer.cancelConnection(mDevice); 10 } else { 11 mBtGattServer.clearServices(); 12 mBtGattServer.close(); 13 mBtGattServer = null; 14 } 15 } 16 } 17 18/** コールバック作成 */ 19 private BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() { 20 21 @Override 22 public void onConnectionStateChange(BluetoothDevice device, int status, int newState) { 23 if(newState == BluetoothProfile.STATE_CONNECTED){ 24 mDevice = device; 25 Log.d(LOG_TAG_DATA, "-----------------------接続完了"); 26 27 /** アドバタイズ終了 */ 28 if (mBtAdvertiser != null) { 29 mBtAdvertiser.stopAdvertising(mAdvertiseCallback); 30 } 31 } 32 else{ 33 Log.d(LOG_TAG_DATA, "-----------------------接続解除"); 34 if (mBtGattServer != null) { 35 mBtGattServer.clearServices(); 36 mBtGattServer.close(); 37 mBtGattServer = null; 38 } 39 if (mBtAdvertiser != null) { 40 mBtAdvertiser.stopAdvertising(mAdvertiseCallback); 41 } 42 mDevice = null; 43 } 44 } 45 };
ログ
07-02 09:36:45.489 3635-3635/com.example.XXX.XXXD/★wear: gattServer終了処理 07-02 09:36:45.489 3635-3635/com.example.XXX.XXXD/BluetoothGattServer: cancelConnection() - device: 12:34:56:78:90:38 07-02 09:36:45.491 490-1951/? D/BtGatt.GattService: serverDisconnect() - address=12:34:56:78:90:38, connId=4 07-02 09:36:45.491 490-637/? E/bt_btif: bta_gatts_cancel_open failed for open request 07-02 09:36:45.491 490-637/? W/bt_btif: bta_dm_act no entry for connected service cbs
補足情報(FW/ツールのバージョンなど)
セントラル:AndroidOS 7
ペリフェラル:WearOS 2
あなたの回答
tips
プレビュー