9

我正在開發一個應用程序,我必須連接到Android 4.3上的藍牙設備。如何在連接到BLE設備後獲取電池電量?

我想通過使用Battery_ServiceBattery_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);); 
    } 

mBluetoothGatt.readCharacteristic(batteryLevel);值是不是電池的電平值

如何讀取電池????

+0

你會得到什麼樣的價值? –

+0

mBluetoothGatt.readCharacteristic(batteryLevel)的值;是「真的」 – Wun

+0

對不起,我不認爲我可以幫助...也許這會:http://heveloper.samsung.com/forum/board/thread/view.do?boardName=SDK&messageId=240110 –

回答

19

我已經解決了這個問題。

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)); 
} 

當你調用該函數getbattery(),它會調用onCharacteristicRead。 和onCharacteristicRead將調用broadcastUpdate並將特徵和操作傳送給它。

而broadcastUpdate中的characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)是BLE設備的電池值。

+1

它使電池電量爲真。 –

+0

@Wun @RajeshNarwal你是如何得到'UUID'的? –

+0

@ M.S。它是一個公共服務,在Bluetooth SIG網站中定義。 [看到這裏](https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml) – IronBlossom