I need to write some kind of client-server application using bluetooth. I need to create some server on my laptop (I use java and bluecove) which will be send some data to application (on android). Here is my code:
server:
public class OBEXPutServer {
static final String serverUUID = "11111111111111111111111111111123";
public static void main(String[] args) throws IOException {
LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
SessionNotifier serverConnection = (SessionNotifier) Connector.open("btgoep://localhost:"
+ serverUUID + ";name=ObexExample");
int count = 0;
while(count < 2) {
RequestHandler handler = new RequestHandler();
serverConnection.acceptAndOpen(handler);
System.out.println("Received OBEX connection " + (++count));
}
}
private static class RequestHandler extends ServerRequestHandler {
public int onPut(Operation op) {
try {
HeaderSet hs = op.getReceivedHeaders();
String name = (String) hs.getHeader(HeaderSet.NAME);
if (name != null) {
System.out.println("put name:" + name);
}
return ResponseCodes.OBEX_HTTP_OK;
} catch (IOException e) {
e.printStackTrace();
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
}
}
}
}
application:
public class BTTestActivity extends Activity {
String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
static final String serverUUID = "11111111111111111111111111111123";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BroadcastReceiver discoveryResult = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
BluetoothDevice remoteDevice;
remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("@#$%^&*(*&^%$#@#$%^&*(", "WYSWIETLAM");
Toast.makeText(getApplicationContext(), "Discovered: " + remoteDeviceName + " address " + remoteDevice.getAddress(), Toast.LENGTH_SHORT).show();
try{
BluetoothDevice device = bluetooth.getRemoteDevice(remoteDevice.getAddress());
BluetoothSocket clientSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(serverUUID));
clientSocket.connect();
} catch (IOException e) {
Log.d("BLUETOOTH", e.getMessage());
}
}
};
registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
public void fun(View view)
{
bluetooth.enable();
if (!bluetooth.isDiscovering())
bluetooth.startDiscovery();
}
}
My problem is that when I try to connect to service from application I get something like this:
11-26 13:41:04.959: E/AndroidRuntime(8830): FATAL EXCEPTION: main
11-26 13:41:04.959: E/AndroidRuntime(8830): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND (has extras) } in andr.andr.BTTestActivity$1@40513e00
11-26 13:41:04.959: E/AndroidRuntime(8830): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
11-26 13:41:04.959: E/AndroidRuntime(8830): at android.os.Handler.handleCallback(Handler.java:587)
11-26 13:41:04.959: E/AndroidRuntime(8830): at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 13:41:04.959: E/AndroidRuntime(8830): at android.os.Looper.loop(Looper.java:130)
11-26 13:41:04.959: E/AndroidRuntime(8830): at android.app.ActivityThread.main(ActivityThread.java:3835)
11-26 13:41:04.959: E/AndroidRuntime(8830): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 13:41:04.959: E/AndroidRuntime(8830): at java.lang.reflect.Method.invoke(Method.java:507)
11-26 13:41:04.959: E/AndroidRuntime(8830): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
11-26 13:41:04.959: E/AndroidRuntime(8830): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
11-26 13:41:04.959: E/AndroidRuntime(8830): at dalvik.system.NativeStart.main(Native Method)
11-26 13:41:04.959: E/AndroidRuntime(8830): Caused by: java.lang.IllegalArgumentException: Invalid UUID: 11111111111111111111111111111133
11-26 13:41:04.959: E/AndroidRuntime(8830): at java.util.UUID.fromString(UUID.java:226)
11-26 13:41:04.959: E/AndroidRuntime(8830): at andr.andr.BTTestActivity$1.onReceive(BTTestActivity.java:44)
11-26 13:41:04.959: E/AndroidRuntime(8830): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
11-26 13:41:04.959: E/AndroidRuntime(8830): ... 9 more
Do anyone know what is wrong with this code? Thanks for any help.
EDIT:
I’ll run this example what you show to me and I run it but its not work 🙁 I have problem wyth server. He is stops on “StreamConnection streamConnection = connectionNotifier.acceptAndOpen();” line in start method. And hare is log from your android app:
11-26 16:08:32.569: D/MyActivity(2725): ON CREATE
11-26 16:08:32.569: D/MyActivity(2725): ON START
11-26 16:08:32.569: D/MyActivity(2725): INSIDE WHILE STATE ON
11-26 16:08:32.589: D/MyActivity(2725): Start discovery = true
11-26 16:08:32.589: D/MyActivity(2725): INSIDE WHILE IS DISCOVERING
11-26 16:08:32.589: D/MyActivity(2725): INSIDE WHILE IS DISCOVERING
11-26 16:08:32.589: D/MyActivity(2725): INSIDE WHILE IS DISCOVERING
11-26 16:08:32.589: D/MyActivity(2725): INSIDE WHILE IS DISCOVERING
11-26 16:08:32.589: D/MyActivity(2725): INSIDE WHILE IS DISCOVERING
11-26 16:08:32.589: D/MyActivity(2725): INSIDE WHILE IS DISCOVERING
11-26 16:08:32.589: D/MyActivity(2725): INSIDE WHILE IS DISCOVERING
11-26 16:08:32.619: D/MyActivity(2725): about to connect
11-26 16:18:18.529: D/MyActivity(3166): Connected!
and when I click button I get in log
11-26 16:19:02.389: D/MyActivity(3166): In listener button.
In your app code I change bluetooth adress to my bluetooth device on laptop. Do you know what is wrong, maybe I need to do something else with your code? And I have a question. Where in yout app code u set UUID of server?
I notice that the log from your app is the same even if I dont run server. Its looks like that the appliaction see only bluetooth adapter on my laptop and dont see server application running on my laptop.
BTW. I use BlueSoleil 6.4.149.0 as my bluetooth stack couse my “Toshiba for windows” stack doesnt work with bluecove. Maybe here is problem?
Try looking at my answer: Why am I losing bluetooth client/server connection?
You should try using reflection when invoking
createRfcommSocket:I already did a server/client bluetooth connection, and it worked for me. I had similar problems and it turned out to be okay when using reflection.