Android uses tabhost to switch interfaces and each interface is an independent activity operation

  • 2021-12-09 09:51:24
  • OfStack

I won't talk too much, let's just look at the code ~


//  To extends TabActivity
public class Main_activity extends TabActivity {
  private TabHost tabHost;//  Establish Tabhost Control 

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    tabHost = getTabHost();
    addTab("act1", " Interface 1", blue_tooth_Activity.class);// Add 
    addTab("act2", " Interface 2", map_Activity.class);
    addTab("act3", " Interface 3", plane_parameter_activity.class);

    setContentView(tabHost);//  Display 

  }
  /**
   *  Add Activity Label 
   * @param tag   Identification 
   * @param title  Label title 
   * @param clazz  Activated interface 
   */
  private void addTab(String tag, String title, Class clazz) {
    TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
    tabSpec.setIndicator(title);

    Intent intent = new Intent(getApplicationContext(),clazz);
    tabSpec.setContent(intent);
    tabHost.addTab(tabSpec);
  }

  @Override
  protected void onStop() {
    super.onStop();
  }
}

Supplementary knowledge: How to break the maximum length of data transmitted in Android and BLE

Many friends have been troubled by one thing:

Want to transmit 1 point long data to gatt server on gatt client (1 is usually a mobile phone) (1 is usually an Bluetooth smart device, that is, a device with only BLE function), but through

writeCharacteristic(BluetoothGattCharacteristic)

When I came to write, I found that I could only write up to 20 byte data.

This article will answer the following questions:

1) Why 20?

2) How to break through 20?

3) How to achieve it more gracefully?

The first question, why is it limited to 20 bytes?

core spec defines that the default MTU of ATT is 23 bytes. After removing opcode1 bytes of ATT and handle2 bytes of ATT, the remaining 20 bytes are reserved for GATT.

Considering that some Bluetooth smart devices have weak functions and dare not use memory space too extravagantly, core spec stipulates that every device must support MTU of 23.

At the initial stage of the connection between the two devices, everyone is like a new friend, and they don't know the details of each other, so it is safest to follow the routine strictly, that is, to send 20 bytes at most once.

Since the maximum length of ATT is 512byte,

Therefore, it is generally considered that the maximum length of MTU is 512 byte, and no matter how big it is, it is meaningless. You can't send a data of ATT exceeding 512, just like Sun Monkey can't run 5 rows of mountains.

Therefore, the maximum length of MTU of ATT can be regarded as 512 bytes.

The second question, how to break through 20?

Very simple, change the transmission of ATT MTU on the line, we through friendly negotiations, get the results both sides want, is the best. On Android (API 21), change the interface of ATT MTU to:

public boolean requestMtu (int mtu) Added in API level 21 Request an MTU size used for a given connection. When performing a write request operation (write without response), the data sent is truncated to the MTU size. This function may be used to request a larger MTU size to be able to send more data at once. A onMtuChanged(BluetoothGatt, int, int) callback will indicate whether this operation was successful. Requires BLUETOOTH permission. Returns true, if the new MTU value has been requested successfully

Say aloud how much you want to pass, just call the interface above, and then see the final result in the following function (of course, if your peripheral application changes MTU and succeeds, this callback will also be called):


@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
  super.onMtuChanged(gatt, mtu, status);
 
  if (status == BluetoothGatt.GATT_SUCCESS) {
    this.supportedMTU = mtu;//local var to record MTU size
  }
}

After that, you can happily send data as long as supportedMTU-3.

The third question, how to achieve it gracefully?

As the saying goes, if Lang is affectionate, he is afraid that his concubine has no intention. What if the other device disagrees with your request?

For app, 1 generally knows how much data you want to send at the maximum. For example, if you want to send 100 bytes at a time, try to apply for 103 under 1 first. If you fail, apply for 53 under 1, that is, 2 points, and the rest can only be sent by yourself.

Generally speaking, the developers of app and the developers of opposite devices are all in the same group, which is a good thing. They can discuss how much MTU should be according to the hardware conditions of their own devices.

In a word, putting things on the table and doing them well in advance will make your program more professional.


Related articles: