import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;*/
/*******************************************************************************
* Copyright 2013 pyros2097
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
public static BlueTooth bluetooth;
//if(Gdx.app.getType() == ApplicationType.Desktop)
// bluetooth = new IBlueTooth();
// bluetooth = new BlueToothAndroid();
/*private static class BlueToothAndroid implements IBlueTooth {
public LinkedList<BluetoothDevice> devices;
private BluetoothDevice connectedDevice = null;
private int deviceIndex = 0;
private Activity currentActivity;
private final Handler mHandler;
private AcceptThread mAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
public static final int STATE_NONE = 0;
public static final int STATE_LISTEN = 1;
public static final int STATE_CONNECTING = 2;
public static final int STATE_CONNECTED = 3;
public boolean isConnected = false;
public boolean canConnect = true;
public boolean messageTaken = true;
public BluetoothAdapter bta = null;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
public boolean isConnected() {
public String getMessage() {
message = message.substring(0, 2);
public String getTest() {
for (int i = 0; i < devices.size(); i++) {
if (devices.get(i).getName().contains("HTC")
|| devices.get(i).getName().contains("GT")) {
connectedDevice = devices.get(i);
return devices.get(i).getName();
} catch (Exception exc) {
public String getDevice() {
connectedDevice = devices.get(deviceIndex);
return connectedDevice.getName();
} catch (Exception exc) {
public boolean isLast() {
if (connectedDevice == null)
if (connectedDevice.equals(devices.getLast())) {
} catch (Exception exc) {
public boolean isFirst() {
if (connectedDevice == null)
if (connectedDevice.equals(devices.getFirst())) {
} catch (Exception exc) {
public void switchToNextDevice() {
connectedDevice = devices.get(++deviceIndex);
public void switchToPrevDevice() {
connectedDevice = devices.get(--deviceIndex);
devices = new LinkedList<BluetoothDevice>();
public BluetoothManager(Activity activity, Handler handler) {
currentActivity = activity;
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
currentActivity.registerReceiver(mReceiver, filter);
private synchronized void setState(int state) {
mHandler.obtainMessage(MainActivity.MESSAGE_STATE_CHANGE, state, -1)
public synchronized int getState() {
public synchronized void start() {
if (mConnectThread != null) {
if (mConnectedThread != null) {
mConnectedThread.cancel();
if (mAcceptThread == null) {
mAcceptThread = new AcceptThread();
public synchronized void connect(BluetoothDevice device) {
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectThread = new ConnectThread(device);
setState(STATE_CONNECTING);
public synchronized void connected(BluetoothSocket socket,
BluetoothDevice device) {
if (mConnectThread != null) {
if (mConnectedThread != null) {
mConnectedThread.cancel();
if (mAcceptThread != null) {
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(MainActivity.DEVICE_NAME, device.getName());
mHandler.sendMessage(msg);
setState(STATE_CONNECTED);
public synchronized void stop() {
if (mConnectThread != null) {
if (mConnectedThread != null) {
mConnectedThread.cancel();
if (mAcceptThread != null) {
public void write(byte[] out) {
if (mState != STATE_CONNECTED)
private void connectionFailed() {
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(MainActivity.TOAST, "Unable to connect device");
mHandler.sendMessage(msg);
private void connectionLost() {
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(MainActivity.TOAST, "Device connection was lost");
mHandler.sendMessage(msg);
public void enableDiscoveribility() {
Intent discoverableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
currentActivity.startActivity(discoverableIntent);
public BluetoothAdapter getAdapter() {
bta = BluetoothAdapter.getDefaultAdapter();
public void enableBluetooth() {
if (!getAdapter().isEnabled()) {
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
currentActivity.startActivityForResult(enableIntent, 2);
public boolean isDiscovering() {
return getAdapter().isDiscovering();
public void discoverDevices() {
if (getAdapter().isDiscovering()) {
getAdapter().cancelDiscovery();
getAdapter().startDiscovery();
public void stopDiscovering() {
getAdapter().cancelDiscovery();
public boolean startServer() {
if (getAdapter().isEnabled()) {
public boolean canConnect() {
public void connectToServer() {
BluetoothDevice device = getAdapter().getRemoteDevice(
connectedDevice.getAddress());
} catch (Exception exc) {
public void sendMessage(String message) {
if (message.length() > 0) {
if (message.length() == 1) {
byte[] send = message.getBytes();
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
BluetoothServerSocket tmp = null;
.listenUsingRfcommWithServiceRecord(
UUID.fromString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"));
} catch (IOException e) {
BluetoothSocket socket = null;
while (mState != STATE_CONNECTED) {
socket = mmServerSocket.accept();
} catch (IOException e) {
synchronized (BluetoothManager.this) {
connected(socket, socket.getRemoteDevice());
} catch (IOException e) {
} catch (IOException e) {
} catch (IOException e) {
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
tmp = device.createRfcommSocketToServiceRecord(UUID
.fromString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"));
} catch (IOException e) {
getAdapter().cancelDiscovery();
} catch (IOException connectException) {
} catch (IOException closeException) {
synchronized (BluetoothManager.this) {
connected(mmSocket, mmDevice);
} catch (IOException e) {
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
byte[] buffer = new byte[1024];
bytes = mmInStream.read(buffer);
setMessage(new String(buffer, "UTF-8"));
mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1,
} catch (IOException e) {
public void write(byte[] bytes) {
mmOutStream.write(bytes);
} catch (IOException e) {
} catch (IOException e) {
private void setMessage(String message) {
public static interface BlueTooth {
public void enableWifi();
public void enableDiscoveribility();
public void discoverDevices();
public void stopDiscovering();
public boolean startServer();
public void connectToServer();
public void sendMessage(String message);
public String getMessage();
public boolean isConnected();
public boolean canConnect();
public void switchToNextDevice();
public void switchToPrevDevice();
public String getDevice();
public boolean isFirst();
public boolean isDiscovering();