Example connecting Android and Bluecove on a Mac - android

I have an Android device (HTC Incredible) and I would like to setup bluetooth communication between the android phone and a MacBook Pro. I have got bluecove to start on the Mac and I have done coding using Sockets in Android, but I can not get a connection working. Here is my andriod code
BluetoothDevice device = reciever.getDevice("00:25:00:XX:XX:XX"); //my bluetooth address
UUID generalUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
socket = device.createRfcommSocketToServiceRecord(generalUuid);
socket.connect();
writer = new OutputStreamWriter(socket.getOutputStream());
reader = new InputStreamReader(socket.getInputStream());
on the Mac
String serverUUID = "btspp://localhost:0000110100001000800000805F9B34FB;name=matt";
StreamConnectionNotifier notifier = (StreamConnectionNotifier) Connector.open(serverUUID);
StreamConnection connection = notifier.acceptAndOpen();
// prepare to send/receive data
byte buffer[] = new byte[100];
String msg = "hello there, client";
InputStream is = connection.openInputStream();
OutputStream os = connection.openOutputStream();
Any help or examples would be helpful.

You change the line of code
socket = device.createRfcommSocketToServiceRecord (generalUuid);
by
Method m;
m = device.getClass (). getMethod ("createRfcommSocket", new Class [] {int.class});
tmp = (BluetoothSocket) m.invoke (device, 1);
mmSocket = tmp;
mmSocket.connect ();
in Android

you can use bluecove for android.
add the bluecove library and the bluecove-android to your build path and the work should be easier.

Related

Android : Multicast is working with IPV4, not with IPV6

This code works with this address "228.5.6.7", but not with this one "FF7E:230::1234" (devices used: Samsung S4 & TabA).
// join a Multicast group and send the group salutations
String msg = "Hello";
InetAddress group = InetAddress.getByName("228.5.6.7");
MulticastSocket s = new MulticastSocket(6789);
s.joinGroup(group);
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), group, 6789);
s.send(hi);
// get their responses!
byte[] buf = new byte[1000];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
s.receive(recv);
// OK, I'm done talking - leave the group...
s.leaveGroup(group);
I tried this example too, it gave me the same result.
Impossible to know if the problem happens when the group is joined or when the DatagramPacket is sent.

jsr 82 connect connection is not created (failed or aborted) sometimes appear when I try to connect to Cashregister via Bluetooth rfcomm

I'm trying to connect to my cashregister via rfcomm bluetooth on my phone Lenovo A369i using reflection like this:
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = null;
String macAddr = "00:01:90:ED:27:73";
for (BluetoothDevice bd : ba.getBondedDevices())
{
if (bd.getAddress().equals(macAddr))
{
device = bd;
break;
}
}
Method htcMethod = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
htcMethod.invoke(device, Integer.valueOf(1));
And sometimes I'v get error "jsr 82 connect connection is not created (failed or aborted)"
I'v already add all bletooth permissions to androidManifest.xml
I don't know what to do, please help me...

Device discovery in local network

I'm currently developing an android app using SDK >= 16 which should be able to discover different android devices (later also iOS devices) in a local area network using the WiFi radio.
My first guess was to use multicast which turned out to be non functional on my Samsung Galaxy S2: packets are only received when sent from the same device.
My second guess is to actively scan the network using a limited IP address range and wait for a proper response. Unfortunately, this implies that the network uses DHCP to address the IP addresses.
None of the above solutions seem to be the perfect solution.
My current solution for my first guess:
public class MulticastReceiver extends AsyncTask<Activity, Integer, String> {
private static final String host = "224.1.1.1";
private static final int port = 5007;
private static final String TAG = "MulticastReceiver";
protected String doInBackground(Activity... activities) {
WifiManager wm = (WifiManager)activities[0].getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
multicastLock.acquire();
String message = "Nothing";
if (multicastLock.isHeld()) {
Log.i(TAG, "held multicast lock");
}
try {
InetAddress addr = InetAddress.getByName(host);
MulticastSocket socket = new MulticastSocket(port);
socket.setTimeToLive(4);
socket.setReuseAddress(true);
socket.joinGroup(addr);
byte[] buf = new byte[5];
DatagramPacket recv = new DatagramPacket(buf, buf.length, addr, port);
socket.receive(recv);
message = new String(recv.getData());
socket.leaveGroup(addr);
socket.close();
} catch (Exception e) {
message = "ERROR " + e.toString();
}
multicastLock.release();
return message;
}
}
This code results in blocking on line socket.receive(recv); If I specify a timeout, I get a timeout exception.
Check my answer in very similar question Android Network Discovery Service (ish) before API 14
I do not belive that multicast is not working on Galaxy S2, some time ago when I was coding some network application, I made several test on many devices, some older like G1 but also on S2, S3 and Galaxy Tab 10.
But to be able to use multicast you must enable it programatically.
Have you used this piece of code?
WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null){
WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
lock.acquire();
}
Check out http://developer.android.com/training/connect-devices-wirelessly/index.html It mentions two ways of finding local services- NSD and wifi direct.

Issue with Bluetooth Device pairing

I am developing one simple Bluetooth application where I want to send a text message. Facing issues when trying to pair the devices, it throws exception “java.io.IOException: Service discovery failed”. For more details I am posting code herewith.
Ported application on LG phone Android version 2.3.3
private static final UUID MY_UUID = UUID.fromString("00000003-0000-1000-8000-00805F9B34FB");
BluetoothDevice btDevice = BluetoothActivity.btDevices.get(position).getBtDevice();
clientSocket = btDevice.createRfcommSocketToServiceRecord(MY_UUID);
//Method m = btDevice.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] { UUID.class } );
//clientSocket =(BluetoothSocket) m.invoke(btDevice, MY_UUID);
//Method m = btDevice.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
//clientSocket =(BluetoothSocket) m.invoke(btDevice, 1);
if(clientSocket!=null)
{
if(BluetoothActivity.btAdapter.isDiscovering()){
BluetoothActivity.btAdapter.cancelDiscovery();
}
//facing issue during paring
clientSocket.connect();
tmpOut = clientSocket.getOutputStream();
tmpOut.write("HelloWorld.txt".getBytes());
if(tmpOut!=null){
tmpOut.close();
}
}
Any kind of help is appreciated,
Try using this UUID:
fa87c0d0-afac-11de-8a39-0800200c9a66
for some reason, this was the only UUID I could ever get to work... I have no idea why.

Android: Bluetooth Serial (Com Port) communication with Android Phone

I am trying to communicate with a Bluetooth programmable Microcontroller. The Bluetooth device on the microcontroller communicates (specifically) on Bluetooth Serial COM Port number 4.
QUESTION: How can I get the Android App to read data from this COM port (number 4)?
I know the UUID is a well known unique ID,that works for this device, but I don't think that it has anything to do with specifying the COM port.
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
btSocket = btDevice.createRfcommSocketToServiceRecord( myUUID);
btSocket.connect();
valid.append( btDevice.getName() + "\n" + btDevice.getAddress());
north.append("Socket Connected");
InputStream mmInStream = btSocket.getInputStream();
OutputStream mmOutStream = btSocket.getOutputStream();
byte[] buffer = new byte[10];
int bytes;
StringBuffer str = new StringBuffer();
while (true) {
try {
mmOutStream.write("a".getBytes());
//Reads a # of bytes until the end of stream is reached
bytes = mmInStream.read(buffer);
//Transform to string
str.append(buffer.toString()+"\t"); //Clear the buffer
Log.e("DATA", "THE DATA: "+ str.toString());
south.setText(str.toString());
str.delete(0,str.length());
} catch (IOException e) {
break;
} }}
The COM port is something that exists only on the microcontroller, not the Bluetooth device attached to it. The Bluetooth device does not even know which COM port the microcontroller used to connect to it. The Bluetooth device's connection to the micro is via the TX and RX lines. The fact that they are attached to pins on the micro assigned to a specific COM port is irrelevant and unknown to the Bluetooth device.
I had this problem with a custom bluetooth device I built. Instead of using createRfcommSocketToServiceRecord in your connect thread, try something similar to the following:
public ConnectThread(BluetoothDevice device) throws
SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
mmDevice = device;
BluetoothSocket tmp = null;
// Force a BluetoothSocket for a connection with the
// given BluetoothDevice
Method m = mmDevice.getClass().getMethod("createRfcommSocket",
new Class[]{int.class});
mmSocket = (BluetoothSocket)m.invoke(mmDevice, Integer.valueOf(1));
}
Where my mmDevice is your btDevice.
This forces a socket connection between the unknown device and the smartphone. From what I've heard, there's an issue in Android connecting "non-similar" devices. Worth a shot.

Categories

Resources