2016-05-12 120 views
0

對於一個應用程序,我需要一個Android設備既是一個Ble Gatt外設和服務器,以接受傳入和傳出的郵件;然而,似乎除了在github上查看其他人的項目外,我無法找到關於建立和維護服務器的許多信息。 任何人都可以告訴我正確的解決方案或指導我有關設置BluetoothGattServer的更多信息。Android使用藍牙收集服務器

回答

6

我想快速提及,大多數使用的Android設備不支持BluetoothGattServer。不過新型號的功能越來越多。

首先您可能想要宣傳您的服務,以便其他設備瞭解服務器。爲此,我們需要創建AdvertiseSettings,AdvertiseData和AdvertData for ScanResponses。

AdvertiseSettings settings = new AdvertiseSettings.Builder() 
       .setConnectable(true) 
       .build(); 

AdvertiseData advertiseData = new AdvertiseData.Builder() 
        .setIncludeDeviceName(true) 
        .setIncludeTxPowerLevel(true) 
        .build(); 

AdvertiseData scanResponseData = new AdvertiseData.Builder() 
        .addServiceUuid(new ParcelUuid(your_service_uuid)) 
        .setIncludeTxPowerLevel(true) 
        .build(); 

之後,你需要創建廣告狀態回調:

AdvertiseCallback callback = new AdvertiseCallback() { 
       @Override 
       public void onStartSuccess(AdvertiseSettings settingsInEffect) { 
        Log.d(TAG, "BLE advertisement added successfully"); 
       } 

       @Override 
       public void onStartFailure(int errorCode) { 
        Log.e(TAG, "Failed to add BLE advertisement, reason: " + errorCode); 
       } 
      }; 

現在你可以開始宣傳你的服務:

BluetoothLeAdvertiser bluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); 
bluetoothLeAdvertiser.startAdvertising(settings, advertiseData, scanResponseData, callback); 

因爲我們需要先創建BluetoothGattServer a BluetoothGattServerCallback:

BluetoothGattServerCallback callback = new BluetoothGattServerCallback() { 
      @Override 
      public void onConnectionStateChange(BluetoothDevice device, int status, int newState) { 
       super.onConnectionStateChange(device, status, newState); 
      } 

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

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

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

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

      @Override 
      public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) { 
       super.onDescriptorReadRequest(device, requestId, offset, descriptor); 
      } 

      @Override 
      public void onNotificationSent(BluetoothDevice device, int status) { 
       super.onNotificationSent(device, status); 
      } 

      @Override 
      public void onMtuChanged(BluetoothDevice device, int mtu) { 
       super.onMtuChanged(device, mtu); 
      } 

      @Override 
      public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) { 
       super.onExecuteWrite(device, requestId, execute); 
      } 
     }; 

您不需要實現所有這些方法,只需要那些您感興趣的方法。例如,您可以實現onCharacteristicReadRequest(...)方法以將數據返回到讀取BluetoothGattServer上的特性的設備。

之後,你可以打開一個GattServer,創建服務和服務添加到服務器:

BluetoothGattServer bluetoothGattServer = mBluetoothManager.openGattServer(Context, callback); 
BluetoothGattService service = new BluetoothGattService(your_service_uuid, BluetoothGattService.SERVICE_TYPE_PRIMARY); 

//add a read characteristic. 
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(your_characteristic_uuid, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ); 

service.addCharacteristic(characteristic) 

bluetoothGattServer.addService(service); 

現在你已經設置了寫特性,您BluetoothGattServer從其他設備可以讀取。 我希望這個簡短的總結對你有幫助,否則我會盡力幫你澄清不清楚的事情。

+0

服務器也可以成爲gatt客戶端嗎?我似乎有一個問題,因爲添加服務器與gatt客戶端連接,但我假設當手機試圖連接到已經連接到其他設備的問題發生。由於接口更改,並且equals方法僅基於更改的地址,並且在api23中返回不正確的地址,所以再次無法識別可更新設備。 – kdgwill

+0

是的,設備可以同時充當外設和中央設備。至於你所遇到的問題,我不太確定,但據我所知它應該起作用。請記住,Android ble堆棧非常不穩定。 – p2pkit

+0

對於識別,有一種方法可以識別設備。在廣告設置中,您可以將附加數據添加到服務uuid​​,只需在ScanResponseData的構建中添加對addServiceData(...)的調用即可。但是ScanResponseData的大小不能超過31個字節。另一個缺陷是,在接收端,serviceData鍵是32位uuid而不是128位。 – p2pkit