9

Android 4.3에서 Bluetooth 장치에 연결해야하는 응용 프로그램을 개발 중입니다.BLE 장치에 연결 한 후 배터리 잔량을 얻는 방법은 무엇입니까?

은 내가 BATTERY_LEVELBattery_Service를 사용하여 배터리 수준을 싶어.

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://developer.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를 호출합니다. 및 onCharacteristicReadbroadcastUpdate을 호출하고 특성 및 조치를 전송합니다.

broadcastUpdate의 characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)은 BLE 장치의 배터리 값입니다.

+1

배터리 레벨을 true로 지정합니다. –

+0

@Wun @RajeshNarwal 어떻게'UUID'를 얻었습니까? –

+0

@ M.S. 자사의 공공 서비스, 블루투스 SIG 사이트에 정의 된. [여기를보십시오] (https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml) – IronBlossom