在socket.connect空指针异常()的Galaxy Tab 2运行Android 4.04 [英] NullPointer Exception on socket.connect() Galaxy Tab 2 running Android 4.04

查看:178
本文介绍了在socket.connect空指针异常()的Galaxy Tab 2运行Android 4.04的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在socket.connect要面对这个奇怪的错误():

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)

是的,我检查,如果套接字连接之前空。我似乎只有面对这个问题的的Galaxy Tab 2运行4.0.4 。我的code正常工作中的其他设备/ Android的版本[包括JB。不知道什么可能会在这里继续。下面列出的是一小块演示如何初始化我的插座:

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();
}

任何帮助将是很大的AP preciated!

Any help would be greatly appreciated!

推荐答案

警告:code以下可能是不安全的,使用风险自负

在我来说,我是能够使用连接 createInsecureRfcommSocketToServiceRecord ,而不是 createRfcommSocketToServiceRecord ,但我看你已经这样做。我的code看起来更像这个(错误/异常检查删除):

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 

请注意,虽然不安全的连接是不完美的,在我们的例子不安全的​​连接是preferable没有连接。我张贴这是一个答案,这样你可以尝试备用code。

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.

这篇关于在socket.connect空指针异常()的Galaxy Tab 2运行Android 4.04的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆