Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
RohitBB
Posts: 3
Registered: ‎08-14-2013
My Device: Blackberry 9860
My Carrier: Vodafone

Bluetooth printing via Blackberry 7

[ Edited ]

I am trying to print data on thermal bluetooth printer by using "btspp". I am able to discover the devices and even able to print the data..but due to some problem am not able to print the complete data.Sometimes it gets printed completely and other times not.behaves abnormally..I guess some thread issues are prevailing in my code..Here is my code

 

this screen is the front screen

 

package mypackage;
 
import java.io.DataOutputStream;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
String address;
String url;
private DataOutputStream output;
private StreamConnection con;
UiApplication uiScreen = UiApplication.getUiApplication();

public MyScreen()
{
// Set the displayed title of the screen
setTitle("MyTitle");
ButtonField b = new ButtonField("Find and print");
b.setChangeListener(new FieldChangeListener() {

public void fieldChanged(Field field, int context) {

if(BlueToothDiscovery.getBluetoothAddress()!=null){
try{
BlueToothPrint l_bpc = new BlueToothPrint(BlueToothDiscovery.getBluetoothAddress());
//l_bpc.setPriority(10);
Thread t= new Thread(l_bpc, "New");
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}catch(Exception e){

Dialog.alert("Exception "+e.getMessage());
}
}
else{

uiScreen.invokeLater(new Runnable() {

public void run() {
uiScreen.pushScreen(new BlueToothDiscovery());// TODO Auto-generated method stub

}
});

}


}
});

add(b);
}
}

 this is bluetoothprintcommand class

package mypackage;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
public class BlueToothPrint implements Runnable{
String address;
String url;
private DataOutputStream output;
private StreamConnection con;
public BlueToothPrint(String b_address){

address = b_address;

}

public void run() {
try {
String url = "btspp://" + address + ":1;authenticate=false;encrypt=false;master=false";

con = (StreamConnection) Connector.open(url, Connector.WRITE);

output = con.openDataOutputStream();



output.write(0x0A);
output.write(0x1B);
output.write(0x64);
output.write(0x02);


output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);
output.write("this is a testing string".getBytes());
output.write(0x0A);

output.flush();
output.close();
con.close();

}catch(final Exception e){

UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {
Dialog.alert("Exception printing...."+e.getMessage());

}
});
}
}
 
}

 and here is the bluetooth discovery class which discovers the bluetooth.

 

 

 
 
.
 
 
package mypackage;
import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
 
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class BlueToothDiscovery extends MainScreen implements DiscoveryListener{

private static String bluetoothAddress;

private DiscoveryAgent
agent = null
;

private Vector
deviceAddressVector
;

private Vector
deviceVector = null
;
private DiscoveryListener
l_DiscoveryListener
;
LabelField labelField;

private Contact[]
contactArray
;
private VerticalFieldManager
mainVerticalFieldManager
;
private LabelField[] mFields;
public BlueToothDiscovery(){

try{
l_DiscoveryListener = this;
labelField = new LabelField("Searching..... Please Wait");
add(labelField);

LocalDevice local = null;

try {

local = LocalDevice.getLocalDevice();
agent = local.getDiscoveryAgent();

} catch (BluetoothStateException e) {
//labelField.setText("Exception 1"+e.getMessage());
}
deviceAddressVector = new Vector();
deviceVector = new Vector();
// addDevices();
try {


agent.startInquiry(DiscoveryAgent.GIAC, l_DiscoveryListener);

} catch (BluetoothStateException e) {
e.printStackTrace();
// labelField.setText("Exception 2"+e.getMessage());
}

}catch(Exception ex){
ex.printStackTrace();
// labelField.setText("Exception 3"+ex.getMessage());
}


}


public static String getBluetoothAddress() {
return bluetoothAddress;
}
public void deviceDiscovered(RemoteDevice device, DeviceClass arg1) {

String names = null;
String address = device.getBluetoothAddress();
try {
names = device.getFriendlyName(device.isTrustedDevice());
} catch (IOException ex) {
ex.printStackTrace();
// labelField.setText("Exception 6"+ex.getMessage());
}
deviceAddressVector.addElement(device);
deviceVector.addElement(names + " (" + address + ") ");
}
public void inquiryCompleted(int type) {
labelField.setText("Search completed");
if (type != DiscoveryListener.INQUIRY_COMPLETED) {
if (type == DiscoveryListener.INQUIRY_TERMINATED) {
return;
}
} else {

}

try{


contactArray = new Contact[deviceVector.size()];
/*if(contactArray.length ==0){
uiScreen.pushScreen(loadingDialog);
}*/
for (int i = 0; i < deviceVector.size(); i++) {
String string = (String) deviceVector.elementAt(i);
String name = string.substring(0, string.indexOf("("));
String address = string.substring(string.indexOf("(") + 1, string.indexOf(")"));
contactArray[i] = new Contact(name.trim(), address.trim());



}

mainVerticalFieldManager = new VerticalFieldManager(VERTICAL_SCROLL|
NO_HORIZONTAL_SCROLL|
NO_VERTICAL_SCROLLBAR);

mFields = new LabelField[deviceVector.size()];
for (int i = 0; i < contactArray.length; i++) {
LabelField lg = new LabelField(contactArray[i].getDeviceName(), Field.FOCUSABLE | Field.USE_ALL_WIDTH);
//BluetoothManager.rhoLogInfo(" DEVICE["+String.valueOf(i)+"]="+devices[i]);
mFields[i] = lg;
add(lg);
}
mainVerticalFieldManager.add(new SeparatorField());





}catch (Exception e) {


}


}
public boolean invokeAction(int action)
{
try{
switch(action)
{
case ACTION_INVOKE: // Trackball click.
int i;
for (i = 0; i < mFields.length; i++) {
if (mFields[i].isFocus()) {

final Contact c = contactArray[i];
bluetoothAddress = c.getDeviceAddress();
UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

Dialog.alert("Bluetooth address "+bluetoothAddress);
Dialog.alert("Device Name "+c.getDeviceName());

}
});
UiApplication.getUiApplication().popScreen(this);

return true; // We've consumed the event.
}
}
} }catch(Exception e){
UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

Dialog.alert("Exception on selecting it");

}
});

}
return super.invokeAction(action);
}
public void serviceSearchCompleted(int arg0, int arg1) {
// TODO Auto-generated method stub

}
public void servicesDiscovered(int arg0, ServiceRecord[] arg1) {
// TODO Auto-generated method stub

}
class Contact {
private String deviceName;
private String deviceAddress;
public Contact(String deviceName, String deviceAddress) {
this.deviceName = deviceName;
this.deviceAddress = deviceAddress;
}
public String getDeviceName() {
return deviceName;
}
public String getDeviceAddress() {
return deviceAddress;
}
}
}
 

 

 

 

Also please tell me is it the right way for bluetooth printing via blackberry...as  same code is used in j2me...pls share helpful links on this topic..

 

 

 

Please use plain text.
New Contributor
RohitBB
Posts: 3
Registered: ‎08-14-2013
My Device: Blackberry 9860
My Carrier: Vodafone

Bluetooth printing via blackberry 7

Anybody please help me out in printing on a bluetooth printer via blackberry 7..I am stuck with this problem for weeks and now it has started cringing me...Any kind of help is welcome...!!!!

Please use plain text.
Developer
simon_hain
Posts: 15,876
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Bluetooth printing via blackberry 7

welcome to the support forums.
you posted this to the native boards, i have asked a moderator to move it to the appropriate java forum.

all blocking code should be executed on a thread, you do not do that. see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865 for details.
your Thread.sleep call is on the event thread, for example, and blocks the UI.

please edit your post and use the code tag to format the code, it makes it better readable.

regarding your code i took only a quick look. you should log your exceptions, maybe you get something usable from there already.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
RohitBB
Posts: 3
Registered: ‎08-14-2013
My Device: Blackberry 9860
My Carrier: Vodafone

Re: Bluetooth printing via blackberry 7

do you have some piece of code to do the mentioned..and no exceptions are not coming..its just that printing behavior is abnormal as sometimes it prints complete data and others it prints small chunks only...
Please use plain text.