9

Tôi đang phát triển một ứng dụng mà tôi phải kết nối với thiết bị Bluetooth trên Android 4.3.Cách lấy mức pin sau khi kết nối với thiết bị BLE?

Và tôi muốn lấy mức pin bằng cách sử dụng Battery_ServicePin_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);); 
    } 

Nhưng giá trị của mBluetoothGatt.readCharacteristic(batteryLevel); không phải là giá trị mức pin

Làm thế nào để đọc pin ????

+0

bạn nhận được giá trị nào? –

+0

giá trị của mBluetoothGatt.readCharacteristic (pinLevel); là "true" – Wun

+0

xin lỗi, tôi không nghĩ rằng tôi có thể giúp ... có thể điều này sẽ: http://developer.samsung.com/forum/board/thread/view.do?boardName=SDK&messageId=240110 –

Trả lời

19

Tôi đã giải quyết được sự cố này.

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

Khi bạn gọi hàm getbattery(), nó sẽ gọi onCharacteristicRead. và onCharacteristicRead sẽ gọi broadcastUpdate và truyền đặc tính và hành động cho nó.

characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) trong broadcastUpdate là giá trị pin của thiết bị BLE.

+1

Nó mang lại mức pin đúng. –

+0

@Wun @RajeshNarwal Bạn nhận được 'UUID' như thế nào? –

+0

@ M.S. Một dịch vụ công cộng của nó, được xác định trong trang web SIG Bluetooth. [Xem tại đây] (https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml) – IronBlossom

Các vấn đề liên quan