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!
I had the same problem with the Galaxy tab 2 and I solved with:
Hope this helps =D
Warning: the code below may be insecure, use at your own risk
In my case I was able to connect using
createInsecureRfcommSocketToServiceRecord
rather thancreateRfcommSocketToServiceRecord
but I see you were already doing that. My code looks more like this (error/exception checking removed):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.