简体   繁体   English

从TI CC2540蓝牙低功耗套件接收数据

[英]Receive data from TI CC2540 Bluetooth Low Energy kit

I have a CC2540 BLE kit and i want to see the battery level of the kit in my smartphone app. 我有一个CC2540 BLE套件,我想在我的智能手机应用程序中查看该套件的电池电量。 I have made the code, which gets the battery level from the kit. 我已经编写了代码,可以从套件中获取电池电量。 The battery level is called BodySensor in this case: 在这种情况下,电池电量称为BodySensor:

First, i will go into the characteristic value of the battery service part for the kit. 首先,我将介绍该套件的电池服务部分的特征值。 So i use the Service and Characteristic UUID's for the battery service: 因此,我将“服务和特征UUID”用于电池服务:

public void getBodySensorLoc(BluetoothDevice device) 
{
    Log.i(TAG, "getBodySensorLoc");
    BluetoothGattService mHRP = mBluetoothGatt.getService(device, HRP_SERVICE);
    if (mHRP == null) 
    {
        Log.e(TAG, "getBodySensorLoc: mHRP = null");

        return;
    }
    BluetoothGattCharacteristic mBSLcharac = mHRP.getCharacteristic(BODY_SENSOR_LOCATION);
    if (mBSLcharac == null) {
        Log.e(TAG, "getBodySensorLoc: mBSLcharac = null");
        return;
    }
    mBluetoothGatt.readCharacteristic(mBSLcharac);
}

My purpose is to read the battery level, when pressing a button in my application. 我的目的是在我的应用程序中按一个按钮时读取电池电量。 So i implement this button in my Activity class: 所以我在Activity类中实现了这个按钮:

    ((Button) findViewById(R.id.btn_BSL)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            mService.getBodySensorLoc(mDevice);
        }
    });

This kit will automatically send the battery level back to the smartphone. 该套件将自动将电池电量发送回智能手机。 So i will set focus on onCharacteristicRead method of BluetoothGattCallback part in my code: 因此,我将在代码中将重点放在BluetoothGattCallback部分的onCharacteristicRead方法上:

    private BluetoothGattCallback mGattCallbacks = new BluetoothGattCallback() 
{

    public void onCharacteristicRead(BluetoothGattCharacteristic charac, int status) 
    {
        UUID charUuid = charac.getUuid();
        Bundle mBundle = new Bundle();
        Message msg = Message.obtain(mActivityHandler, HRP_VALUE_MSG);
        Log.i(TAG, "onCharacteristicRead");
        if (charUuid.equals(BODY_SENSOR_LOCATION))
            mBundle.putByteArray(BSL_VALUE, charac.getValue());
        msg.setData(mBundle);
        msg.sendToTarget();
    }
};

I want the application to show the received battery level in a textview. 我希望应用程序在文本视图中显示接收到的电池电量。 The kit sends the battery level as a integer, so if the level is 70%, it sends "70". 该套件以整数形式发送电池电量,因此,如果电量为70%,则会发送“ 70”。 The code includes a Handler in the Activity class: 该代码在Activity类中包含一个Handler:

private Handler mHandler = new Handler() 
{
    @Override
    public void handleMessage(Message msg) 
    {
        switch (msg.what) 
        {
        case HRPService.HRP_VALUE_MSG:
            Log.d(TAG, "mHandler.HRP_VALUE_MSG");
            Bundle data1 = msg.getData();
            final byte[] bslval = data1.getByteArray(HRPService.BSL_VALUE);
            runOnUiThread(new Runnable() 
            {
                public void run() 
                {
                    if (bslval != null) 
                    {
                        try {
                            Log.i(TAG, "BYTE BSL VAL =" + bslval[0]);
                            TextView bsltv = (TextView) findViewById(R.id.BodySensorLocation);
                            bsltv.setText("\t" + mContext.getString(R.string.BodySensorLocation)
                                    + getBodySensorLocation(bslval[0]));
                        } catch (Exception e) 
                        {
                            Log.e(TAG, e.toString());

                        }

                    }
                }
            });

        default:
            super.handleMessage(msg);
        }
    }
};

The problem is that the textview doesn't show anything, when i press this button in my application after i connected my smartphone with the kit. 问题是,当我将智能手机与套件连接后,在应用程序中按此按钮时,textview不会显示任何内容。 Can anyone tell me, where the problem is ?. 谁能告诉我问题出在哪里?

Thanks in advance 提前致谢

Unsure what exact kit you have. 不确定您拥有的确切套件。 Unsure if they all have battery level measuring hardware on board. 不确定它们上是否都装有电池电量测量硬件。 Maybe this is the reason. 也许这就是原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM