Android Open Source - bluetooth Client Connection






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.client;
/*w w  w.jav  a  2  s  .co  m*/
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

public class ClientConnection implements ClientEvent
{
  private ClientEvent clientEvent;
  private ClientThread clientThread;
  private ClientLink clientLink;
  
  public ClientConnection(ClientEvent clientEvent)
  {
    this.clientEvent = clientEvent;
  }
  
  public void setListener(ClientEvent clientEvent)
  {
    this.clientEvent = clientEvent;
  }
  
  public void connect(BluetoothDevice device, String uuid)
  {
    if (this.clientThread == null)
    {
      this.clientThread = new ClientThread(this, device, uuid);
      this.clientThread.start();
    }
  }
  
  public String getDeviceName()
  {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    
    return (bluetoothAdapter == null) ? "" : bluetoothAdapter.getName();
  }
  
  public String getDeviceAddress()
  {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    
    return (bluetoothAdapter == null) ? "" : bluetoothAdapter.getAddress();
  }
  
  void connected(BluetoothSocket socket)
  {
    try
    {
      this.clientLink = new ClientLink(socket, this);
      this.clientLink.start();
      
      onConnect();
    }
    catch (Exception e)
    {
      onErrorConnecting();
    }
  }
  
  @Override
  public void onReceive(byte[] message)
  {
    this.clientEvent.onReceive(message);
  }
  
  @Override
  public void onConnect()
  {
    this.clientEvent.onConnect();
  }
  
  @Override
  public void onErrorConnecting()
  {
    this.clientEvent.onErrorConnecting();
  }
  
  @Override
  public void onDisconnect()
  {
    this.clientEvent.onDisconnect();
  }
  
  public boolean send(byte[] message, boolean force)
  {
    boolean result = false;
    
    if (this.clientLink != null)
    {
      result = this.clientLink.send(message, force);
    }
    
    return result;
  }
  
  public void close()
  {
    if (this.clientThread != null)
    {
      this.clientThread.close();
    }
    
    if (this.clientLink != null)
    {
      this.clientLink.close();
    }
  }
}




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