Kiran
Kiran

Reputation: 3133

connect to external bluetooth device and start receiving data

want to connect to device and start receiving data from the external device now able to list devices and pair to that .....now wish to connet to it and start receiving data from it

Is there any way because i dont exactly know wat kind of data it is sending 
means in which formate etc ...

that is Bluetooth device is continuously sends data to app so the app should continuously receive data

Upvotes: 2

Views: 1611

Answers (2)

Abhinav Singh Maurya
Abhinav Singh Maurya

Reputation: 3313

Answers to your questions are as follows

Is there any way because i dont exactly know wat kind of data it is sending means in which formate etc ...

Yes there are 2 ways you can do it

1) use intent

 Intent i = new Intent(Intent.ACTION_SEND);
 i.setType("image/jpeg");
 i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/sdcard/file.jpg")); startActivity(Intent.createChooser(i, "Send Image"));

code sample to send data using inbuild bluetooth

2) Use open source code which use socket connection and bluetooth manager to do it via code. following is the link of open source code

blueterm opensource code

Data sent over bluetooth is in form of byte array which you have to parse and convert it to string or any form that you want to use in your code.

There are two things in bluetooth

1) pairing.

2) global connection.

If you use first method of bluetooth (via intent) device will be paired but connection will not be continuous. while sending data connection is established else connection is not there. But if you are using blueterm open source code you pairing and connection is constant and will not break untill user wants.

So its upto you which kind of bluetooth functionality you want.

Let me know if my answer solves your problem

Upvotes: 0

urveshpatel50
urveshpatel50

Reputation: 1685

use listenUsingRfcommWithServiceRecord instead of this code in service

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class} );
            btSocket = (BluetoothSocket) m.invoke(device, 1);  

Upvotes: 3

Related Questions