2016-05-12 81 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