9

Ich entwickle eine Anwendung, wo ich mit Bluetooth-Gerät auf Android 4.3 verbinden muss.Wie erhole ich den Akkustand nach dem Anschluss an das BLE-Gerät?

Und ich möchte den Akkuladestand erhalten, indem Sie mit Battery_Service und Battery_Level.

public class BluetoothLeService extends Service { 

private static final UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb"); 
    private static final UUID Battery_Level_UUID = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb"); 



public void getbattery() { 

     BluetoothGattService batteryService = mBluetoothGatt.getService(Battery_Service_UUID); 
     if(batteryService == null) { 
      Log.d(TAG, "Battery service not found!"); 
      return; 
     } 

     BluetoothGattCharacteristic batteryLevel = batteryService.getCharacteristic(Battery_Level_UUID); 
     if(batteryLevel == null) { 
      Log.d(TAG, "Battery level not found!"); 
      return; 
     } 

     mBluetoothGatt.readCharacteristic(batteryLevel); 
     // What should I do that I can get the battery level ?? 
     Log.d(TAG, "Battery level " + mBluetoothGatt.readCharacteristic(batteryLevel);); 
    } 

Aber der Wert der mBluetoothGatt.readCharacteristic(batteryLevel); ist nicht der Batteriepegelwert

Wie die Batterie lesen ????

+0

Welchen Wert erhalten Sie? –

+0

der Wert von mBluetoothGatt.readCharacteristic (batteryLevel); ist "wahr" – Wun

+0

sorry, ich glaube nicht, dass ich helfen kann ... vielleicht würde dies: http://developer.samsung.com/forum/board/thread/view.do?boardName=SDK&messageId=240110 –

Antwort

19

Ich habe dieses Problem gelöst.

public class BluetoothLeService extends Service { 

private static final UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb"); 
private static final UUID Battery_Level_UUID = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb"); 

public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 

    if(status == BluetoothGatt.GATT_SUCCESS) { 
     broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
    } 
} 


private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { 

    final Intent intent = new Intent(action); 
    Log.v(TAG, "characteristic.getStringValue(0) = " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)); 
    intent.putExtra(DeviceControl.EXTRAS_DEVICE_BATTERY, characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)); 
    sendBroadcast(intent); 
} 

public void getbattery() { 

    BluetoothGattService batteryService = mBluetoothGatt.getService(Battery_Service_UUID); 
    if(batteryService == null) { 
     Log.d(TAG, "Battery service not found!"); 
     return; 
    } 

    BluetoothGattCharacteristic batteryLevel = batteryService.getCharacteristic(Battery_Level_UUID); 
    if(batteryLevel == null) { 
     Log.d(TAG, "Battery level not found!"); 
     return; 
    } 
    mBluetoothGatt.readCharacteristic(batteryLevel); 
    Log.v(TAG, "batteryLevel = " + mBluetoothGatt.readCharacteristic(batteryLevel)); 
} 

Wenn Sie die Funktion getbattery() rufen, wird es onCharacteristicRead nennen. und onCharacteristicRead werden broadcastUpdate aufrufen und das Merkmal und die Aktion übertragen.

Und die characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) in BroadcastUpdate ist der Akku-Wert des BLE-Geräts.

+1

Es gibt Batteriestand als wahr. –

+0

@Wun @RajeshNarwal Wie hast du die 'UUID' bekommen? –

+0

@ M. S. Es ist ein öffentlicher Dienst, der in der Bluetooth SIG Site definiert ist. [Siehe hier] (https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml) – IronBlossom