我通过蓝牙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();
}
活动类中的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”上。我该怎么做呢 ?。
提前致谢
答案 0 :(得分:0)
我创建了一个名为BLEGattCallback的类,它接收更新。在这个课程中,我实现了你提到的界面。接收和显示我的数据 发送和意图将所需的信息放入Bundle中。只需在Activity中创建一个BroadcastReceiver并为Action注册它。从Bundle读取所有数据并通过调用TextView.setText(String)显示它。
UUIDManager只是我创建的一个类,用于将特性转换为可读的字符串。
@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!");
}
}