NullPointer Exception on socket.connect() Galaxy Tab 2 running Android 4.04
Asked Answered
P

2

3

I seem to be facing this weird error on a socket.connect():

09-18 14:41:22.968: W/System.err(2593): java.lang.NullPointerException
09-18 14:41:22.968: W/System.err(2593):     at android.sec.enterprise.BluetoothUtils.**isSocketAllowedBySecurityPolicy**(BluetoothUtils.java:106)
09-18 14:41:22.968: W/System.err(2593):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:220)
09-18 14:41:22.968: W/System.err(2593):     at com._._.android._._.bluetoothmodule.BluetoothController.connectToDevice(BluetoothController.java:136)
09-18 14:41:22.976: W/System.err(2593):     at com._._.android._._.controllermodule.ControllerModule.connectToDevice(ControllerModule.java:235)
09-18 14:41:22.976: W/System.err(2593):     at com._._.android._._.controllermodule.ControllerModule.connectToDevice(ControllerModule.java:263)
09-18 14:41:22.976: W/System.err(2593):     at com._._.android._._.service.ConnectionService.onHandleIntent(ConnectionService.java:70)
09-18 14:41:22.976: W/System.err(2593):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
09-18 14:41:22.976: W/System.err(2593):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 14:41:22.976: W/System.err(2593):     at android.os.Looper.loop(Looper.java:137)
09-18 14:41:22.976: W/System.err(2593):     at android.os.HandlerThread.run(HandlerThread.java:60)

Yes, I am checking if the socket is null before connecting to it. I seem to be only facing this issue on the Galaxy Tab 2 running 4.0.4. My code works fine on other devices/android versions [including JB]. Not sure what might be going on here. Listed below is a small chunk demonstrating how i initialize my socket:

Method m = bluetoothDevice.getClass().getMethod(
            "createInsecureRfcommSocket", new Class[] { int.class });
Logging.getInstance().logMessage(this.getClass().getSimpleName(), "Returned the Reflection method successfully..",Logging.LOG_ERROR);
      // get readSocket
    mreadSocket = (BluetoothSocket) m.invoke(bluetoothDevice, 1);
      assert (mreadSocket != null) : "readSocket is Null";
 if (mreadSocket != null) {

        mreadSocket.connect();
}

Any help would be greatly appreciated!

Prescott answered 18/9, 2012 at 22:7 Comment(4)
Your app has all the bluetooth permissions it needs?Sonni
yes, it has android.permissions.INTERNET [for creation of sockets] as well as android.permissions.BLUETOOTH & BLUETOOTH_ADMINPrescott
no solution yet, but I believe I'm getting similar behaviour on my Galaxy Tab 2 running 4.0.3, while my code runs successfully on HTC One X and Samsung Nexus. Will test on Galaxy S3 when it comes back into the office, I suspect this doesn't work either.Serai
Tested on Galaxy S3 running 4.0.4, same exception is thrown.Aviary
N
1

Warning: the code below may be insecure, use at your own risk

In my case I was able to connect using createInsecureRfcommSocketToServiceRecord rather than createRfcommSocketToServiceRecord but I see you were already doing that. My code looks more like this (error/exception checking removed):

BluetoothDevice device;
String deviceName = ... selected or hardcoded device name. See Android HDR sample code
BluetoothDevice[] mAllBondedDevices = (BluetoothDevice[]) mBluetoothAdapter.getBondedDevices().toArray(new BluetoothDevice[0]);

for (BluetoothDevice d : mAllBondedDevices) {
  if (deviceName.equals(d.getName())) {
    device = d;
    break;
  }
}

UUID uuid = device.getUuids()[0].getUuid();
//FAILED: socket = device.createRfcommSocketToServiceRecord(uuid);
// Succeeds: Warning, INSECURE!
socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
socket.connect();
this.dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
this.dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//etc 

Note that while an insecure connection is not perfect, in our case an insecure connection is preferable to no connection. I posted this an an answer so that you could try the alternate code.

Nephograph answered 20/9, 2012 at 4:31 Comment(3)
note that the secure connection worked on a HTC One X and Samsung Nexus, but not with the Samsung Galaxy Tab 2 (7.0)Serai
Thanks for this David. I tried to get the UUID using: #11003780 .Then used createInsecureRfcommSocketToServiceRecord. However I get a null list from the Galaxy tab 2. Note: this was after a successful scan and device detection.Prescott
The NULL list issue seems to be due to the other device I am using. So your solution is the correct one. I'm curious as to why Samsung forces us to use these methods? Seems rather annoying from a DEV perspective. Especially since we use our own encryption and such [make the insecure system secure]Prescott
I
0

I had the same problem with the Galaxy tab 2 and I solved with:

        BluetoothSocket tmp = null;
        try {
            tmp = device.createRfcommSocketToServiceRecord(SPP_UUID);

            // for others devices its works with:
            // Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

            // for galaxy tab 2 with:
            Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});

            tmp = (BluetoothSocket) m.invoke(device, 1);
        } catch (IOException e) {
            Log.e(TAG, "failed! error: " + e.getMessage());
        }
        socket = tmp;

        socket.connect();
        Log.i(TAG, "Client Connected!");

Hope this helps =D

Incoercible answered 14/6, 2013 at 0:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.