gdx-studio
git clone https://git.pyrossh.dev/gdx-studio
An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop
src/scene2d/Net.java
| a9b2a64 | 1 | package scene2d; |
| a62d533 | 2 | /* |
| a62d533 | 3 | import java.io.IOException; |
| a62d533 | 4 | import java.io.InputStream; |
| a62d533 | 5 | import java.io.OutputStream; |
| a62d533 | 6 | import java.util.LinkedList; |
| a62d533 | 7 | import java.util.UUID; |
| a62d533 | 8 | import android.app.Activity; |
| a62d533 | 9 | import android.bluetooth.BluetoothAdapter; |
| a62d533 | 10 | import android.bluetooth.BluetoothDevice; |
| a62d533 | 11 | import android.bluetooth.BluetoothServerSocket; |
| a62d533 | 12 | import android.bluetooth.BluetoothSocket; |
| a62d533 | 13 | import android.content.BroadcastReceiver; |
| a62d533 | 14 | import android.content.Context; |
| a62d533 | 15 | import android.content.Intent; |
| a62d533 | 16 | import android.content.IntentFilter; |
| a62d533 | 17 | import android.os.Bundle; |
| a62d533 | 18 | import android.os.Handler; |
| a62d533 | 19 | import android.os.Message; |
| a62d533 | 20 | |
| a62d533 | 21 | import com.badlogic.gdx.Application.ApplicationType; |
| a62d533 | 22 | import com.badlogic.gdx.Gdx;*/ |
| a62d533 | 23 | |
| a62d533 | 24 | |
| a62d533 | 25 | /******************************************************************************* |
| a62d533 | 26 | * Copyright 2013 pyros2097 |
| a62d533 | 27 | * |
| a62d533 | 28 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| a62d533 | 29 | * you may not use this file except in compliance with the License. |
| a62d533 | 30 | * You may obtain a copy of the License at |
| a62d533 | 31 | * |
| a62d533 | 32 | * http://www.apache.org/licenses/LICENSE-2.0 |
| a62d533 | 33 | * |
| a62d533 | 34 | * Unless required by applicable law or agreed to in writing, software |
| a62d533 | 35 | * distributed under the License is distributed on an "AS IS" BASIS, |
| a62d533 | 36 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| a62d533 | 37 | * See the License for the specific language governing permissions and |
| a62d533 | 38 | * limitations under the License. |
| a62d533 | 39 | ******************************************************************************/ |
| a62d533 | 40 | |
| a62d533 | 41 | |
| a62d533 | 42 | public final class Net {
|
| a62d533 | 43 | public static BlueTooth bluetooth; |
| a62d533 | 44 | |
| a62d533 | 45 | private Net(){
|
| a62d533 | 46 | //if(Gdx.app.getType() == ApplicationType.Desktop) |
| a62d533 | 47 | // bluetooth = new IBlueTooth(); |
| a62d533 | 48 | //else |
| a62d533 | 49 | // bluetooth = new BlueToothAndroid(); |
| a62d533 | 50 | } |
| a62d533 | 51 | |
| a62d533 | 52 | /*private static class BlueToothAndroid implements IBlueTooth {
|
| a62d533 | 53 | public LinkedList<BluetoothDevice> devices; |
| a62d533 | 54 | private BluetoothDevice connectedDevice = null; |
| a62d533 | 55 | private int deviceIndex = 0; |
| a62d533 | 56 | private Activity currentActivity; |
| a62d533 | 57 | private final Handler mHandler; |
| a62d533 | 58 | private int mState; |
| a62d533 | 59 | private AcceptThread mAcceptThread; |
| a62d533 | 60 | private ConnectThread mConnectThread; |
| a62d533 | 61 | private ConnectedThread mConnectedThread; |
| a62d533 | 62 | |
| a62d533 | 63 | public static final int STATE_NONE = 0; |
| a62d533 | 64 | public static final int STATE_LISTEN = 1; |
| a62d533 | 65 | public static final int STATE_CONNECTING = 2; |
| a62d533 | 66 | public static final int STATE_CONNECTED = 3; |
| a62d533 | 67 | |
| a62d533 | 68 | public boolean isConnected = false; |
| a62d533 | 69 | public boolean canConnect = true; |
| a62d533 | 70 | |
| a62d533 | 71 | public String message; |
| a62d533 | 72 | public boolean messageTaken = true; |
| a62d533 | 73 | |
| a62d533 | 74 | public BluetoothAdapter bta = null; |
| a62d533 | 75 | |
| a62d533 | 76 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
| a62d533 | 77 | @Override |
| a62d533 | 78 | public void onReceive(Context context, Intent intent) {
|
| a62d533 | 79 | String action = intent.getAction(); |
| a62d533 | 80 | |
| a62d533 | 81 | if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
| a62d533 | 82 | BluetoothDevice device = intent |
| a62d533 | 83 | .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| a62d533 | 84 | devices.add(device); |
| a62d533 | 85 | } |
| a62d533 | 86 | } |
| a62d533 | 87 | }; |
| a62d533 | 88 | |
| a62d533 | 89 | public boolean isConnected() {
|
| a62d533 | 90 | return isConnected; |
| a62d533 | 91 | } |
| a62d533 | 92 | |
| a62d533 | 93 | public String getMessage() {
|
| a62d533 | 94 | if (!messageTaken) {
|
| a62d533 | 95 | messageTaken = true; |
| a62d533 | 96 | if (message != null) |
| a62d533 | 97 | message = message.substring(0, 2); |
| a62d533 | 98 | return message; |
| a62d533 | 99 | } |
| a62d533 | 100 | |
| a62d533 | 101 | return null; |
| a62d533 | 102 | } |
| a62d533 | 103 | |
| a62d533 | 104 | public String getTest() {
|
| a62d533 | 105 | try {
|
| a62d533 | 106 | for (int i = 0; i < devices.size(); i++) {
|
| a62d533 | 107 | if (devices.get(i).getName().contains("HTC")
|
| a62d533 | 108 | || devices.get(i).getName().contains("GT")) {
|
| a62d533 | 109 | connectedDevice = devices.get(i); |
| a62d533 | 110 | return devices.get(i).getName(); |
| a62d533 | 111 | } |
| a62d533 | 112 | } |
| a62d533 | 113 | } catch (Exception exc) {
|
| a62d533 | 114 | |
| a62d533 | 115 | } |
| a62d533 | 116 | return null; |
| a62d533 | 117 | } |
| a62d533 | 118 | |
| a62d533 | 119 | public String getDevice() {
|
| a62d533 | 120 | try {
|
| a62d533 | 121 | connectedDevice = devices.get(deviceIndex); |
| a62d533 | 122 | return connectedDevice.getName(); |
| a62d533 | 123 | } catch (Exception exc) {
|
| a62d533 | 124 | |
| a62d533 | 125 | } |
| a62d533 | 126 | |
| a62d533 | 127 | return null; |
| a62d533 | 128 | } |
| a62d533 | 129 | |
| a62d533 | 130 | public boolean isLast() {
|
| a62d533 | 131 | try {
|
| a62d533 | 132 | if (connectedDevice == null) |
| a62d533 | 133 | return true; |
| a62d533 | 134 | if (devices.isEmpty()) |
| a62d533 | 135 | return true; |
| a62d533 | 136 | if (connectedDevice.equals(devices.getLast())) {
|
| a62d533 | 137 | return true; |
| a62d533 | 138 | } |
| a62d533 | 139 | } catch (Exception exc) {
|
| a62d533 | 140 | |
| a62d533 | 141 | } |
| a62d533 | 142 | |
| a62d533 | 143 | return false; |
| a62d533 | 144 | } |
| a62d533 | 145 | |
| a62d533 | 146 | public boolean isFirst() {
|
| a62d533 | 147 | try {
|
| a62d533 | 148 | if (connectedDevice == null) |
| a62d533 | 149 | return true; |
| a62d533 | 150 | if (devices.isEmpty()) |
| a62d533 | 151 | return true; |
| a62d533 | 152 | if (connectedDevice.equals(devices.getFirst())) {
|
| a62d533 | 153 | return true; |
| a62d533 | 154 | } |
| a62d533 | 155 | } catch (Exception exc) {
|
| a62d533 | 156 | |
| a62d533 | 157 | } |
| a62d533 | 158 | |
| a62d533 | 159 | return false; |
| a62d533 | 160 | } |
| a62d533 | 161 | |
| a62d533 | 162 | public void switchToNextDevice() {
|
| a62d533 | 163 | if (!isLast()) {
|
| a62d533 | 164 | connectedDevice = devices.get(++deviceIndex); |
| a62d533 | 165 | } |
| a62d533 | 166 | } |
| a62d533 | 167 | |
| a62d533 | 168 | public void switchToPrevDevice() {
|
| a62d533 | 169 | if (!isFirst()) {
|
| a62d533 | 170 | connectedDevice = devices.get(--deviceIndex); |
| a62d533 | 171 | } |
| a62d533 | 172 | } |
| a62d533 | 173 | |
| a62d533 | 174 | private void init() {
|
| a62d533 | 175 | devices = new LinkedList<BluetoothDevice>(); |
| a62d533 | 176 | } |
| a62d533 | 177 | |
| a62d533 | 178 | public BluetoothManager(Activity activity, Handler handler) {
|
| a62d533 | 179 | init(); |
| a62d533 | 180 | |
| a62d533 | 181 | currentActivity = activity; |
| a62d533 | 182 | mState = STATE_NONE; |
| a62d533 | 183 | mHandler = handler; |
| a62d533 | 184 | |
| a62d533 | 185 | IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); |
| a62d533 | 186 | currentActivity.registerReceiver(mReceiver, filter); |
| a62d533 | 187 | } |
| a62d533 | 188 | |
| a62d533 | 189 | private synchronized void setState(int state) {
|
| a62d533 | 190 | mState = state; |
| a62d533 | 191 | |
| a62d533 | 192 | mHandler.obtainMessage(MainActivity.MESSAGE_STATE_CHANGE, state, -1) |
| a62d533 | 193 | .sendToTarget(); |
| a62d533 | 194 | } |
| a62d533 | 195 | |
| a62d533 | 196 | public synchronized int getState() {
|
| a62d533 | 197 | return mState; |
| a62d533 | 198 | } |
| a62d533 | 199 | |
| a62d533 | 200 | public synchronized void start() {
|
| a62d533 | 201 | if (mConnectThread != null) {
|
| a62d533 | 202 | mConnectThread.cancel(); |
| a62d533 | 203 | mConnectThread = null; |
| a62d533 | 204 | } |
| a62d533 | 205 | |
| a62d533 | 206 | if (mConnectedThread != null) {
|
| a62d533 | 207 | mConnectedThread.cancel(); |
| a62d533 | 208 | mConnectedThread = null; |
| a62d533 | 209 | } |
| a62d533 | 210 | |
| a62d533 | 211 | if (mAcceptThread == null) {
|
| a62d533 | 212 | mAcceptThread = new AcceptThread(); |
| a62d533 | 213 | mAcceptThread.start(); |
| a62d533 | 214 | } |
| a62d533 | 215 | setState(STATE_LISTEN); |
| a62d533 | 216 | } |
| a62d533 | 217 | |
| a62d533 | 218 | public synchronized void connect(BluetoothDevice device) {
|
| a62d533 | 219 | if (mState == STATE_CONNECTING) {
|
| a62d533 | 220 | if (mConnectThread != null) {
|
| a62d533 | 221 | mConnectThread.cancel(); |
| a62d533 | 222 | mConnectThread = null; |
| a62d533 | 223 | } |
| a62d533 | 224 | } |
| a62d533 | 225 | |
| a62d533 | 226 | if (mConnectedThread != null) {
|
| a62d533 | 227 | mConnectedThread.cancel(); |
| a62d533 | 228 | mConnectedThread = null; |
| a62d533 | 229 | } |
| a62d533 | 230 | |
| a62d533 | 231 | mConnectThread = new ConnectThread(device); |
| a62d533 | 232 | mConnectThread.start(); |
| a62d533 | 233 | setState(STATE_CONNECTING); |
| a62d533 | 234 | } |
| a62d533 | 235 | |
| a62d533 | 236 | public synchronized void connected(BluetoothSocket socket, |
| a62d533 | 237 | BluetoothDevice device) {
|
| a62d533 | 238 | if (mConnectThread != null) {
|
| a62d533 | 239 | mConnectThread.cancel(); |
| a62d533 | 240 | mConnectThread = null; |
| a62d533 | 241 | } |
| a62d533 | 242 | |
| a62d533 | 243 | if (mConnectedThread != null) {
|
| a62d533 | 244 | mConnectedThread.cancel(); |
| a62d533 | 245 | mConnectedThread = null; |
| a62d533 | 246 | } |
| a62d533 | 247 | |
| a62d533 | 248 | if (mAcceptThread != null) {
|
| a62d533 | 249 | mAcceptThread.cancel(); |
| a62d533 | 250 | mAcceptThread = null; |
| a62d533 | 251 | } |
| a62d533 | 252 | |
| a62d533 | 253 | mConnectedThread = new ConnectedThread(socket); |
| a62d533 | 254 | mConnectedThread.start(); |
| a62d533 | 255 | |
| a62d533 | 256 | Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_DEVICE_NAME); |
| a62d533 | 257 | Bundle bundle = new Bundle(); |
| a62d533 | 258 | bundle.putString(MainActivity.DEVICE_NAME, device.getName()); |
| a62d533 | 259 | msg.setData(bundle); |
| a62d533 | 260 | mHandler.sendMessage(msg); |
| a62d533 | 261 | |
| a62d533 | 262 | setState(STATE_CONNECTED); |
| a62d533 | 263 | } |
| a62d533 | 264 | |
| a62d533 | 265 | public synchronized void stop() {
|
| a62d533 | 266 | if (mConnectThread != null) {
|
| a62d533 | 267 | mConnectThread.cancel(); |
| a62d533 | 268 | mConnectThread = null; |
| a62d533 | 269 | } |
| a62d533 | 270 | if (mConnectedThread != null) {
|
| a62d533 | 271 | mConnectedThread.cancel(); |
| a62d533 | 272 | mConnectedThread = null; |
| a62d533 | 273 | } |
| a62d533 | 274 | if (mAcceptThread != null) {
|
| a62d533 | 275 | mAcceptThread.cancel(); |
| a62d533 | 276 | mAcceptThread = null; |
| a62d533 | 277 | } |
| a62d533 | 278 | setState(STATE_NONE); |
| a62d533 | 279 | isConnected = false; |
| a62d533 | 280 | } |
| a62d533 | 281 | |
| a62d533 | 282 | public void write(byte[] out) {
|
| a62d533 | 283 | ConnectedThread r; |
| a62d533 | 284 | synchronized (this) {
|
| a62d533 | 285 | if (mState != STATE_CONNECTED) |
| a62d533 | 286 | return; |
| a62d533 | 287 | r = mConnectedThread; |
| a62d533 | 288 | } |
| a62d533 | 289 | r.write(out); |
| a62d533 | 290 | } |
| a62d533 | 291 | |
| a62d533 | 292 | private void connectionFailed() {
|
| a62d533 | 293 | setState(STATE_LISTEN); |
| a62d533 | 294 | |
| a62d533 | 295 | canConnect = false; |
| a62d533 | 296 | Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST); |
| a62d533 | 297 | Bundle bundle = new Bundle(); |
| a62d533 | 298 | bundle.putString(MainActivity.TOAST, "Unable to connect device"); |
| a62d533 | 299 | msg.setData(bundle); |
| a62d533 | 300 | mHandler.sendMessage(msg); |
| a62d533 | 301 | } |
| a62d533 | 302 | |
| a62d533 | 303 | private void connectionLost() {
|
| a62d533 | 304 | setState(STATE_LISTEN); |
| a62d533 | 305 | |
| a62d533 | 306 | Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST); |
| a62d533 | 307 | Bundle bundle = new Bundle(); |
| a62d533 | 308 | bundle.putString(MainActivity.TOAST, "Device connection was lost"); |
| a62d533 | 309 | msg.setData(bundle); |
| a62d533 | 310 | mHandler.sendMessage(msg); |
| a62d533 | 311 | } |
| a62d533 | 312 | |
| a62d533 | 313 | public void enableDiscoveribility() {
|
| a62d533 | 314 | Intent discoverableIntent = new Intent( |
| a62d533 | 315 | BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); |
| a62d533 | 316 | discoverableIntent.putExtra( |
| a62d533 | 317 | BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600); |
| a62d533 | 318 | currentActivity.startActivity(discoverableIntent); |
| a62d533 | 319 | } |
| a62d533 | 320 | |
| a62d533 | 321 | public BluetoothAdapter getAdapter() {
|
| a62d533 | 322 | if (bta == null) {
|
| a62d533 | 323 | bta = BluetoothAdapter.getDefaultAdapter(); |
| a62d533 | 324 | } |
| a62d533 | 325 | |
| a62d533 | 326 | return bta; |
| a62d533 | 327 | } |
| a62d533 | 328 | |
| a62d533 | 329 | public void enableBluetooth() {
|
| a62d533 | 330 | if (!getAdapter().isEnabled()) {
|
| a62d533 | 331 | Intent enableIntent = new Intent( |
| a62d533 | 332 | BluetoothAdapter.ACTION_REQUEST_ENABLE); |
| a62d533 | 333 | currentActivity.startActivityForResult(enableIntent, 2); |
| a62d533 | 334 | } |
| a62d533 | 335 | } |
| a62d533 | 336 | |
| a62d533 | 337 | public boolean isDiscovering() {
|
| a62d533 | 338 | return getAdapter().isDiscovering(); |
| a62d533 | 339 | } |
| a62d533 | 340 | |
| a62d533 | 341 | public void discoverDevices() {
|
| a62d533 | 342 | if (getAdapter().isDiscovering()) {
|
| a62d533 | 343 | getAdapter().cancelDiscovery(); |
| a62d533 | 344 | } |
| a62d533 | 345 | |
| a62d533 | 346 | getAdapter().startDiscovery(); |
| a62d533 | 347 | } |
| a62d533 | 348 | |
| a62d533 | 349 | public void stopDiscovering() {
|
| a62d533 | 350 | getAdapter().cancelDiscovery(); |
| a62d533 | 351 | } |
| a62d533 | 352 | |
| a62d533 | 353 | public boolean startServer() {
|
| a62d533 | 354 | if (getAdapter().isEnabled()) {
|
| a62d533 | 355 | this.start(); |
| a62d533 | 356 | return true; |
| a62d533 | 357 | } |
| a62d533 | 358 | |
| a62d533 | 359 | return false; |
| a62d533 | 360 | } |
| a62d533 | 361 | |
| a62d533 | 362 | public boolean canConnect() {
|
| a62d533 | 363 | return this.canConnect; |
| a62d533 | 364 | } |
| a62d533 | 365 | |
| a62d533 | 366 | public void connectToServer() {
|
| a62d533 | 367 | canConnect = true; |
| a62d533 | 368 | try {
|
| a62d533 | 369 | BluetoothDevice device = getAdapter().getRemoteDevice( |
| a62d533 | 370 | connectedDevice.getAddress()); |
| a62d533 | 371 | this.connect(device); |
| a62d533 | 372 | } catch (Exception exc) {
|
| a62d533 | 373 | |
| a62d533 | 374 | } |
| a62d533 | 375 | } |
| a62d533 | 376 | |
| a62d533 | 377 | public void sendMessage(String message) {
|
| a62d533 | 378 | if (message.length() > 0) {
|
| a62d533 | 379 | if (message.length() == 1) {
|
| a62d533 | 380 | message = "0" + message; |
| a62d533 | 381 | } |
| a62d533 | 382 | byte[] send = message.getBytes(); |
| a62d533 | 383 | this.write(send); |
| a62d533 | 384 | } |
| a62d533 | 385 | } |
| a62d533 | 386 | |
| a62d533 | 387 | private class AcceptThread extends Thread {
|
| a62d533 | 388 | private final BluetoothServerSocket mmServerSocket; |
| a62d533 | 389 | |
| a62d533 | 390 | public AcceptThread() {
|
| a62d533 | 391 | BluetoothServerSocket tmp = null; |
| a62d533 | 392 | try {
|
| a62d533 | 393 | tmp = getAdapter() |
| a62d533 | 394 | .listenUsingRfcommWithServiceRecord( |
| a62d533 | 395 | "ServiceName", |
| a62d533 | 396 | UUID.fromString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"));
|
| a62d533 | 397 | } catch (IOException e) {
|
| a62d533 | 398 | } |
| a62d533 | 399 | mmServerSocket = tmp; |
| a62d533 | 400 | } |
| a62d533 | 401 | |
| a62d533 | 402 | public void run() {
|
| a62d533 | 403 | BluetoothSocket socket = null; |
| a62d533 | 404 | while (mState != STATE_CONNECTED) {
|
| a62d533 | 405 | try {
|
| a62d533 | 406 | socket = mmServerSocket.accept(); |
| a62d533 | 407 | } catch (IOException e) {
|
| a62d533 | 408 | break; |
| a62d533 | 409 | } |
| a62d533 | 410 | if (socket != null) {
|
| a62d533 | 411 | synchronized (BluetoothManager.this) {
|
| a62d533 | 412 | switch (mState) {
|
| a62d533 | 413 | case STATE_LISTEN: |
| a62d533 | 414 | case STATE_CONNECTING: |
| a62d533 | 415 | connected(socket, socket.getRemoteDevice()); |
| a62d533 | 416 | break; |
| a62d533 | 417 | case STATE_NONE: |
| a62d533 | 418 | case STATE_CONNECTED: |
| a62d533 | 419 | try {
|
| a62d533 | 420 | socket.close(); |
| a62d533 | 421 | } catch (IOException e) {
|
| a62d533 | 422 | } |
| a62d533 | 423 | break; |
| a62d533 | 424 | } |
| a62d533 | 425 | } |
| a62d533 | 426 | try {
|
| a62d533 | 427 | mmServerSocket.close(); |
| a62d533 | 428 | } catch (IOException e) {
|
| a62d533 | 429 | e.printStackTrace(); |
| a62d533 | 430 | } |
| a62d533 | 431 | break; |
| a62d533 | 432 | } |
| a62d533 | 433 | } |
| a62d533 | 434 | } |
| a62d533 | 435 | |
| a62d533 | 436 | public void cancel() {
|
| a62d533 | 437 | try {
|
| a62d533 | 438 | mmServerSocket.close(); |
| a62d533 | 439 | } catch (IOException e) {
|
| a62d533 | 440 | } |
| a62d533 | 441 | } |
| a62d533 | 442 | } |
| a62d533 | 443 | |
| a62d533 | 444 | private class ConnectThread extends Thread {
|
| a62d533 | 445 | private final BluetoothSocket mmSocket; |
| a62d533 | 446 | private final BluetoothDevice mmDevice; |
| a62d533 | 447 | |
| a62d533 | 448 | public ConnectThread(BluetoothDevice device) {
|
| a62d533 | 449 | BluetoothSocket tmp = null; |
| a62d533 | 450 | mmDevice = device; |
| a62d533 | 451 | |
| a62d533 | 452 | try {
|
| a62d533 | 453 | tmp = device.createRfcommSocketToServiceRecord(UUID |
| a62d533 | 454 | .fromString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"));
|
| a62d533 | 455 | } catch (IOException e) {
|
| a62d533 | 456 | } |
| a62d533 | 457 | mmSocket = tmp; |
| a62d533 | 458 | } |
| a62d533 | 459 | |
| a62d533 | 460 | public void run() {
|
| a62d533 | 461 | getAdapter().cancelDiscovery(); |
| a62d533 | 462 | |
| a62d533 | 463 | try {
|
| a62d533 | 464 | mmSocket.connect(); |
| a62d533 | 465 | } catch (IOException connectException) {
|
| a62d533 | 466 | connectionFailed(); |
| a62d533 | 467 | try {
|
| a62d533 | 468 | mmSocket.close(); |
| a62d533 | 469 | } catch (IOException closeException) {
|
| a62d533 | 470 | } |
| a62d533 | 471 | |
| a62d533 | 472 | return; |
| a62d533 | 473 | } |
| a62d533 | 474 | |
| a62d533 | 475 | synchronized (BluetoothManager.this) {
|
| a62d533 | 476 | mConnectThread = null; |
| a62d533 | 477 | } |
| a62d533 | 478 | |
| a62d533 | 479 | connected(mmSocket, mmDevice); |
| a62d533 | 480 | } |
| a62d533 | 481 | |
| a62d533 | 482 | public void cancel() {
|
| a62d533 | 483 | try {
|
| a62d533 | 484 | mmSocket.close(); |
| a62d533 | 485 | } catch (IOException e) {
|
| a62d533 | 486 | } |
| a62d533 | 487 | } |
| a62d533 | 488 | } |
| a62d533 | 489 | |
| a62d533 | 490 | private class ConnectedThread extends Thread {
|
| a62d533 | 491 | private final BluetoothSocket mmSocket; |
| a62d533 | 492 | private final InputStream mmInStream; |
| a62d533 | 493 | private final OutputStream mmOutStream; |
| a62d533 | 494 | |
| a62d533 | 495 | public ConnectedThread(BluetoothSocket socket) {
|
| a62d533 | 496 | mmSocket = socket; |
| a62d533 | 497 | InputStream tmpIn = null; |
| a62d533 | 498 | OutputStream tmpOut = null; |
| a62d533 | 499 | isConnected = true; |
| a62d533 | 500 | |
| a62d533 | 501 | try {
|
| a62d533 | 502 | tmpIn = socket.getInputStream(); |
| a62d533 | 503 | tmpOut = socket.getOutputStream(); |
| a62d533 | 504 | } catch (IOException e) {
|
| a62d533 | 505 | } |
| a62d533 | 506 | |
| a62d533 | 507 | mmInStream = tmpIn; |
| a62d533 | 508 | mmOutStream = tmpOut; |
| a62d533 | 509 | } |
| a62d533 | 510 | |
| a62d533 | 511 | public void run() {
|
| a62d533 | 512 | byte[] buffer = new byte[1024]; |
| a62d533 | 513 | int bytes; |
| a62d533 | 514 | |
| a62d533 | 515 | while (true) {
|
| a62d533 | 516 | try {
|
| a62d533 | 517 | bytes = mmInStream.read(buffer); |
| a62d533 | 518 | setMessage(new String(buffer, "UTF-8")); |
| a62d533 | 519 | mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, |
| a62d533 | 520 | buffer).sendToTarget(); |
| a62d533 | 521 | } catch (IOException e) {
|
| a62d533 | 522 | connectionLost(); |
| a62d533 | 523 | break; |
| a62d533 | 524 | } |
| a62d533 | 525 | } |
| a62d533 | 526 | } |
| a62d533 | 527 | |
| a62d533 | 528 | public void write(byte[] bytes) {
|
| a62d533 | 529 | try {
|
| a62d533 | 530 | mmOutStream.write(bytes); |
| a62d533 | 531 | } catch (IOException e) {
|
| a62d533 | 532 | } |
| a62d533 | 533 | } |
| a62d533 | 534 | |
| a62d533 | 535 | public void cancel() {
|
| a62d533 | 536 | try {
|
| a62d533 | 537 | mmSocket.close(); |
| a62d533 | 538 | isConnected = false; |
| a62d533 | 539 | } catch (IOException e) {
|
| a62d533 | 540 | } |
| a62d533 | 541 | } |
| a62d533 | 542 | } |
| a62d533 | 543 | |
| a62d533 | 544 | private void setMessage(String message) {
|
| a62d533 | 545 | if (messageTaken) {
|
| a62d533 | 546 | this.message = message; |
| a62d533 | 547 | messageTaken = false; |
| a62d533 | 548 | } |
| a62d533 | 549 | } |
| a62d533 | 550 | }*/ |
| a62d533 | 551 | |
| a62d533 | 552 | public static interface BlueTooth {
|
| a62d533 | 553 | public void enableWifi(); |
| a62d533 | 554 | public void enableDiscoveribility(); |
| a62d533 | 555 | public void discoverDevices(); |
| a62d533 | 556 | public void stopDiscovering(); |
| a62d533 | 557 | public boolean startServer(); |
| a62d533 | 558 | public void connectToServer(); |
| a62d533 | 559 | public String getTest(); |
| a62d533 | 560 | public void sendMessage(String message); |
| a62d533 | 561 | public String getMessage(); |
| a62d533 | 562 | public boolean isConnected(); |
| a62d533 | 563 | public boolean canConnect(); |
| a62d533 | 564 | public void switchToNextDevice(); |
| a62d533 | 565 | public void switchToPrevDevice(); |
| a62d533 | 566 | public String getDevice(); |
| a62d533 | 567 | public void stop(); |
| a62d533 | 568 | public boolean isFirst(); |
| a62d533 | 569 | public boolean isLast(); |
| a62d533 | 570 | public boolean isDiscovering(); |
| a62d533 | 571 | } |
| a62d533 | 572 | |
| a62d533 | 573 | } |