2016-12-16 94 views
2

我想開發應用程序,如藍牙LE外圍設備停止廣告與藍牙LE中央設備連接並限制連接多個藍牙LE中心的藍牙LE外圍設備。藍牙LE外圍設備停止廣告與藍牙LE中央設備連接

一個藍牙LE外圍設備一次只能連接一個藍牙LE中央。成功連接藍牙LE外圍設備和藍牙LE的中央

至此之後 其他藍牙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) { 
     } 
    }; 

我對stopAdvertising連接與BLE中心設備mAdvertiser.stopAdvertising(mAdvCallback);

這是斷開連接。

請幫我在這個用例。 感謝提前

回答

5

BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect)置於BluetoothGatt.STATE_CONNECTED之前stopAdvertising,因爲預期的Android框架行爲。如果需要繼續持有該鏈接,不想再投放廣告,您需要調用解決方案的額外connect()

代碼片段

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

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

這不適合我。但是我稍後停止了廣告,而不是onConnectionStateChange。即使我調用mGattServer.connect(mDevice,false),設備也會斷開;在停止廣告之前。 –

+0

請把mGattServer.connect(mDevice,false);在onConnectionStateChange – Palak