I want to receive images via Bluetooth and display that image in Image View.I know how to pass image from one activity to another activity but i don't no how to receive images using Bluetooth.
Android framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.
Using the Bluetooth APIs, an Android application can perform the following:
Scan for other Bluetooth devices
Query the local Bluetooth adapter for paired Bluetooth devices
Establish RFCOMM channels
Connect to other devices through service discovery
Transfer data to and from other devices
Manage multiple connections
Create a BluetoothSocket and connect to it:
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(<your-device>.getUuids()[0].getUuid());
socket.connect();
Listen to the socket (Get data from the device)
InputStream inStream = socket.getInputStream();
while (inStream.available() > 0) {
inStream.read(); // <-- data from device
}
Write to the socket (Send data to the device)
OutputStream outStream = socket.getOutputStream();
byte[] bytes = <some-data>
outStream.write(bytes);
and for more details you can read Bluetooth Api Documentation here
Related
I'm trying to develop an Android application for a medical device using Bluetooth SPP. My Android application works as Bluetooth server. The problem is that the medical device (UA-767PBT) may not conform SDP process and uses a fixed port# for the connection. So the connection only works after reboot the Android device.
I'm seeking the way to create a Bluetooth server side socket with a specific rfcomm channel/port# using Java reflection.
I've found some code doing the similar things using Java reflection:
Create Bluetooth client side socket with a specific port#.
//Instead of using createRfcommSocketToServiceRecord
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
// using port#1 / channel #1
clientSocket = (BluetoothSocket)m.invoke(mmDevice, Integer.valueOf(1));
Inspect the port# used in Bluetooth server side socket.
BluetoothServerSocket serverSocket;
BluetoothSocket socket;
int port;
serverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(SERVICE_NAME, DEVICE_UUID);
Field mSocketField = BluetoothServerSocket.class.getDeclaredField("mSocket");
mSocketField.setAccessible(true);
socket = (BluetoothSocket) mSocketField.get(serverSocket);
mSocketField.setAccessible(false);
Field mPortField = BluetoothSocket.class.getDeclaredField("mPort");
mPortField.setAccessible(true);
port = (Integer) mPortField.get(socket);
mPortField.setAccessible(false);
Log.d("BT Server:", "The random port#: " + port);
socket = serverSocket.accept();
Create Bluetooth server side socket with a specific port number. How?
I also seek other solutions for this connection problem.
I am developing Bluetooth based application; here I want to send text data as well as image. I am able to pair my device with the other Bluetooth enable phone, but even after successful pairing I am not able to send require data to targeted phone.
As of my search in Android we didn’t require any Headerset during transferring the data "<"just comparing J2me Bluetooth">". Then how to send data to targeted phone?
The code snippet which I used after successful connection establishment with target phone is herewith.
clientSocket.connect(); //successfully connected with targeted phone
tmpOut = clientSocket.getOutputStream();//out stream opened
tmpOut.write(image, 0, image.length);//trying to send data, didn't get any exception
//closed clientsocket and outputstream
I'm trying to use my Android phone as a handsfree kit (like the one for cars) in order to connect to another phone (any phone) and perform some handsfree functionality like (answer an incoming call, reject,.. etc) which can be done using the AT commands for handsfree profile.
For that, I'm using the well-known Bluetooth chat App, and reflection work around in order to establish a connection with any device:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device,1);
However, in order to achieve the handsfree functionality and understand the AT commands that I'm sending, the connected phone needs to be over the handsfree profile which uses the UUID: "0000111f-0000-1000-8000-00805F9B34FB"
Therefore, is there a way to achieve a connection to the handsfree profile?
Thanks!
You should only use this code when you have no other choice. The 1 in this code is the RFCOMM port. Each service has it's own RFCOMM port. This port is usually random between 1 and 31. You need to know which port the service (here handsfree profile) is using on the device that you want to connect to. You have to use the createRfcommSocketToServiceRecord method from the BluetoothDevice object to do this:
try { clientSocket = bluetoothDevice.createRfcommSocketToServiceRecord( serviceUUID ); }
catch (IOException e)
{
// handle error
}
This code is the correct way to use Bluetooth and should replace the one you're using.
I am building tic tac to for two players and need a Bluetooth connection to exchange some data, I can enable Bluetooth, enable discover-ability but my problem in "BluetoothServerSocket" and the client "BluetoothSocket", I don't know how to manipulate this part,
this is the code:
ArrayList<String>al=new ArrayList<String>();
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = ba.getBondedDevices();
if(pairedDevices.size()>0)
for(BluetoothDevice d: pairedDevices)
al.add(d.getName()+" , "+d.getAddress());
if (!ba.isEnabled())
ba.enable();
BluetoothDevice device;
Intent dis=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
dis.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
startActivity(dis);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID);
socket.connect();
InputStream is=socket.getInputStream();
OutputStream os=socket.getOutputStream();
Both the players will communicate over 'BluetoothSocket's using which you can send/receive data using InputStreams and OutputStreams. But for getting such a pair of sockets you can do this thing :
Create BluetoothServerSocket on the side of one player and other one connects to it. The BluetoothServerSocket listens for connections using the 'accept' method which blocks till a client BluetoothSocket connects to it. After that the BluetoothServerSocket.accept() method returns a BluetoothSocket which can be used with the client Btsocket for 2-way info transfer.
Hope this helps...
PS: createRfcommSocketToServiceRecord just creates one such client mentioned above. You may use the same UUID for both sides
It seems you're missing a lot of complexity regarding data exchange. Mainly you'll need to deal with threads to listen/send data. Here you have a complete implementation of what I'm talking about: https://github.com/buddles/AndBT/blob/master/AndBT/src/br/pucrs/tcii/BluetoothService.java
Have you considered to use an already implemented library? This project comes with a TicTacToe sample and a Chat app that supports up to seven connections: https://github.com/buddles/AndBT
You may refer this link. This is a simple bluetooth chat app. You can modify this app to send and receive the required data.
My goal is to write an app that allows me to control my Motorola Xoom with a Playstation 3 Bluetooth Remote Control.
The device is able to be discovered by the native bluetooth app & classified as being a joystick. However, I cannot pair via the native bluetooth app because the app requires a PIN & the device does not have a pin that I am aware of.
So far I am able to programmatically discover the device & create a socket, however all attempts to connect to the device fail.
In both cases:
UUID u = UUID.fromString("00001124-0000-1000-8000-00805f9b34fb");
This is supposed to be the UUID used by HID devices. I also used the method described on another site to verify the UUID is available on the device.
Method1 (many people seem to have issues with this):
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(u);
socket.connect();
Result: IOException "Service discovery failed"
Method2 (the accepted workaround to Method1. I also tried ports 1-100):
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
Result: IOException "Connection refused"
I have tried this code using 3 devices (plus their UUIDs):
Playstation Remote
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
Nintento Wiimote
00001000-0000-1000-8000-00805f9b34fb
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
Microsoft Bluetooth Number Pad
(which sucessfully pairs, with a pin, via the natvie bluetooth app)
00001000-0000-1000-8000-00805f9b34fb
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
To be able to connect to the devices you have to connect over the HID profile, what you are trying is connecting over SPP (Serial Port Profile) to the UUID for HID etc, this will not work.
In addition these devices have some "custom" HID protocol descriptors that allow it to work with this pre-paired gaming consoles, you will need to get access to those to be able to control the Xoom with these controllers