2016-12-16 10 views
2

私は、ブルートゥースLE周辺デバイスのように、ブルートゥースLEセントラルデバイスと接続して広告を停止し、複数のBluetooth LEセントラルに接続するBluetooth LE周辺デバイスを制限するアプリケーションを開発したいと考えています。ブルートゥースLE周辺の広告は、ブルートゥースLEセントラルデバイスと接続して

1つのBluetooth LE周辺デバイスは、一度に1つのBluetooth LEのみに接続します。

private final BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() { 

     @Override 
     public void onServiceAdded(int status, BluetoothGattService service) { 
                              super.onServiceAdded(status, service); 
     } 

     @Override 
     public void onConnectionStateChange(BluetoothDevice device, final int status, int newState) { 
      super.onConnectionStateChange(device, status, newState); 
      if (status == BluetoothGatt.GATT_SUCCESS) { 
       if (newState == BluetoothGatt.STATE_CONNECTED) { 
        mBluetoothDevices.add(device); 

        // Bluetooth LE peripheral stop advertising on connect with Bluetooth LE central device 
        mAdvertiser.stopAdvertising(mAdvCallback); 

        Log.v(TAG, "Connected to device: " + device.getAddress()); 
       } else if (newState == BluetoothGatt.STATE_DISCONNECTED) { 
        mBluetoothDevices.remove(device); 
        Log.v(TAG, "Disconnected from device"); 
       } 
      } else { 
       mBluetoothDevices.remove(device); 
       // There are too many gatt errors (some of them not even in the documentation) so we just 
       // show the error to the user. 
       final String errorMessage = getString(R.string.status_errorWhenConnecting) + ": " + status; 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show(); 
        } 
       }); 
       Log.e(TAG, "Error when connecting: " + status); 
      } 
     } 

     @Override 
     public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, 
               BluetoothGattCharacteristic characteristic) { 
     } 

     @Override 
     public void onNotificationSent(BluetoothDevice device, int status) { 
      super.onNotificationSent(device, status); 
      Log.v(TAG, "Notification sent. Status: " + status); 
     } 

     @Override 
     public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, 
               BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) { 
     } 

     @Override 
     public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, 
              BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, 
              int offset, 
              byte[] value) { 
     } 
    }; 

私はBLE中央装置mAdvertiser.stopAdvertising(mAdvCallback);

との接続にstopAdvertisingています: その他のBluetooth LE中央装置は、私は、コードの下にしようと今まで中央

のBluetooth LE周辺とBluetooth LEのに成功し、接続した後にスキャンができませんでした

切断接続です。

この使用例で私を助けてください。 アドバイス

答えて

5

BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect)BluetoothGatt.STATE_CONNECTEDの前に入れてください。stopAdvertisingの前に、期待されるAndroidフレームワークの動作があります。あなたは、リンクを保持するために継続する必要があり、もう宣伝したくない場合は、

//******************* SOLUTION ************************** 
     BluetoothDevice mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress()); 
     mGattServer.connect(mDevice, false); 
//******************************************************* 

ソリューションの追加connect()

コードスニペットonConnectionStateChangeのコードスニペット()の実装に呼び出す必要があります

@Override 
public void onConnectionStateChange(BluetoothDevice device, final int status, int newState) { 
    super.onConnectionStateChange(device, status, newState); 
    if (status == BluetoothGatt.GATT_SUCCESS) { 
     if (newState == BluetoothGatt.STATE_CONNECTED) { 
      mBluetoothDevices.add(device); 

//******************* SOLUTION ************************** 
     BluetoothDevice mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress()); 
     mGattServer.connect(mDevice, false); 
//******************************************************* 

      // Bluetooth LE peripheral stop advertising on connect with Bluetooth LE central device 
      mAdvertiser.stopAdvertising(mAdvCallback); 

      Log.v(TAG, "Connected to device: " + device.getAddress()); 
     } else if (newState == BluetoothGatt.STATE_DISCONNECTED) { 
      mBluetoothDevices.remove(device); 
      Log.v(TAG, "Disconnected from device"); 
     } 
    } else { 
     mBluetoothDevices.remove(device); 
     // There are too many gatt errors (some of them not even in the documentation) so we just 
     // show the error to the user. 
     final String errorMessage = getString(R.string.status_errorWhenConnecting) + ": " + status; 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show(); 
      } 
     }); 
     Log.e(TAG, "Error when connecting: " + status); 
    } 
} 
+1

私の実験では、getRemoteDeviceは必要ないと思われます。これは次のようにも動作します: 'if(status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED && device!= null)mGattServer.connect(device、false); mAdvertiser.stopAdvertising(mAdvCallback); '接続が何らかの形で変わったときに広告を続けるのは良くないと思われるので、接続が成功したかどうかに関係なく、広告を停止します。 – JustAMartin

+0

これは私にとっては役に立ちません。しかし、私は後でstopAdvertisingを行い、onConnectionStateChangeではしません。 mGattServer.connect(mDevice、false)を呼び出してもデバイスが切断されます。 stopAdvertisingの前に –

+0

mGattServer.connect(mDevice、false)を入力してください。 onConnectionStateChange – Palak

関連する問題