Android examples for android.bluetooth:Bluetooth
connect to a Bluetooth Device and Write list of string
import android.annotation.TargetApi; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Intent; import android.os.Build; import android.util.Log; import java.io.OutputStream; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.UUID; public class Main{ static final UUID UUID_RFCOMM_GENERIC = UUID .fromString("00001101-0000-1000-8000-00805F9B34FB"); @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1) public static boolean connectWriteBlocos(BluetoothDevice device, final List<String[]> nota) { BluetoothAdapter mBTAdapter = BluetoothAdapter.getDefaultAdapter(); Log.i("Bluetooth MAC: ", device.getAddress()); BluetoothDevice mBTDevice = mBTAdapter.getRemoteDevice(device .getAddress());/*from w ww . j av a2 s . co m*/ BluetoothSocket mBTSocket; try { mBTSocket = mBTDevice .createRfcommSocketToServiceRecord(UUID_RFCOMM_GENERIC); //mBTSocket = mBTDevice.createInsecureRfcommSocketToServiceRecord(UUID_RFCOMM_GENERIC); mBTSocket.connect(); OutputStream mBTOutputStream = mBTSocket.getOutputStream(); mBTOutputStream.write(nota.get(0)[0].getBytes()); for (int i = 0; i < nota.get(1).length; i++) { mBTOutputStream.write(nota.get(1)[i].getBytes()); } mBTOutputStream.write(nota.get(2)[0].getBytes()); Thread.sleep(12000 + (nota.get(1).length * 8000)); mBTOutputStream.close(); return true; } catch (Exception e1) { e1.printStackTrace(); return false; } } @SuppressWarnings({ "unchecked", "rawtypes", "unused" }) public static Boolean connect(BluetoothDevice bdDevice) { Boolean bool = false; try { Class cl = Class.forName("android.bluetooth.BluetoothDevice"); Class[] par = {}; Method method = cl.getMethod("createBond", par); Object[] args = {}; bool = (Boolean) method.invoke(bdDevice);//, args);// this invoke creates the detected devices paired. } catch (Exception e) { e.printStackTrace(); } return bool.booleanValue(); } }