2017-05-06 15 views
0

서비스가 먼저 발견 된 후 장치와 (gatt) 연결이 이루어진 것으로 adb 로그에 나타납니다. 사실입니까? 갓 연결이 먼저 만들어지지 않고 서비스가 종속 장치에서 검색됩니다.서비스 안드로이드 BLE API의 Gatt 연결

답변

0

설명 :

먼저 당신이 당신의 장치 상태가 연결되어있는 경우 그런 다음 gatt.discoverServices()를 사용하여 장치를 검색 할 수 onConnectionStateChange에 장치 연결 상태를 확인할 수 있습니다. 그런 다음 onServicesDiscovered에 서비스 발견 응답을받을 수 있습니다. 상태는 BluetoothGatt.GATT_SUCCESS입니다. 이제 장치에 쓸 수 있습니다.

코드 예제 :

private BluetoothGattCallback gattCallback = new BluetoothGattCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     super.onConnectionStateChange(gatt, status, newState); 

     if (newState == BluetoothProfile.STATE_CONNECTED) { 
       // Device Connected, Now Discover your service 
       gatt.discoverServices(); // You need to Discovered your gatt Service first 
     } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 

     } 

    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     super.onServicesDiscovered(gatt, status); 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      // You can get discovered gatt service here. Now you can WRITE on your gatt service 
     } 
    } 

    @Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicWrite(gatt, characteristic, status); 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      //You can get WRITE characteristic response here 
     } 
    } 

    @Override 

    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicRead(gatt, characteristic, status); 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      //You can get READ characteristic response here 
     } 
    } 
}; 

나는 그것이 희망이 당신을 도와줍니다.

+0

답장을 보내 주셔서 감사합니다.하지만 연결하기 전에 서비스 검색 로그를 어떻게받을 수 있습니까? 이것을 증명하는 다른 공식 문서 (spec 또는 Android 문서)가 있습니까? – Raulp

+0

BLE 장치와 읽기 - 쓰기 데이터를 연결하는 데 필자의 경험에 따라 답을했습니다. 어떤 lib를 사용하고 있습니까? – pRaNaY