Android sample bluetooth code to send a simple string via bluetooth -
i want send simple string data such 'a' android device other 1 via bluetooth. looked sample bluetooth code in android sdk complex me. cannot understand how can send specific data when press button. how can solve problem?
private outputstream outputstream; private inputstream instream; private void init() throws ioexception { bluetoothadapter blueadapter = bluetoothadapter.getdefaultadapter(); if (blueadapter != null) { if (blueadapter.isenabled()) { set<bluetoothdevice> bondeddevices = blueadapter.getbondeddevices(); if(bondeddevices.size() > 0) { object[] devices = (object []) bondeddevices.toarray(); bluetoothdevice device = (bluetoothdevice) devices[position]; parceluuid[] uuids = device.getuuids(); bluetoothsocket socket = device.createrfcommsockettoservicerecord(uuids[0].getuuid()); socket.connect(); outputstream = socket.getoutputstream(); instream = socket.getinputstream(); } log.e("error", "no appropriate paired devices."); } else { log.e("error", "bluetooth disabled."); } } } public void write(string s) throws ioexception { outputstream.write(s.getbytes()); } public void run() { final int buffer_size = 1024; byte[] buffer = new byte[buffer_size]; int bytes = 0; int b = buffer_size; while (true) { try { bytes = instream.read(buffer, bytes, buffer_size - bytes); } catch (ioexception e) { e.printstacktrace(); } } }
Comments
Post a Comment