Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing an android application.I need to get data from meter by optical probe via bluetooth. As I know I should connect to probe with bluetooth and send some request to that for getting the meter values. Please if anybody knows about how to do it .. help me. I used BluetoothSocket,Outputstream and inputstream but no luck!

What I have tried:

bluetoothSocket = device.createRfcommSocketToServiceRecord(myUUID);

inputStearm = bluetoothSocket .getInputStream();
             outputStream = bluetoothSocket .getOutputStream();
Posted
Updated 16-Oct-17 23:02pm

1 solution

First of all, did you update the manifest with the right permissions?
The following permissions needed to be included:
<manifest ... >
  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  ...
</manifest>

Before continuing to the next phase, make sure the device is already paired up.
You can simply check the paired state with:
if(device.getBondState()==device.BOND_BONDED)

Get the correct UUID(example):
private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");

You must be able to get a socket at this point.
mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

Read the socket:
mSocket.connect();
InputStream input = mSocket.getInputStream();
DataInputStream dinput = new DataInputStream(input);

You can read it later on with:
dinput.readFully(byteArray, 0, byteArray.length);
 
Share this answer
 
Comments
marimir 17-Oct-17 5:30am    
Thank you for the answer.. I've tested this code before but the app is actually stopping at the line:
DataInputStream dinput = new DataInputStream(input);
Wessel Beulink 17-Oct-17 5:37am    
What is the input result, you are able to debug the result?
marimir 17-Oct-17 5:41am    
no the app stopped at the line and I cant get the data from inputstream at all!
Wessel Beulink 17-Oct-17 5:45am    
Put a try and catch around it, try to get the exception.
Does logcat not shows a crash report?
marimir 17-Oct-17 5:54am    
I've put the try catch but it is just like some long process. no crash is shown in logcat and after a while the app is stopped

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900