Witam,
mam taki problem, że moja aplikacja nie odbiera niektórych "ramek"
wysyłam z terminala przez rs do btm222 ramkę danych hex
68 1A 1A 68 00 01 04 1A 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 16
i jest ok, ale jak już wyślę ramkę
68 1A 1A 68 00 05 00 1A 00 1E 00 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 E1 10 16
to niestety jej nie odbiera. Poniżej metody połączenia bt i obsługi bufora.
metoda msg wyswietla toasty, które są dla mnie "checkpointami"
czy ktoś może mi powiedzieć o co chodzi?
private class ConnectBT extends AsyncTask<Void, Void, Void>{
private boolean ConnectSuccess = true; //if it's here, it's almost connected
@Override
protected void onPreExecute()
{
progress = ProgressDialog.show(DeviceActivity.this, "Łączenie...", "Proszę czekać!!!"); //show a progress dialog
}
@Override
protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background
{
try
{
if (btSocket == null || !isBtConnected)
{
myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();//start connection
}
}
catch (IOException e)
{
ConnectSuccess = false;//if the try failed, you can check the exception here
}
return null;
}
@Override
protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
{
super.onPostExecute(result);
if (!ConnectSuccess)
{
msg("Nie udało się nawiązać połączenia z wybranym urządzeniem.");
finish();
}
else
{
msg("Połączono.");
isBtConnected = true;
mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
doItAferFullConnect();
}
progress.dismiss();
}
}
public class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
//creation of the connect thread
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
//Create I/O streams for connection
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[256];
int bytes;
// Keep looping to listen for received messages
while (true) {
try {
bytes = mmInStream.read(buffer); //read bytes from input buffer
String readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity via handler
bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
} catch (IOException e) {
break;
}
}
}
//write method
public void write(byte[] input) {
byte[] msgBuffer = input;
try {
mmOutStream.write(msgBuffer); //write bytes over BT connection via outstream
} catch (IOException e) {
//if you cannot write, close the application
Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
finish();
}
}
}
private void addToBuffer(byte[] receivedBytes){
byte[] temp_0 = buffer_0;
byte[] temp_1 = receivedBytes;
byte[] buff_1 = new byte[temp_0.length + temp_1.length];
for(int i=0; i<buff_1.length; ++i){
if(i<temp_0.length) buff_1[i] = temp_0[i];
else buff_1[i] = temp_1[i - temp_0.length];
}
buffer_0 = buff_1;
bufferControl();
}
private void bufferControl(){
if(buffer_0.length>3){ //dlugosc na glowka = 4bajty
while(true){
if(buffer_0[0] == 0x68 && buffer_0[1] == 0x1A && buffer_0[2] == 0x1A && buffer_0[3] == 0x68){
if(buffer_0.length>31){
if(buffer_0[31] == 0x16){
byte[] frame;// = new byte[32];
frame = getFrameFromBuffer();
buffer_0 = moveBufferLeft(32);
msg("dobra ramka");
commandCenter.received(frame);
}
}
break;
}
else{
buffer_0 = moveBufferLeft(1);//przesuniecie bufora
if (buffer_0.length<4) break;//jesli naglowek nie miesci sie w buforze
}
}
}
}