Android Open Source - bluetooth Connection Thread






From Project

Back to project page bluetooth.

License

The source code is released under:

MIT License

If you think the Android project bluetooth listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mauriciotogneri.bluetooth.connection.kernel;
//from   www  .  j a  v a 2  s  .co m
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

public abstract class ConnectionThread extends Thread
{
  private final BluetoothSocket socket;
  private final InputStream inputStream;
  private final OutputStream outputStream;
  private boolean connected = false;
  private volatile boolean lastMessageSent = true;
  
  private static final int BUFFER_SIZE = 1024;
  
  public ConnectionThread(BluetoothSocket socket) throws IOException
  {
    this.socket = socket;
    this.inputStream = socket.getInputStream();
    this.outputStream = socket.getOutputStream();
    this.connected = true;
  }
  
  @Override
  public void run()
  {
    byte[] buffer = new byte[ConnectionThread.BUFFER_SIZE];
    
    BluetoothDevice device = this.socket.getRemoteDevice();
    
    while (true)
    {
      try
      {
        int bytes = this.inputStream.read(buffer);
        
        try
        {
          byte[] message = Arrays.copyOfRange(buffer, 0, bytes);
          onReceive(device, message);
        }
        catch (Exception e)
        {
          Log.e("TEST", "ERROR", e);
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
        break;
      }
    }
    
    this.connected = false;
    onDisconnect(device);
  }
  
  protected abstract void onReceive(BluetoothDevice device, byte[] message);
  
  protected abstract void onDisconnect(BluetoothDevice device);
  
  public boolean send(byte[] message, boolean force)
  {
    boolean result = false;
    
    if ((this.connected) && (force || this.lastMessageSent))
    {
      this.lastMessageSent = false;
      
      try
      {
        this.outputStream.write(message);
        this.outputStream.flush();
        
        result = true;
      }
      catch (Exception e)
      {
      }
      
      this.lastMessageSent = true;
    }
    
    return result;
  }
  
  public void close()
  {
    try
    {
      this.socket.close();
    }
    catch (Exception e)
    {
    }
  }
}




Java Source Code List

com.mauriciotogneri.bluetooth.beacons.BeaconFilter.java
com.mauriciotogneri.bluetooth.beacons.BeaconListener.java
com.mauriciotogneri.bluetooth.beacons.BeaconManager.java
com.mauriciotogneri.bluetooth.beacons.BeaconService.java
com.mauriciotogneri.bluetooth.beacons.Beacon.java
com.mauriciotogneri.bluetooth.beacons.UnsupportedBluetoothLeException.java
com.mauriciotogneri.bluetooth.connection.client.ClientConnection.java
com.mauriciotogneri.bluetooth.connection.client.ClientEvent.java
com.mauriciotogneri.bluetooth.connection.client.ClientLink.java
com.mauriciotogneri.bluetooth.connection.client.ClientThread.java
com.mauriciotogneri.bluetooth.connection.kernel.ConnectionThread.java
com.mauriciotogneri.bluetooth.connection.scan.DeviceScanner.java
com.mauriciotogneri.bluetooth.connection.scan.ScannerManager.java
com.mauriciotogneri.bluetooth.connection.server.ServerConnection.java
com.mauriciotogneri.bluetooth.connection.server.ServerEvent.java
com.mauriciotogneri.bluetooth.connection.server.ServerLink.java
com.mauriciotogneri.bluetooth.connection.server.ServerThread.java