2013-03-28 67 views
0

我的BleFindMeClient與TI Mini Mini Keyfob一起在HTC 1X +上工作。我試圖稍微擴展它以讀取電池電量(不註冊電池更新)。我可以從BTool做到這一點,但我的Android PROG失敗,我得到的調試消息:讀取電池電量 - Broadcom BLE堆棧和HTC 1x +

getCharacteristic - 沒有找到

服務數據這是什麼意思?我在哪裏可以找到這個和其他錯誤消息的含義?

很明顯,我可以寫出特徵,因爲我可以設置警報。但有一些相當基本的東西,我沒有理解閱讀特徵,但我找不到示例代碼。

請問有人能給我一個更好的代碼片段,或者在下面發現一些愚蠢的東西?

public class BatteryProfileClient extends BleClientProfile { 

private static String TAG = "BatteryProfileClient"; 
static public BleGattID myUuid = new BleGattID("0000180f-0000-1000-8000-00805f9b34fb"); 
private static final BleGattID BATTERY_CHARACTERISTIC = new BleGattID("00002a19-0000-1000-8000-00805f9b34fb"); 

private BatteryServiceClient mBatteryServiceClient = new BatteryServiceClient(); 

public BatteryProfileClient(Context context) { 
    super(context, myUuid); 

    Log.d(TAG, "Instantiated"); 

    ArrayList<BleClientService> services = new ArrayList<BleClientService>(); 
    services.add(mBatteryServiceClient); 

    init(services, null); 
} 


public void batt(BluetoothDevice device) { 
    BleCharacteristic battLevelCharacteristic = mBatteryServiceClient.getCharacteristic(device, BATTERY_CHARACTERISTIC); 
    byte b[] = battLevelCharacteristic.getValue(); 
    Log.d(TAG, "battery " + b); 
} 

}

回答

0

不知道如果IM晚了,但應該解決您的問題 -

public int getbattery(BluetoothGatt mBluetoothGatt) { 

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

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

Hi Bro,您可以在單一服務中共享電池和人力資源監控器的完整代碼。 – Shankar