在运行Android 4.04的Galaxy Tab 2上,socket.connect()出现了NullPointer Exception。

3

我似乎在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();
}

非常感谢您的帮助!


1
你的应用程序是否拥有所需的所有蓝牙权限? - Tim Roes
是的,它具有android.permissions.INTERNET权限[用于创建套接字],以及android.permissions.BLUETOOTH和BLUETOOTH_ADMIN权限。 - bge0
目前还没有解决方案,但我相信我的 Galaxy Tab 2 运行着 4.0.3 版本时会出现类似的问题,而我的代码在 HTC One X 和 Samsung Nexus 上成功运行。当 Galaxy S3 回到办公室时,我会进行测试,我怀疑这也无法正常工作。 - David O'Meara
在运行4.0.4版本的Galaxy S3上进行了测试,仍然抛出相同的异常。 - Mani
2个回答

1

警告:以下代码可能存在安全隐患,请自行承担风险

在我的情况下,我能够使用createInsecureRfcommSocketToServiceRecord而不是createRfcommSocketToServiceRecord进行连接,但我看到您已经这样做了。我的代码看起来更像这样(已删除错误/异常检查):

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 

请注意,虽然不安全的连接并不完美,但在我们的情况下,不安全的连接比没有连接更好。我将其发布为答案,以便您可以尝试替代代码。

请注意,安全连接在HTC One X和三星Nexus上运行正常,但在三星Galaxy Tab 2(7.0)上无法使用。 - David O'Meara
谢谢你的回复,David。我尝试使用以下方法获取UUID:http://stackoverflow.com/questions/11003280/finding-uuids-in-android-2-0。然后使用createInsecureRfcommSocketToServiceRecord。但是,从Galaxy Tab 2中获得了一个空列表。注意:这是在成功扫描和检测设备之后发生的。 - bge0
NULL列表问题似乎是由我使用的其他设备引起的。因此,您的解决方案是正确的。我很好奇为什么三星强制我们使用这些方法?从开发人员的角度来看,这似乎相当烦人。特别是因为我们使用自己的加密等[使不安全的系统变得安全]。 - bge0

0

我在使用Galaxy Tab 2时也遇到了同样的问题,但我通过以下方法解决了:

        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!");

希望这有所帮助 =D

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接