Reading from multiple Bluetooth devices in Android? - android

Just to make it short. I want to connect to three devices and read the data from all the three at the same time. I know how to connect to one and i can read the data from that But What i want is to connect the other two and read from them.Any suggestions , or other example i can follow.
Here is part of the code which is similar to mine but shorter . This code can connect to one device using the mac. So How to modify this to connect to three devices at the same time and display the data they are sending?
Please help i really need it cause am stuck at this part of my project. Thanks for the help.
private boolean connected = false;
private BluetoothSocket sock;
private InputStream in;
public void test() throws Exception {
if (connected) {
return;
}
BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
getRemoteDevice("00:14:C5:A1:02:67");
Method m = zee.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
sock = (BluetoothSocket)m.invoke(zee, Integer.valueOf(1));
devicecon.setText("Connecting......");
sock.connect();
devicecon.setText("Connected");
in = sock.getInputStream();
byte[] buffer = new byte[50];
int read = 0;
Log.d("ZeeTest", "++++ Listening...");
try {
while (true) {
read = in.read(buffer);
connected = true;
StringBuilder buf = new StringBuilder();
for (int i = 0; i < read; i++) {
int b = buffer[i] & 0xff;
if (b < 0x10) {
buf.append("0");
}
buf.append(Integer.toHexString(b)).append(" ");
}
Log.d("ZeeTest", "++++ Read "+ read +" bytes: "+ buf.toString());
}
} catch (IOException e) {}
Log.d("ZeeTest", "++++ Done: test()");

All you need to do is create a new thread (I suggest using an instance of AsyncTask for this rather than an actual Thread) for each device and call the connection code snippet
BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
getRemoteDevice("00:14:C5:A1:02:67");
Method m = zee.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
sock = (BluetoothSocket)m.invoke(zee, Integer.valueOf(1));
devicecon.setText("Connecting......");
sock.connect();
from inside that new thread.

Related

issue printing from app to thermal printer

Hi guys i'm creating a method to print an invoice from my app, but when i send the bytes to print it doesn't print all of the bytes i'm sending to the printer, the last bytes are getting cut all time resulting in an incomplete invoice, this is the code that i'm using at the moment:
public void Print(string nombreImpresora, string formatoFactura)
{
var listOfDevices = BluetoothAdapter.DefaultAdapter;
if (listOfDevices == null)
throw new Exception("No Bluetooth adapter found.");
if (!listOfDevices.IsEnabled)
throw new Exception("Bluetooth adapter is not enabled.");
var device = (from bd in listOfDevices.BondedDevices
where bd.Name == nombreImpresora
select bd).FirstOrDefault();
if (device == null)
throw new Exception("Named device not found.");
BluetoothSocket socket;
var uuid = device.GetUuids()?.ElementAt(0);
if (uuid != null)
{
socket = device.CreateInsecureRfcommSocketToServiceRecord(uuid.Uuid);
}
else
{
socket = device.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
}
if (socket.IsConnected)
{
return;
}
socket.Connect();
byte[] completeBuffer = Encoding.ASCII.GetBytes(formatoFactura);
Toast.MakeText(Forms.Context, Convert.ToString(completeBuffer.Length), ToastLength.Short).Show();
var bufferSize = 256;
int completedBufferLength = completeBuffer.Length;
List<byte[]> bufferList = new List<byte[]>();
for (int i = 0; i < completedBufferLength; i = i + bufferSize)
{
byte[] val = new byte[bufferSize];
if (completedBufferLength < i + bufferSize)
{
bufferSize = completedBufferLength - i;
}
Array.Copy(completeBuffer, i, val, 0, bufferSize);
bufferList.Add(val);
}
for (int j = 0; j < bufferList.Count; j++)
{
socket.OutputStream.Write(bufferList[j], 0, bufferList[j].Length);
}
socket.Close();
socket.Dispose();
}
i'm sending a string and converting it to bytes in the method above, the string is a custom invoice that i made with the invoice data from another page of my app.
The printer that i'm using is Bixolon SPP-R310, at this point i don't know if it is a printer related issue really.
¿Can anyone help me with this pls?
thanks in advance
First of all try to append some empty lines at the end of your string using "\n". If the output is still incomplete change device.CreateInsecureRfcommSocketToServiceRecord to device.createRfcommSocketToServiceRecord.

Bluetooth Code to send string In Android

I have got list of paired devices of Bluetooth.Now i want to send text to that particular paired device.I have done code for that.
Below is the code:
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 is 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();
}
}
}
But i'm getting these Error:
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
04-05 10:53:41.356 5580-5580/? W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:900)
04-05 10:53:41.356 5580-5580/? W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:912)
04-05 10:53:41.356 5580-5580/? W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:531)
So, please help me how to solve this error.
What kind of device are you trying to connect to?
-> Based on discussion: If trying to connect to another phone running Android there must be application which is accepting the connection. Working example is available from Google: BluetoothChat
You can try speeding up the connection process:
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
Or you can try using different UUID (are there more supported)?
System.out.println(uuids);
Or you can try using insecure connection:
device.createInsecureRfcommSocketToServiceRecord(...)
BTW: Your code for reading data from inpust stream won't work very well. Take a look at example from Google Chat Application: BluetoothChatService

Android Bluetooth: Slow data rates calculated from BluetoothSocket

Using: HTC Legend and HTC Salsa
I'm calculating the speed using:
while(true)
{
try
{
int num = in.read(buffer);
if(reading == false)
{
prevTime = SystemClock.uptimeMillis();
reading = true;
}
else
{
//Calculate KB/s
count += num;
Long deltaTime = SystemClock.uptimeMillis()- prevTime;
if(deltaTime >= 1000)
{
Float speed = (float)count/deltaTime;
Log.d(TAG,"Data: " + speed + "KB/s");
count = 0;
prevTime = SystemClock.uptimeMillis();
}
}
} catch (IOException e) {
}
}
And writing some test data using
out.writeUTF("ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" +
"ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" +
"ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa" +
"ababababababababababababababababbabababaababababababababababababababababbabababaababababababababababababababababbabababa");
out.flush();
The writes are within another threads while(true) also.
I'm getting the following results.
02-13 18:17:16.897: D/krazyTag(3432): Data: 31.554672KB/s
02-13 18:17:17.927: D/krazyTag(3432): Data: 29.854227KB/s
02-13 18:17:18.977: D/krazyTag(3432): Data: 29.285034KB/s
02-13 18:17:20.067: D/krazyTag(3432): Data: 38.446888KB/s
02-13 18:17:21.097: D/krazyTag(3432): Data: 35.89484KB/s
02-13 18:17:22.127: D/krazyTag(3432): Data: 33.67118KB/s
02-13 18:17:23.227: D/krazyTag(3432): Data: 33.512726KB/s
02-13 18:17:24.307: D/krazyTag(3432): Data: 33.277622KB/s
Which is confusing me since the phones specs state they use Bluetooth® 2.1 with EDR
Which is capable of 260KB/S but I'm not even getting the old standard 90KB/s
I'm not sure if it's my stream and read/write calls (I'm using a buffered datainputstream)
Or if I'm calculating things wrong or have the wrong information?
I think the speed depends on your implementation of the Send and Receive threads, since you connects 2 Android devices with your own applications. Could you post your implementation?
I got the same problem also.
I am using ACER TAB A500 to communicate with a Bluetooth stick connected to PC and I got even slower result 12,3KB/s for sending data only.
That's why I did some experiments. I sent a message for 10000times and I got that the data rate depends on the length of the message.
For 1KB message, the data rate is 232KB/s. For 40Byte message, the
data rate is 18KB/s. For 1Byte message, the data rate is
0.48KB/s.
Here is my code:
// Get the BluetoothDevice object.
while(true){
driverBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
driverBluetoothDevice = driverBluetoothAdapter.getRemoteDevice("XX:XX:XX:XX:XX:XX");
if (driverBluetoothDevice == null){
break;
}
Method insecureMethod = driverBluetoothDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
byte portNumber = 5; // The SPP in port 5.
driverBluetoothSocket = (BluetoothSocket) insecureMethod.invoke(driverBluetoothDevice, portNumber);
// Try to connect to the Bluetooth device.
try {
driverBluetoothSocket.connect();
} catch (IOException e1) {
// Failed to connect to the device
break;
}
// Open input and output stream.
try {
driverInputStream = driverBluetoothSocket.getInputStream();
} catch (IOException e) {
break;
}
try {
driverOutputStream = driverBluetoothSocket.getOutputStream();
} catch (IOException e) {
break;
}
byte[] message = new byte[3000];
Random randomGenerator = new Random();
for (int i = 0; i < message.length; i++){
message[i] = (byte) randomGenerator.nextInt(100);
}
Date TimeValue = new Date();
long TimeStamp1 = TimeValue.getTime();
for (int i = 0; i < 10000; i++){
try {
driverOutputStream.write(message, 0, message.length);
} catch (IOException e) {
break;
}
}
TimeValue = new Date();
long TimeStamp2 = TimeValue.getTime();
long TimeDifference = TimeStamp2 - TimeStamp1;
TimeDifference = 0;
break;
}
Not sure if this helps with your speed problem and I could have overlooked this detail in your code snippet but are you reading and writing on the same thread? The documentation suggests you don't,
First and foremost, you should use a dedicated thread for all stream reading and writing. This is important because both read(byte[]) and write(byte[]) methods are blocking calls.
Bluetooth Android Developer

RFCOMM connection between two Android devices?

I have two Android devices which I want to connect, using Bluetooth, and transfer data over an RFCOMM channel. I only one one device to receive data, while the other device sends it...
Using this code, I am able to connect to the other device and begin listening to an RFCOMM channel:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 2);
socket.connect();
class BasicThread implements Runnable{
public void run() {
try {
InputStream stream = socket.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
while (true){
Log.d("myapp", "now listening...");
latestLine = r.readLine();
Log.d("myapp", latestLine);
}
} catch (IOException e) {
}
}
}
new Thread(new BasicThread()).run();
Using the other device, I have implemented a listening socket like this:
Method m = blue.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
BluetoothServerSocket socket = (BluetoothServerSocket) m.invoke(blue, 2);
BluetoothSocket sock = socket.accept();
Log.d("myapp", "Connected...\n\n\n\n\n\n\n\n");
OutputStream s = sock.getOutputStream();
final PrintWriter out = new PrintWriter(s);
They both connect on RFCOMM channel 2, and both SEE eachother, however, the second device always remains blocked at the BluetoothSocket sock = socket.accept();
Any help?
OK, I am newbie, but I can try to help. So here is my experience, I managed to connect two devices using reflection. My Android phone is receiving data using method listenUsingInsecureRfcommOn, while other devices are masters in communication and send the data over BT SPP. I had a problem with this method since it makes no visible SDP record, so I could not detect it with other devices. Because of that, I made small sniffer using Bluecove and Java SE that tries to connect to every port in given range. Here's the code:
package application.test;
import static java.lang.System.out;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
public class RfCommClient {
public static void main(String args[]) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
String add = "btspp://8C71F894A36D:";
String par = ";authenticate=false;encrypt=false;master=true";
String url = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd;HH-mm-ss-SSS");
for (int i = 1; i < 15; ++i) {
try {
url = add + i + par;
out.format("Time: %s, port = %d\n", sdf.format(System.currentTimeMillis()), i);
StreamConnection conn = (StreamConnection) Connector.open(url);
PrintStream ops = new PrintStream(conn.openOutputStream());
ops.println("Hi there...");
// response
Thread.sleep(1000);
InputStream is = conn.openInputStream();
byte[] resp = new byte[5];
int r = is.read(resp);
out.println("r = " + r + ", response = " + new String(resp, "US-ASCII"));
Thread.sleep(10 * 1000);
conn.close();
} catch (Exception e) {
out.println("Exception occured, time = " + sdf.format(System.currentTimeMillis()) + ", i = " + i);
//e.printStackTrace();
}
}
}
}
What I've learned it that some ports are taken, and that some ports can not be uses (as documentation says, e.g. port 0). For example, port 2 I believe was taken, because when I send some data to it I receive 5 chars back beginning with ERR :).
While, on the other hand, my thread is still waiting?! :)
That leads us to another thing I noticed, ports (or channels) are not always mapped to desired number. For example, to me often happened that I want to send something on port 15, but on Android, thread waiting on port 9 received the data :)
So I suggest, check which port is really allocated!
You can achieve that using the code I posted.
And another thing, here is a link to channelPicker function, which selects channel when ordinary API is used, if I am not mistaken, inside some constants should represent reserved channels.
I just noticed something, my code for registering port is slightly different, here is how I do it:
Method m = cba.getDeclaredMethod("listenUsingInsecureRfcommOn", int.class);
ss = (BluetoothServerSocket) m.invoke(BluetoothAdapter.getDefaultAdapter(), port);
Anyway, I know that this is probably too late, but, maybe someone in future has similar question.

How to detect EOF on android bluetooth file transfer?

I have implemented a bluetooth connection using the now-classic Google Bluetooth Chat code. However, I have a question which I just cannot seem to wrap my brain around.
The reading of the input stream goes something like this:
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
Now, that's fine if I was just printing out the characters I was receiving as in the original example. However, suppose I wanted to transfer an image file. I don't know the size of the file, so I cannot count the bytes received or anything like that. In my tests, I don't seem to be ever receiving a "-1" from the input stream, which appears to be the "norm" for reading from input streams. So how can I know that I have reached the end of the file that was being sent?
Thank you for your help and your time.
It seems Android bluetooth input streams never return -1.
I guess setup a simple protocol by sending file size in the first place and EOF signals at last will help.
No it does not. Android sends -1 only when the Socket is closed as far as I know. So a workaround could be to do a reconnect, but I was trying that for hours and did not get it working, since I do not understand this "special" Code here (copied from a Stackoverflow Thread) for setting up the socket:
BluetoothSocket tmp = null;
Log.d(TAG, "New Connection initialized");
Method m;
try {
m = device.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (Exception e) {
e.printStackTrace();
}
mmSocket = tmp;
This Socket only works, when my App is started for the first filetransfer. If I want to "Reconnect" with a completely new instantiated Object (and a new Socket created with that Code), the program freezes on the blocking method mmSocket.connect(). It seems like the Method never comes to an ending. This is driving me nuts...
Try
while ((bytes = mmInStream.read(buffer) != -1)
and see if that helps.
Try this:
public void run() {
byte[] buffer;
ArrayList<Integer> arr_byte = new ArrayList<Integer>();
while (true) {
try {
int data = mmInStream.read();
if(mmInStream.available()>0) {
arr_byte.add(data);
} else {
arr_byte.add(data);
buffer = new byte[arr_byte.size()];
for(int i = 0 ; i < arr_byte.size() ; i++) {
buffer[i] = arr_byte.get(i).byteValue();
}
Log.e("INPUT",new String(buffer));
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
arr_byte = new ArrayList<Integer>();
}
} catch (IOException e) {
break;
}
}
}

Categories

Resources