I am writing an application that sends data to a bluetooth printer. Can someone help me? how can i use bluetooth stack for android? or is there an external api or sdk to use?
Here is my bluetooth search code ...
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); registerReceiver(ActionFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); btArrayAdapter.add(device.getName() + "\n" + device.getAddress()); btArrayAdapter.notifyDataSetChanged(); } } };
and here is my code to send data to the printer.
BluetoothDevice mDevice = bluetoothAdapter.getRemoteDevice("00:15:FF:F2:56:A4"); Method m = mDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); mBTsocket = (BluetoothSocket) m.invoke(mDevice, 1); System.out.println("Connecting....."); mBTsocket.connect(); System.out.println("Connected"); OutputStream os = mBTsocket.getOutputStream(); os.flush(); os.write(Receipt.getBytes());
When I write socket.close (), the data does not get printed to the printer, since the socket is closed before printing the data .. and if I did not write socket.close (), then the data is printed only once .. I could not print the data a second time until I restart the bluetooth of my phone.
can anyone have a solution? or is there any other way to get rid of this seal?
Nirav bhandari
source share