I won't say much nonsense, let's just read the code~
// To extends TabActivitypublic class Main_activity extends TabActivity { private TabHost tabHost;// Create Tabhost control protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); tabHost = getTabHost(); addTab("act1", "Interface 1", blue_tooth_Activity.class);//Add to addTab("act2", "Interface 2", map_Activity.class); addTab("act3", "Interface 3", plane_parameter_activity.class); setContentView(tabHost);// show } /** * Add Activity tag * @param tag logo * @param title tag title * @param clazz activated interface */ private void addTab(String tag, String title, Class clazz) { tabSpec = (tag); (title); Intent intent = new Intent(getApplicationContext(),clazz); (intent); (tabSpec); } @Override protected void onStop() { (); } }
Supplementary knowledge:How to break the maximum length of data transmitted in Android BLE
Many friends have been troubled by one thing:
Want to transfer longer data to the gatt server (usually a Bluetooth smart device, that is, a device with only BLE functions), but through
writeCharacteristic(BluetoothGattCharacteristic)
When I came to write, I found that at most 20 bytes of data can be written.
This article will answer the following questions:
1) Why is it 20?
2) How to break through 20?
3) How to achieve it more elegantly?
The first question is, why is it limited to 20 bytes?
The core spec defines the default MTU of ATT to be 23 bytes. After removing one byte of ATT's opcode and two bytes of ATT's handle, the remaining 20 bytes are left for GATT.
Considering that some Bluetooth smart devices have weak functions and dare not use memory space too luxuriously, the core spec stipulates that each device must support an MTU of 23.
In the early stage of connecting the two devices, everyone is like a new friend, not knowing the other party's background, so they strictly follow the routine, that is, sending up to 20 bytes at a time, which is the safest.
Since the maximum length of ATT is 512byte,
Therefore, it is generally believed that the maximum length of MTU is 512 bytes, and it doesn’t make much sense no matter how big it is. You cannot send a data of ATT of more than 512, just like Monkey King cannot run away from the Five Elements Mountain.
Therefore, the maximum length of ATT's MTU can be regarded as 512 bytes.
The second question is, how to break through 20?
It's very simple. Just change the MTU of the transmitted ATT. After friendly negotiation, we get the results that both parties want, which 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 loudly how much you want to pass at once, just call the above interface, 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) { (gatt, mtu, status); if (status == BluetoothGatt.GATT_SUCCESS) { = mtu;//local var to record MTU size } }
After that, you can happily send supportedMTU-3 length data.
The third question is, how to achieve it elegantly?
As the saying goes, a man is afraid that his concubine will be unintentional if he has feelings. What if the other party’s device does not agree to your request?
For apps, you generally know how much data you want to send. For example, if you want to send 100 bytes at a time, then try applying for 103 first. If you fail, apply for 53, that is, the dichotomy method, and the rest can only be distributed in segments and sent.
Generally speaking, the developers of the app and the developers of the peer device are all in the same group. This is a good thing. They can discuss the amount of MTU based on the hardware situation of their own devices.
In short, putting things on the desktop and doing them in advance will make your program more professional.
The above article Android uses tabhost to implement interface switching. Each interface is an independent activity operation. This is all the content I share with you. I hope you can give you a reference and I hope you can support me more.