android - Application force close when printer device off -
i developed app can connect device printer.
firstly connect app device, when printer device off application force closed, dont know why happens on devices.
below script connect it:
public boolean openbtbyaddress(context _context,string address) { activity activity = (activity) _context; boolean cek = false; try { mbluetoothadapter = bluetoothadapter.getdefaultadapter(); if(mbluetoothadapter == null) { // mylabel.settext("no bluetooth adapter available"); toast.maketext(_context,"no bluetooth adapter available",toast.length_short).show(); cek= false; } if(!mbluetoothadapter.isenabled()) { intent enablebluetooth = new intent(bluetoothadapter.action_request_enable); activity.startactivityforresult(enablebluetooth, 0); } set<bluetoothdevice> paireddevices = mbluetoothadapter.getbondeddevices(); if(paireddevices.size() > 0) { (bluetoothdevice device : paireddevices) { bluetoothdevice da = device; // rpp300 name of bluetooth printer device // got name list of paired devices if (device.getaddress().equals(address)) { cek= openbt(device,_context); //mmdevice = device; break; } } } else { cek= false; } }catch(exception e){ e.printstacktrace(); cek= false; } return cek; } private boolean openbt(bluetoothdevice bd,context _context ) throws ioexception { boolean cek = false; try { uuid uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb"); mmsocket = bd.createrfcommsockettoservicerecord(uuid); mmsocket.connect(); mmoutputstream = mmsocket.getoutputstream(); mminputstream = mmsocket.getinputstream(); beginlistenfordata(); toast.maketext(_context,"connect_success",toast.length_short).show(); cek =true; } catch (exception e) { e.printstacktrace(); toast.maketext(_context,"connect_failed",toast.length_short).show(); cek =false ; } return cek; } private void beginlistenfordata() { try { final handler handler = new handler(); final byte delimiter = 10; stopworker = false; readbufferposition = 0; readbuffer = new byte[1024]; workerthread = new thread(new runnable() { public void run() { while (!thread.currentthread().isinterrupted() && !stopworker) { try { if(mminputstream != null) { int bytesavailable = mminputstream.available(); if (bytesavailable > 0) { byte[] packetbytes = new byte[bytesavailable]; mminputstream.read(packetbytes); (int = 0; < bytesavailable; i++) { byte b = packetbytes[i]; if (b == delimiter) { byte[] encodedbytes = new byte[readbufferposition]; system.arraycopy( readbuffer, 0, encodedbytes, 0, encodedbytes.length ); final string data = new string(encodedbytes, "us-ascii"); readbufferposition = 0; // tell user data sent bluetooth printer device handler.post(new runnable() { public void run() { } }); } else { readbuffer[readbufferposition++] = b; } } } } } catch (ioexception ex) { stopworker = true; } } } }); workerthread.start(); } catch (exception e) { e.printstacktrace(); } }
Comments
Post a Comment