2017-08-05 130 views
0

我正在做一個藍牙應用程序,我連接任何設備並做一些工作。我有很多活動。我在BlutoothConnectionService.java課中做了所有連接材料。在我的BondedDevicesActivity中,單擊我想要連接的設備。然後服務工作,並打開另一個活動。然後我點擊一些按鈕打開另一個活動。在所有活動中,我都通過藍牙發送一些信息。因此,無論何時我打開另一個活動,我都使用BlutoothConnectionService.java類。問題在於這個類,它總是試圖連接到設備。我希望一旦連接完成,它就再也不會嘗試它,直到連接死亡。但我無法弄清楚如何做到這一點。我知道有一種叫做isConnected()的方法,但我不知道把它放在服務類中。這裏是我的服務代碼:藍牙IsConnected()

public class BluetoothConnectionService { 
private static final String TAG = "BluetoothConnectionSrvc"; 
private static final UUID connectionUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
private final BluetoothAdapter bluetoothAdapter; 
Context ctx; 
private ConnectThread connectThread; 
private BluetoothDevice bluetoothDevice; 
private UUID deviceUUID; 
ProgressDialog progressDialog; 
private ConnectedThread connectedThread; 
String incomingMessage; 

public BluetoothConnectionService(Context context, BluetoothDevice device, UUID uuid) { 
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    ctx = context; 
    startClient(device, uuid); 
} 

public void startClient(BluetoothDevice device, UUID uuid) { 
    Log.d(TAG, "startClient: Started."); 
    connectThread = new ConnectThread(device, uuid); 
    connectThread.start(); 
} 

private class ConnectThread extends Thread { 
    private BluetoothSocket bluetoothSocket; 

    public ConnectThread(BluetoothDevice device, UUID uuid) { 
     Log.d(TAG, "ConnectThread: started."); 
     bluetoothDevice = device; 
     deviceUUID = uuid; 
    } 

    public void run() { 
     Log.i(TAG, "ConnectThread: Run."); 
     BluetoothSocket tmp = null; 
     try { 
      Log.d(TAG, "ConnectThread: Trying to create RFcommSocket using UUID: " + connectionUUID); 
      tmp = bluetoothDevice.createRfcommSocketToServiceRecord(deviceUUID); 
      progressDialog = ProgressDialog.show(ctx, "Cihaza Bağlanılıyor", "Lütfen Bekleyiniz...", true); 
     } catch (Exception e) { 
      progressDialog.dismiss(); 
      e.printStackTrace(); 
      Log.e(TAG, "ConnectThread: Couldn't create RFcommSocket" + e.getMessage()); 
      showMessage("Cihaza bağlanılamadı, lütfen bağlantınızı kontrol ederek tekrar deneyiniz."); 
      AnaEkranActivity.instance.finish(); 
     } 

     if (tmp != null) { 
      bluetoothSocket = tmp; 
      bluetoothAdapter.cancelDiscovery(); 

      try { 
       bluetoothSocket.connect(); 
       Log.d(TAG, "run: ConnectionThread connected."); 
       connected(bluetoothSocket); 
      } catch (Exception e) { 
       progressDialog.dismiss(); 
       e.printStackTrace(); 
       showMessage("Cihaza bağlanılamadı, lütfen bağlantınızı kontrol ederek tekrar deneyiniz."); 
       AnaEkranActivity.instance.finish(); 
       try { 
        bluetoothSocket.close(); 
        Log.d(TAG, "run: Closed Socket."); 
       } catch (Exception e1) { 
        e1.printStackTrace(); 
        Log.e(TAG, "ConnectThread: run: Unable to close connection in socket" + e1.getMessage()); 
       } 
       Log.d(TAG, "run: ConnectThread: Could not connect to UUID: " + connectionUUID); 
      } 
     } 
    } 
} 

public void connected(BluetoothSocket socket) { 
    Log.d(TAG, "Connected: Starting."); 
    connectedThread = new ConnectedThread(socket); 
    connectedThread.start(); 
} 

private class ConnectedThread extends Thread { 
    private final BluetoothSocket bluetoothSocket; 
    private final InputStream inputStream; 
    private final OutputStream outputStream; 

    public ConnectedThread(BluetoothSocket socket) { 
     Log.d(TAG, "ConnectedThread: Starting."); 
     bluetoothSocket = socket; 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     try { 
      progressDialog.dismiss(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     try { 
      tmpIn = bluetoothSocket.getInputStream(); 
      tmpOut = bluetoothSocket.getOutputStream(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     inputStream = tmpIn; 
     outputStream = tmpOut; 
    } 

    public void run() { 
     byte[] readBuffer = new byte[1024]; 
     int readBytes; 

     while (true) { 
      try { 
       readBytes = inputStream.read(readBuffer); 
       incomingMessage = new String(readBuffer, 0, readBytes); 
       Log.d(TAG, "InputStream: " + incomingMessage); 
       showMessage(incomingMessage); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       Log.e(TAG, "read: Error reading from inputStream." + e.getMessage()); 
       showMessage("Connection is dead."); 
       AnaEkranActivity.instance.finish(); 
       break; 
      } 
     } 
    } 

    public void write(byte[] writeBytes) { 
     String text = new String(writeBytes, Charset.defaultCharset()); 
     Log.d(TAG, "write: Writing to outputStream: " + text); 
     try { 
      outputStream.write(writeBytes); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.e(TAG, "write: Error writing to outputStream." + e.getMessage()); 
      showMessage("Error while writing."); 
     } 
    } 
} 

public void write (byte[] out) { 
    Log.d(TAG, "write: Write Called."); 
    connectedThread.write(out); 
} 

public String read() { 
    Log.d(TAG, "read: Read Called."); 
    connectedThread.run(); 
    return incomingMessage; 
} 

public void showMessage(final String toastMessage) { 
    AnaEkranActivity.instance.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(ctx, toastMessage, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

}

任何想法?

回答

0

在android文檔中,你會得到這個答案。我們在藍牙聊天應用程序中使用相同的東西。這裏是鏈接Bluetooth chat app

它給你提供有關連接的信息。並且this explanation

會幫你解決這個問題。一次清楚地通過你會得到解決方案。

+0

不幸的是,我深入研究了這些文檔,但對於我的問題絕對沒有任何用處。 – Burak