Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>public class BlueTooth { public String tag = "Bluetooth"; public static final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); public BluetoothAdapter adapter; public BluetoothDevice device = null; public BluetoothSocket socket = null; //public static final char ESC = (char) 27; //public static final char CR = (char) 13; //public static final char LF = (char) 10; public BlueTooth(String MAC) { this(MAC,null); } @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1) public BlueTooth(String MAC, byte[] pin) { try { adapter = BluetoothAdapter.getDefaultAdapter(); // wait for enable this is not very correct if (!adapter.isEnabled()) { adapter.enable(); SystemClock.sleep(5000); if (!adapter.isEnabled()) throw new IOException("Bluetooth is off"); } if (!BluetoothAdapter.checkBluetoothAddress(MAC)) throw new Exception("Err MAC adress (" + MAC + ")"); device = adapter.getRemoteDevice(MAC); // this is only for testing purpouse i dont use this if (pin!=null) setPin(pin); // i have some trouble with my device on different OS if (android.os.Build.VERSION.SDK_INT &gt; android.os.Build.VERSION_CODES.HONEYCOMB) socket = device.createInsecureRfcommSocketToServiceRecord(uuid); else socket = device.createRfcommSocketToServiceRecord(uuid); } catch (Exception e) { throw new RuntimeException(e); } finally { } } public void setPin(byte[] pin) { try { Method setPin = device.getClass().getMethod("setPin", byte[].class); setPin.invoke(device, pin); Log.i("BlueTooth", "PIN is set: "+new String(pin)); } catch (Exception e) { e.printStackTrace(); } } @Override protected void finalize() throws Throwable { disconnect(); super.finalize(); } // Android REfs recommend to cancel discover public boolean connect() { try { while (adapter.isDiscovering()) { adapter.cancelDiscovery(); Thread.sleep(1000); } socket.connect(); return true; } catch(Exception e) { throw new RuntimeException(e.getMessage(),e); } } // After write is good(only for shure for next write) to clear buffer with flush public void write(byte[] in) throws Exception { socket.getOutputStream().write(in, 0, in.length); socket.getOutputStream().flush(); } // Wait for incomming data // if is something in the buffer start count but max 3sec // 3 sec is defined by my device vendor for reply // // after it we something on buffer. // you must realize the reading is on the fly // there for you must wait after read to next data sleep(1mxec) // until data are available // then return public byte[] read() throws Exception { int treshHold = 0; while (socket.getInputStream().available()==0 &amp;&amp; treshHold&lt;3000) { Thread.sleep(1); treshHold++; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.reset(); while (socket.getInputStream().available() &gt; 0) { baos.write(socket.getInputStream().read()); Thread.sleep(1); } return baos.toByteArray(); } // This is little low // after disconnect you must create // new socket // so after disconnect you are not able to use connect // withous createRFcommSocket public void disconnect() throws Exception { if (socket != null) socket.close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload