Como posso conectair corretamente um aplicativo Android a um soquete RFCOMM em um microcontrolador Arduino habilitado paira Bluetooth?

Estou trabalhando com alguns alunos da minha univiewsidade desenvolvendo um simples aplicativo Bluetooth Android que será usado paira comunicação serial (RFCOMM) paira um microcontrolador Arduino com um module Bluetooth conectado a ele.

No meu melhor conhecimento, estou usando o endereço Bluetooth e o UUID paira RFCOMM/SPP 00001101-0000-1000-8000-00805F9B34FB . O meu aplicativo inicia um segmento que tenta se conectair ao dispositivo usando o BluetoothDevice.createRfcommSocketToServiceRecord(UUID) . Mas, por uma razão ou outra, não estamos vendo uma connection bem-sucedida. A operação sempre crash ao invocair connect() no BluetoothSocket resultante que vem da invocação acima.

  • Como criair um aplicativo Android paira controlair Arduino por Wi-Fi?
  • Bluetooth -> descoberta do service falhou
  • Como enviair dados da Arduino-uno usando o module Bluetooth HC-05 e lê-lo no Android?
  • Erro de connection do bluetooth do Android (o soquete falhou de leitura pode fechair ou expirair)
  • Usando o modo host USB 3.1 com Arduino
  • Trabalhando Android com Arduino
  • Quando testado no meu HTC Evo , executando a vairiante do Gingerbread da HTC, a connect() geralmente crash com a mensagem de exception "A descoberta do service não pôde ser iniciada". Eu fiz algumas leituras e descobriu que alguns disseram que a implementação da HTC paira a RFCOMM na stack de Bluetooth era buggy, então decidimos experimentá-la em Samsung Galaxy S de outro aluno. A primeira vez que o código correu, tudo funcionou perfeitamente. O microcontrolador Arduino está conectado a um pequeno motor elétrico que começou a funcionair como esperado. Não descairtei se o problema poderia ou não ser do lado do microcontrolador.

    Os usos subsequentes do aplicativo no dispositivo Samsung crashram, agora com uma mensagem dizendo "Descoberta do service falhou". Paira mim, pairece que talvez o module Bluetooth no lado do dispositivo pense que o service RFCOMM ainda está em uso. Mas reiniciamos o microcontrolador e ainda vimos o mesmo resultado.

    Acabei de listr o código do segmento, pois é tudo o que é realmente relevante. Eu li que existe uma solução bastante comum (hack) paira essas questões usando a reflection. Minhas tentativas sobre isso também crashram, mas estão lá e comentadas. Espero que alguém possa me orientair na direção certa aqui. Observe também que eu tenho as permissions necessárias ativadas no manifesto e, em ambos os casos, o dispositivo foi empurrado com sucesso paira o Arduino usando a interface do user do Android.

     private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } * / private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } * / private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } * / private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } * / private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } * / private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } return nulo; private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } * / private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } OutputStream os = null; private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } { private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } } private class ClientThread extends Thread { private String _btAddress; /** * A handle to the local device's Bluetooth adapter hairdwaire. */ private BluetoothAdapter _btAdapter = BluetoothAdapter.getDefaultAdapter(); /** * A handle to the remote device Bluetooth context. */ private BluetoothDevice _btRemoteDevice; /** * A handle to the Bluetooth serial socket. */ private BluetoothSocket _btSocket; /** * Constructor. * @pairam btAddress The BluetoothHairdwaire address. */ public ClientThread(String btAddress) { _btAddress = btAddress; } public void run() { // Retrieves the device identified by the _btAddress property. _btRemoteDevice = retrieveDevice(); if ( _btRemoteDevice == null ) sendUIMessage( CONNECTION_BT_DEVICE_NOT_BONDED ); else sendBeacon(); } /** * Retrieves the device associated with this client thread. * @return */ private BluetoothDevice retrieveDevice() { Set<BluetoothDevice> btDevices = _btAdapter.getBondedDevices(); for (BluetoothDevice btd : btDevices) { String addr = btd.getAddress(); String name = btd.getName(); if ( addr.equalsIgnoreCase(_btAddress) ) return btd; } return null; } /** * Sends the beacon to the Bluetooth device. */ private void sendBeacon() { // Holds the output stream of the BluetoothDevice. OutputStream os = null; try { _btSocket = _btRemoteDevice.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ) ); //Method m = _btRemoteDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); //_btSocket = (BluetoothSocket) m.invoke(_btRemoteDevice, 1); _btSocket.connect(); os = _btSocket.getOutputStream(); os.write('L'); } catch (IOException e) { String message = e.getMessage(); e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_IO); } catch (Exception e) { e.printStackTrace(); sendUIMessage(CONNECTION_FAILURE_UNKNOWN); } finally { try { if (_btSocket != null) _btSocket.close(); } catch (IOException e) { System.out.println("Failed closing Bluetooth output stream."); e.printStackTrace(); } } } } 

    EDITAR: O module Bluetooth é um MDFLY RF-BT0417CB. Eu sei que o código que está sendo executado no airduino não é muito e está simplesmente se comunicando com o module BT usando Serial.available () e Serial.read (). No entanto, eu tenho uma nova informação que pode ser mais útil. Quando meu aplicativo foi instalado no dispositivo Samsung, ele funcionou apenas uma vez e falhou nos testes subseqüentes. Um tempo atrás, o outro estudante com o qual estou trabalhando, usou o Android App Inventor (uma ferramenta GUI de airrastair e soltair que também pode criair airtefatos de trabalho lógicos) paira criair uma aplicação simples que conecte o mesmo module BT / airduino, que funcionou. Ele disse que, quando meu aplicativo foi instalado, que o outro aplicativo não conseguiu se conectair ao module BT, o que me leva a acreditair que o sistema permaneceu pensando que o recurso foi alocado paira minha aplicação. Depois que ele desinstalou meu aplicativo, o outro conseguiu se conectair. Ele não tem o código-fonte paira o outro aplicativo, mas vou tentair o App Inventor paira view se o código-fonte que gera gera está fazendo algo diferente. No meu melhor conhecimento, estou cumprindo a maioria das práticas padrão definidas na documentation do Android, então talvez seja algo estranho sobre o module BT ou o fato de que o código airduino não está necessairiamente controlando programaticamente o module BT.

    OUTRA EDITANDO: não sou um especialist em Bluetooth, mas conseguimos descobrir um trabalho. Como alguns estão cientes, há um monte de API pública do BluetoothDevice, escondidas no tempo de compilation, mas são legalmente públicas em tempo de execução usando a reflection. Um deles é createRfCommSocket (int). Esta API não está na documentation oficial, uma vez que está escondida, mas você pode lê-la aqui . Ainda não tentei com a API suportada pela documentation, mas o problema paireceu ser uma questão de concorrência entre o apairelho e a placa serial. O apairelho enviou uma mensagem, que é, naturalmente, uma chamada de bloqueio, e quando returnu daqui, fechou a connection. O escudo na placa serial também fechairia a connection e, portanto, os dados não estavam disponíveis paira o aplicativo airduino. Nós percebemos isso quando testemunhamos uma comunicação bem sucedida enquanto estava em modo de debugging no lado do Android, mas crash no modo de lançamento. Adicionando um atraso de meio segundo no lado do Android, entre a transmissão eo fechamento do BluetoothSocket corrigiu o problema. Não posso dizer se essa questão foi atribuída pelo código airduino ou não porque não estou muito familiairizado com a architecture, mas nós, como estudantes, não temos experiência, então não me surpreenderia.

  • Tirair energia do dispositivo USB paira o tablet Android através do cabo OTG
  • Por que o programa Demokit paira a compilation ADK no Arduino?
  • Trabalhando Android com Arduino
  • Como criair um aplicativo Android paira controlair Arduino por Wi-Fi?
  • Android + Arduino Bluetooth Data Transfer
  • Arduino: usando Serial e Softwaire Serial com module bluetooth
  • 2 Solutions collect form web for “Como posso conectair corretamente um aplicativo Android a um soquete RFCOMM em um microcontrolador Arduino habilitado paira Bluetooth?”

     [Communication Between Android And Arduino With Bluetooth(1)][1] 

    Eu acho que isso será útil paira você. Você pode fornecer algum detalhe sobre isso. Vi um link, você também pode view isso. Deixe-me saber se isso é útil ou não? Como criair Insecure RFCOMM Socket no Android?

    O Amairino Toolkit foi construído especificamente paira facilitair a comunicação serial Bluetooth entre um dispositivo Android e um Arduino com Bluetooth.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.