我能够通过连接到外部BLE设备发送数据高达20字节。如何发送的数据大于20字节。我已阅读,我们必须要么对数据进行分段或分割特性所需的零部件。如果我认为我的数据是32个字节,你能告诉我的变化,我需要让我的code得到这个工作?以下是从我的code所需的片段:
I am able to send data upto 20 bytes by connecting to an external BLE device. How do I send data greater than 20 bytes. I have read that we have to either fragment the data or split characteristic to required parts. If I assume my data is 32 bytes, could you tell me changes I need to make in my code to get this working? Following are the required snippets from my code:
public boolean send(byte[] data) {
if (mBluetoothGatt == null || mBluetoothGattService == null) {
Log.w(TAG, "BluetoothGatt not initialized");
return false;
}
BluetoothGattCharacteristic characteristic =
mBluetoothGattService.getCharacteristic(UUID_SEND);
if (characteristic == null) {
Log.w(TAG, "Send characteristic not found");
return false;
}
characteristic.setValue(data);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
return mBluetoothGatt.writeCharacteristic(characteristic);
}
这是在code口用于发送数据。的发送功能用于在以下onclick事件
This is the code I used for sending the data. The "send" function is used in the following onclick event.
sendValueButton = (Button) findViewById(R.id.sendValue);
sendValueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = dataEdit.getText().toString();
yableeService.send(text.getBytes());
}
});
在字符串文本
大于20字节则只有前20个字节被接收。如何纠正呢?
When the String text
is greater than 20 bytes then only the first 20 bytes are received. How to rectify this?
要测试发送多个特点我尝试这样做:
To test sending multiple characteristics I tried this:
sendValueButton = (Button) findViewById(R.id.sendValue);
sendValueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = "Test1";
yableeService.send(text.getBytes());
text = "Test2";
yableeService.send(text.getBytes());
text = "Test3";
yableeService.send(text.getBytes());
}
});
不过,我只收到Test3的即最后一个特点。没有我犯了什么错误?我是新来的BLE所以请忽略任何的naiveness
But I only received "Test3" i.e. the last characteristic. What mistake did I commit? I am new to BLE so please ignore any naiveness
编辑:的
接受的答案后的的人谁认为这一点。
After accepting answer for anyone who views this later.
有两个的方式来做到这一点。
1.分割你的数据,写在一个循环为选定的答案呢。
2.分割你的数据和写入使用回调即 onCharacterisitcWrite()
。这将节省您的错误,如果在写入过程中有任何
There are two ways to accomplish this.
1. Split up your data and write in a loop as the selected answer does.
2. Split up your data and write using callback i.e. onCharacterisitcWrite()
. This will save you from errors if there were any during writing.
不过的的最重要的的的写操作使用视频下载(200)
如果你只写和没有等待固件的响应。这将确保所有数据的到达。如果没有睡眠
我总是得到最后一个数据包。如果您发现该接受的答案,他还使用睡眠
之间。
But most important between the writes use a Thread.sleep(200)
if you are only writing and not waiting for a response from the firmware. This will ensure that all of your data reaches. Without the sleep
I was always getting the last packet. If you notice the accepted answer he has also used sleep
in between.
BLE允许你传输最大为20个字节。
BLE allow you transfer maximum is 20 bytes.
如果你想发送更多的20个字节,你应该定义数组byte []的包括你要多少包。
If you want send more 20 bytes, you should define array byte[] include how many packet you want.
例正常工作,如果你想发送< 160个字符(160字节)。
Example worked fine if you want send < 160 characters (160 bytes).
P / S:让编辑其次为u想要的。不跟着我到底。的
其实,当我们使用BLE,移动端和固件端需要建立密钥(例如,0×03 ...)双方之间定义连接门。
Actually, when we are using BLE, mobile side and firmware side need set up the Key (ex. 0x03 ...) to define the connection gate among both sides.
我们的想法是:
当我们仍然在继续传输的数据包,不能是最后一个。门是字节[1] = 0×01
。
如果我们发送的最后一个,门是字节[1] = 0×00
。
If we send the last one, The gate is byte[1] = 0x00
.
数据敷设渠道(20字节):
The data contruction (20 bytes):
1 - 字节1
- 定义门ID
:恩。邮件门ID 字节[0] = 0×03
。
1 - Byte 1
- Define the Gate ID
: ex. Message gate ID byte[0] = 0x03
.
2 - 字节2
- 定义的再认识
:是最后一个分组 0×00
或继续发送报文 0×01
。
2 - Byte 2
- Define the recognization
: Is the last packet 0x00
or continue sending packets 0x01
.
3 - 字节3
(应该是18个字节后减去字节1
&安培; 字节2
) - 附加邮件内容在这里
3 - Byte 3
(Should be 18 bytes after minus Byte 1
& Byte 2
) - Attach the message content in here.
应该读code下面请的
下面是如约发信息了许多包,每包:字节[20]
Below is example about send Message with many packets, each packet : byte[20].
private void sendMessage(BluetoothGattCharacteristic characteristic, String CHARACTERS){
byte[] initial_packet = new byte[3];
/**
* Indicate byte
*/
initial_packet[0] = BLE.INITIAL_MESSAGE_PACKET;
if (Long.valueOf(
String.valueOf(CHARACTERS.length() + initial_packet.length))
> BLE.DEFAULT_BYTES_VIA_BLE) {
sendingContinuePacket(characteristic, initial_packet, CHARACTERS);
} else {
sendingLastPacket(characteristic, initial_packet, CHARACTERS);
}
}
private void sendingContinuePacket(BluetoothGattCharacteristic characteristic,
byte[] initial_packet, String CHARACTERS){
/**
* TODO If data length > Default data can sent via BLE : 20 bytes
*/
// Check the data length is large how many times with Default Data (BLE)
int times = Byte.valueOf(String.valueOf(
CHARACTERS.length() / BLE.DEFAULT_BYTES_IN_CONTINUE_PACKET));
Log.i(TAG, "CHARACTERS.length() " + CHARACTERS.length());
Log.i(TAG, "times " + times);
// TODO
// 100 : Success
// 101 : Error
byte[] sending_continue_hex = new byte[BLE.DEFAULT_BYTES_IN_CONTINUE_PACKET];
for (int time = 0; time <= times; time++) {
/**
* Wait second before sending continue packet
*/
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (time == times) {
Log.i(TAG, "LAST PACKET ");
/**
* If can not have enough characters to send continue packet,
* This is the last packet will be sent to the band
*/
/**
* Packet length byte :
*/
/**
* Length of last packet
*/
int character_length = CHARACTERS.length()
- BLE.DEFAULT_BYTES_IN_CONTINUE_PACKET*times;
initial_packet[1] = Byte.valueOf(String.valueOf(character_length
+ BLE.INITIAL_MESSAGE_PACKET_LENGTH));
initial_packet[2] = BLE.SENDING_LAST_PACKET;
Log.i(TAG, "character_length " + character_length);
/**
* Message
*/
// Hex file
byte[] sending_last_hex = new byte[character_length];
// Hex file : Get next bytes
for (int i = 0; i < sending_last_hex.length; i++) {
sending_last_hex[i] =
CHARACTERS.getBytes()[sending_continue_hex.length*time + i];
}
// Merge byte[]
byte[] last_packet =
new byte[character_length + BLE.INITIAL_MESSAGE_PACKET_LENGTH];
System.arraycopy(initial_packet, 0, last_packet,
0, initial_packet.length);
System.arraycopy(sending_last_hex, 0, last_packet,
initial_packet.length, sending_last_hex.length);
// Set value for characteristic
characteristic.setValue(last_packet);
} else {
Log.i(TAG, "CONTINUE PACKET ");
/**
* If have enough characters to send continue packet,
* This is the continue packet will be sent to the band
*/
/**
* Packet length byte
*/
int character_length = sending_continue_hex.length;
/**
* TODO Default Length : 20 Bytes
*/
initial_packet[1] = Byte.valueOf(String.valueOf(
character_length + BLE.INITIAL_MESSAGE_PACKET_LENGTH));
/**
* If sent data length > 20 bytes (Default : BLE allow send 20 bytes one time)
* -> set 01 : continue sending next packet
* else or if after sent until data length < 20 bytes
* -> set 00 : last packet
*/
initial_packet[2] = BLE.SENDING_CONTINUE_PACKET;
/**
* Message
*/
// Hex file : Get first 17 bytes
for (int i = 0; i < sending_continue_hex.length; i++) {
Log.i(TAG, "Send stt : "
+ (sending_continue_hex.length*time + i));
// Get next bytes
sending_continue_hex[i] =
CHARACTERS.getBytes()[sending_continue_hex.length*time + i];
}
// Merge byte[]
byte[] sending_continue_packet =
new byte[character_length + BLE.INITIAL_MESSAGE_PACKET_LENGTH];
System.arraycopy(initial_packet, 0, sending_continue_packet,
0, initial_packet.length);
System.arraycopy(sending_continue_hex, 0, sending_continue_packet,
initial_packet.length, sending_continue_hex.length);
// Set value for characteristic
characteristic.setValue(sending_continue_packet);
}
// Write characteristic via BLE
mBluetoothGatt.writeCharacteristic(characteristic);
}
}
public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic,
String data) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return false;
}
if (ActivityBLEController.IS_FIRST_TIME) {
/**
* In the first time,
* should send the Title
*/
byte[] merge_title = sendTitle(data);
// Set value for characteristic
characteristic.setValue(merge_title);
// Write characteristic via BLE
mBluetoothGatt.writeCharacteristic(characteristic);
// Reset
ActivityBLEController.IS_FIRST_TIME = false;
return true;
} else {
/**
* In the second time,
* should send the Message
*/
if (data.length() <= BLE.LIMIT_CHARACTERS) {
sendMessage(characteristic, data);
// Reset
ActivityBLEController.IS_FIRST_TIME = true;
return true;
} else {
// Typed character
typed_character = data.length();
return false;
}
}
}
上一篇:安卓应用程序级的字体大小preference字体大小、应用程序、preference
下一篇:如何从github上导入Eclipse库项目到Android Studio项目?项目、Eclipse、github、Android