0

我通过蓝牙 LE 使用智能手机接收的数据出现在我的服务类中的这种方法中

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

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

};

有人可以告诉我这两种方法之间的关系吗?我从远程设备接收到一组数据,我希望数据显示在 Textview“bsltv”上。我该怎么做呢 ?。

提前致谢

4

1 回答 1

0

我创建了一个名为 BLEGattCallback 的类来接收更新。在这个类中,我实现了你提到的接口。为了接收和显示我刚刚发送的数据和 Intent 以及放入 Bundle 中的所需信息。只需在您的 Activity 中创建一个 BroadcastReceiver 并将其注册为 Action。从 Bundle 中读取所有数据并通过调用 TextView.setText(String) 进行显示。

UUIDManager 只是我创建的一个类,用于将 Characteristic 转换为可读的字符串。

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    super.onCharacteristicRead(gatt, characteristic, status);

    if(status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(CLASSNAME, "onCharacteristicRead for Characteristic: " + characteristic.getUuid().toString());
        Log.d(CLASSNAME, "onCharacteristicRead for: " + UUIDManager.getCharacteristic(characteristic.getUuid().toString()));

        Intent intent = new Intent();
        intent.setAction(ACTION_READ_DATA_AVAILABLE);       

        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            StringBuilder stringBuilder = new StringBuilder(data.length);

            for(byte byteChar : data) {
                stringBuilder.append(String.format("%02X ", byteChar));
            }

            Bundle extras = new Bundle();
            extras.putString(EXTRA_CHARACTERISTIC_UUID, characteristic.getUuid().toString());
            extras.putString(EXTRA_STRING_DATA, stringBuilder.toString());
            extras.putByteArray(EXTRA_RAW_DATA, characteristic.getValue());             
            extras.putString(EXTRA_DEVICE_ADDRESS, gatt.getDevice().getAddress());
            intent.putExtras(extras);
        }

        m_Context.sendBroadcast(intent);
    } else {
        Log.e(CLASSNAME, "onCharacteristicRead was not successfull!");
    }
}
于 2014-03-12T11:14:22.380 回答