2017-12-06 11 views
0

특성에서 값을 읽고 바이트 배열에 값을 저장하려고합니다. 연결이 수립 된 후 나는 getStartUpValue 함수를 호출서비스에 연결 한 후 특성 값을 읽음 연결이 설정된 후 Bluetooth Low Energy Android

public void getStartUpValue(){ 
    Log.w(TAG, "Reading completed");} 
    if(mBluetoothLeService.readWhiteAndIntensityCharacteristic() == null) 
    { 
     Log.w(TAG, "FAILED Reading value failed"); 
    } 
    startValue = mBluetoothLeService.readWhiteAndIntensityCharacteristic(); 
    Log.w(TAG, "Reading completed"); 

} 

: 내 DeviceControlActivity 내부

public byte[] readWhiteAndIntensityCharacteristic() { 
if (mBluetoothAdapter == null || mBluetoothGatt == null) { 
    Log.w(TAG, "BluetoothAdapter not initialized"); 
    return null; 
} 
/*check if the service is available on the device*/ 
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString(UuidAdresssService)); 
if (mCustomService == null) { 
    Log.w(TAG, "Custom BLE Service not found"); 
    return null; 
} 
/*get the read characteristic from the service*/ 
BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString(UuidAdresssWhiteAndIntensityCharastic)); 
byte[] messageByte = mReadCharacteristic.getValue(); 
if (messageByte != null && messageByte.length > 0) { 
    final StringBuilder stringBuilder = new StringBuilder(messageByte.length); 
    for (byte byteChar : messageByte) 
     stringBuilder.append(String.format("%02X", byteChar)); 
    s = "0x" + stringBuilder.toString(); 
    Log.v("Scan Activity", s); 
    if (mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false) { 
     Log.w(TAG, "Failed to read characteristic"); 
    } 
} 
return messageByte; 

} 이 함수를 호출받을 :

은 여기 내 BluetoothLeService 내부의 독서에 대한 내 기능입니다.

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
    final String action = intent.getAction(); 
    if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { 
     mConnected = true; 
     updateConnectionState(R.string.connected); 
     invalidateOptionsMenu(); 
    } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { 
     mConnected = false; 
     updateConnectionState(R.string.disconnected); 
     invalidateOptionsMenu(); 
     clearUI(); 
    } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { 
     // Show all the supported services and characteristics on the user interface. 
     mBluetoothLeService.getSupportedGattServices(); 
     getStartUpValue(); 

    } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { 
     displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)); 

    } 
} 

}};

매번 읽는 데 실패하지만 특성에 값을 전송해도 문제가 없습니다.

이 문제를 어떻게 해결할 수 있습니까?

+0

감사합니다'대신'mReadCharacteristic.getValue()'의 ? 읽기 작업의 결과는'onCharacteristicRead (BluetoothGatt, BluetoothGattCharacteristic, int)'콜백에 의해보고됩니다. –

답변

1

솔루션은 다음과 같습니다 mBluetoothGatt.readCharacteristic (mReadCharacteristic) 읽기가 호출 될 콜백을 완료 한 후

. 당신은`mBluetoothGatt.readCharacteristic (mReadCharacteristic를) 시도 만약에 앤드류 Vovk