我似乎在 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)
是的,我在连接之前检查套接字是否为空。我似乎只在运行 4.0.4 的 Galaxy Tab 2 上遇到这个问题。我的代码在其他设备/android 版本 [包括 JB] 上运行良好。不知道这里可能发生了什么。下面列出的是一小块演示我如何初始化我的套接字:
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();
}
任何帮助将不胜感激!