我使用android示例代碼進行修改。只希望接收包 但是,我的代碼只修改這裏藍牙SPP收到一些包的幀可能丟失或?
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
mTitle.setText(R.string.title_connected_to);
mTitle.append(mConnectedDeviceName);
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
mTitle.setText(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
mTitle.setText(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add(writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
//String readMessage = new String(readBuf, 0, msg.arg1);
//String readMessage = BytesTrans_fill.bytesToHexString(readBuf);
Log.d("read", BytesTrans.bytes2HexString(readBuf,msg.arg1));
String readMessage = BytesTrans.bytes2HexString(readBuf,msg.arg1);
ppV.setText(ppV.getText().toString() + readMessage + "★");
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(
DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};
和BluetoothChatService
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
,並添加此功能
package com.example.android.BluetoothChat;
public class BytesTrans {
public static String byte2HexString(byte b) {
String ret = "";
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase() + " ";
return ret;
}
public static String bytes2HexString(byte[] b, int count) {
String ret = "";
for (int i = 0; i < count; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase() + " ";
}
return ret;
}
/*public static String String2byte(String b)
{
String[] ttt;
for (int i = 0; i < b.length(); i++)
{
for (int j= i; j<=i+1; j++)
{
ttt[i] = b;
}
}
String ttmp = "";
String tmp = "";
ret += tmp;
}*/
public static int hexToTen(String b) {
int D2 = Integer.parseInt(b,16);
return D2;
}
}
但此方案顯示有時甚至不是我的發送包的框架
我發送類似th是:
aa07210820001202140934390000000000000000000000000000000000000000000297c0fe6b
但有時收到包:
aa000297c0fe6b02131452470000000000000000000000000000000000000000000297c0fe6b
我怎樣才能改變我的代碼,即可獲得完整的軟件包的框架
感謝您的回答,非常詳細。因爲這隻使用bluetoothChat例子,所以我只貼修改地方。我不明白,爲什麼這個代碼會產生這個問題。直到現在我還沒有搜索這個bug。你能給我一些教導嗎?非常感謝。 – user1206629 2012-02-15 01:09:44