0

주변 기기와 통신하기 위해 BLE 중앙 앱을 만들려고합니다.android BLE writeCharacteristic는 장치에서 실행될 때 false를 반환하지만 디버그 모드에서 작동합니다.

나는 안드로이드 APP https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner를 사용하고 주변 장치를 만들려면 https://developer.android.com/guide/topics/connectivity/bluetooth-le.html

여기에 주어진 BluetoothLeService 응용 프로그램을 사용하고 있습니다.

특성의 변경 사항을 읽고 통보받을 수 있으며 BluetoothGatt의 writeCharacteristic을 사용하여 특성을 업데이트하도록 BluetoothLeService를 수정했습니다. 사용

장치 :

주변기기 (

  • LGE 넥서스 5X : Redmi는 주 4 Central의로 사용

    장치()에 통보 해주기 모든 권한 읽기 (활성화) Android 7.1.2, API 25)

  • LGE Nexus 4 (Android 5.1.1, API 22)

안드로이드 스튜디오 : 버전 2.3.3 Gradle을 파일 : 안드로이드 { compileSdkVersion 25 buildToolsVersion "25.0.3"

defaultConfig { 
    minSdkVersion 18 
    targetSdkVersion 25 
} 

compileOptions { 
    sourceCompatibility JavaVersion.VERSION_1_7 
    targetCompatibility JavaVersion.VERSION_1_7 
} 

sourceSets { 
    main { 
     dirs.each { dir -> 
      java.srcDirs "src/${dir}/java" 
      res.srcDirs "src/${dir}/res" 
     } 
    } 
    androidTest.setRoot('tests') 
    androidTest.java.srcDirs = ['tests/src'] 

} 

}

코드 :

@Override 
public void onCharacteristicWrite(BluetoothGatt gatt, 
             BluetoothGattCharacteristic characteristic, int status) { 
     Log.w("Tharun", "onServicesDiscovered received: " + status); 
     setStatus(status); 
     broadcastUpdate(WRITE_DATA); 
    } 
}; 


public void writeCharacteristic(BluetoothGattCharacteristic characteristic, String 
     writeString) { 
    if (mBluetoothAdapter == null || mBluetoothGatt == null) { 
     Log.w(TAG, "BluetoothAdapter not initialized"); 
     return; 
    } 
    characteristic.setValue(writeString); 

    boolean result = mBluetoothGatt.writeCharacteristic(characteristic); 

    Log.w(TAG, "writeCharacteristic result : " + result); 
} 

문제 :

위의 장치에서 코드를 실행하면 writeCharacteristic이 false를 반환합니다. 심지어 onCharacteristicWrite가 호출되지 않습니다.

그러나 내가 응용 프로그램에 디버그 모드에서 android studio를 첨부하고 중단 점을 추가하면 (중단 점이없는 경우에만 중단 점이 작동하지 않음) 쓰기가 성공적으로 발생하고 onCharacteristicWrite가 발생합니다 운영.

문제점을 파악할 수 없습니다.

나는 왜 함수가 false를 반환하는지 볼 수있는 코드를 살펴 보려고했지만 아무 것도 찾을 수 없었다.

도움을 주시면 감사하겠습니다.

+0

쓰기 작업을 시작할 때 아직 GATT 작업이 완료되지 않았습니까? – Emil

+0

아직 완료되지 않은 GATT 작업이 없는지 확인했습니다. 나의 초기의 의심은 android-studio 실행 소스 코드가 내 장치의 코드와 다르다는 것이었다. 사실이라고 밝혀졌습니다. 나는 api 22와 android 5.1.1로 모든 프로젝트 코드를 복사 한 새로운 프로젝트를 만들었다. 이제는 잘 작동합니다. 도와 주셔서 감사합니다. – tharun

답변

0

GATT 작업이 아직 완료되지 않았는지 확인했습니다. 나의 초기의 의심은 android-studio 실행 소스 코드가 내 장치의 코드와 다르다는 것이었다. 따라서 디버그 모드에서 실행되는 코드는 장치에서만 실행 중일 때와 다릅니다. 그것이 사실임이 밝혀졌습니다. 나는 api 22와 android 5.1.1로 모든 프로젝트 코드를 복사 한 새로운 프로젝트를 만들었다. 이제는 잘 작동합니다.