1

I want to connect to this device: http://www.rhydolabz.com/index.php?main_page=product_info&products_id=479

I tried using the BluetoothChat class from the samples. But it did not work. It says unable to connect device.

Then I tried to use the following code within try catch

        // Create a Socket connection: need the server's UUID number
Method m = d.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(d, 1);
socket.connect();
Log.d("WCAM", ">>Client connectted");
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
socket.close();
Toast.makeText(this, "Bluetooth is Connected", Toast.LENGTH_LONG).show();

and I get the following error

04-18 20:45:01.660: W/System.err(6780): java.lang.NoSuchMethodException: createRfcommSocketToServiceRecord [int]
04-18 20:45:01.660: W/System.err(6780):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-18 20:45:01.660: W/System.err(6780):     at java.lang.Class.getMethod(Class.java:915)
04-18 20:45:01.660: W/System.err(6780):     at com.xpleria.wirelesscontroller.Login.onStart(Login.java:90)
04-18 20:45:01.660: W/System.err(6780):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
04-18 20:45:01.670: W/System.err(6780):     at android.app.Activity.performStart(Activity.java:4475)
04-18 20:45:01.670: W/System.err(6780):     at android.app.Activity.performRestart(Activity.java:4526)
04-18 20:45:01.670: W/System.err(6780):     at android.app.Activity.performResume(Activity.java:4531)
04-18 20:45:01.670: W/System.err(6780):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
04-18 20:45:01.670: W/System.err(6780):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
04-18 20:45:01.670: W/System.err(6780):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1173)
04-18 20:45:01.670: W/System.err(6780):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 20:45:01.680: W/System.err(6780):     at android.os.Looper.loop(Looper.java:137)
04-18 20:45:01.680: W/System.err(6780):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-18 20:45:01.680: W/System.err(6780):     at java.lang.reflect.Method.invokeNative(Native Method)
04-18 20:45:01.680: W/System.err(6780):     at java.lang.reflect.Method.invoke(Method.java:511)
04-18 20:45:01.680: W/System.err(6780):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
04-18 20:45:01.680: W/System.err(6780):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-18 20:45:01.680: W/System.err(6780):     at dalvik.system.NativeStart.main(Native Method)

Any way how I can modify my code..?

Xpleria
  • 5,472
  • 5
  • 52
  • 66

1 Answers1

6

Couple of notes:

a) It's not obvious what is the class of variable d. I assume it's BluetoothDevice

b) If my assumption from 1) is correct, it can't find a method createRfcommSocketToServiceRecord(int), because actualy method is createRfcommSocketToServiceRecord(UUID).

c) It's not clear to me, why do you use reflection to get to this method. You can call it on your object directly.

d) There was a piece documentation which says: "Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID.". I think it could be applicable to you.

Update 1

Regarding using directly.

You code should be

 d.createRfcommSocketToServiceRecord(someUUID);

since createRfcommSocketToServiceRecord is a method of BluetoothDevice.

Regarding UUID.Take a look at this:

Android: obtaining uuid of a bluetooth device

Android: How do bluetooth UUIDs work?

Community
  • 1
  • 1
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
  • yes `d` is a BluetoothDevice. I'm not sure what this UUID means. What should I use for my device? And actually I pulled out parts of code from other projects. That's how it was in them. If I have to use it directly, how do I use it? – Xpleria Apr 18 '13 at 15:40
  • if I use `socket = createRfcommSocketToServiceRecord(MY_UUID);` directly, it gives me an error saying, `The method createRfcommSocketToServiceRecord(UUID) is undefined for the type Login Login.java` – Xpleria Apr 18 '13 at 15:51
  • using this throws `java.io.IOException: read failed, socket might closed or timeout, read ret: -1` – ChumiestBucket Sep 28 '18 at 16:50