我正在开发一个让用户连接到BLE设备的应用程序,然后在从BLE外围设备读取20个值后,它会移动到一个新的活动/屏幕,在那里我从我读取的20个值中创建一个图像。我希望能够读取一组新的20个值,但不会离开显示图像的屏幕。我怎样才能做到这一点?
在我阅读BLE特征的活动中,我有这个功能:
public class BluetoothLeService extends Service {
private final static String TAG = BluetoothLeService.class.getSimpleName();
....
public int read_counter = 0;
public int measurement_arr[] = new int[20];
// Implements callback methods for GATT events that the app cares about. For example,
// connection change and services discovered.
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
....
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) { // IF THE READ OPERATION WAS SUCCESSFUL.
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.readCharacteristic(characteristic);
}
....
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
final byte[] data = characteristic.getValue(); // data is presented as a byte array over BLE characteristics.
measurement_arr[read_counter] = (int) data[0]; // Read data byte.
// Use hex formatting.
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString()); // THIS IS WHERE DATA IS BEING READ.
}
read_counter += 1; // after value read, increment the count.
if (read_counter < 20){ // Receive 20 packets of data.
Log.d("MEASUREMENT", "Reading new characteristic");
readCharacteristic(characteristic);
}
else {
Log.d("Finished BLE read", "Moving to next activity.");
read_counter = 0;
// Go to next activity where we show image.
Intent show_image = new Intent(this, Reconstruction.class);
show_image.putExtra("myArr", measurement_arr); // Go to acivity where we reconstruct image.
show_image.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(show_image);
}
sendBroadcast(intent);
}
我的第二个活动,使用Canvas显示图像,然后具有以下代码。
public class Reconstruction extends AppCompatActivity {
public int data[] = new int[20];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
// Get Bundle object that contain the array
Bundle extras = getIntent().getExtras();
// Extract the array from the Bundle object
data = extras.getIntArray("myArr");
// Output the array
for(int item:data){
Log.i("2nd_ACTIVITY", String.valueOf(item));
}
}
public class MyView extends View
{
Paint paint = null;
public MyView(Context context)
{
super(context);
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas)
{
... make image
...After making the image, how do I go back to the readCharacteristic function from the previous activity, but without changing what the display is showing?
}
}
}
我已经在线阅读了有关将第二个活动的上下文传递给第一个活动的一些内容,但我不太明白这一点,而且我也不太确定Context是如何工作的。
答案 0 :(得分:1)
请勿使用指向其他活动内部活动的链接。它可能导致内存泄漏。
关于您的问题,您可以使用广播接收器在活动之间进行通信,或使用与您的BLE设备通信的Service
简单使用Broadcast Receiver:
class YourActivity extends Activity {
private BroadcastReceiver mMyReceiver;
protected void onCreate(Bundle data) {
...
mMyReceiver = new MyReceiver();
}
public void onStart() {
registerReceiver(mMyReceiver, new IntentFilter("your action"));
}
public void onStop() {
unregisterReceiver(mMyReceiver);
}
public void onDestroy() {
mMyReceiver = null;
}
// Inner class has a link to YourActivity instance
private class MyReceiver extends BroadcastReceiver {
public void onReceive(Intent intent) {
// procces messages here
}
}
}
// Calling:
context.sendBroadcast(new Intent("your action"));