9

मैं एक ऐसा एप्लीकेशन विकसित कर रहा हूं जहां मुझे एंड्रॉइड 4.3 पर ब्लूटूथ डिवाइस से कनेक्ट करना है।बीएलई डिवाइस से कनेक्ट करने के बाद बैटरी स्तर कैसे प्राप्त करें?

और मैं Battery_Service और का उपयोग कर 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);); 
    } 

लेकिन mBluetoothGatt.readCharacteristic(batteryLevel); का मूल्य बैटरी स्तर मूल्य

कैसे बैटरी को पढ़ने के लिए नहीं है ????

+0

आपको क्या मूल्य मिलता है? –

+0

mBluetoothGatt.readCharacteristic (बैटरी लेवेल) का मान; "सच" – 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 पर कॉल करेंगे और इसकी विशेषता और क्रिया को प्रेषित करेंगे।

और प्रसारण में characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) अद्यतन अद्यतन बीएलई डिवाइस का बैटरी मान है।

+1

की बीएलई सेवा यूयूआईडी है यह बैटरी स्तर को सत्य के रूप में देता है। –

+0

@ वुन @ राजेश नारवाल आपको 'यूयूआईडी' कैसे मिला? –

+0

@ एमएस। ब्लूटूथ एसआईजी साइट में परिभाषित यह एक सार्वजनिक सेवा है। [यहां देखें] (https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml) – IronBlossom

संबंधित मुद्दे