【问题标题】:NullPointer Exception on socket.connect() Galaxy Tab 2 running Android 4.04运行 Android 4.04 的 socket.connect() Galaxy Tab 2 上的 NullPointer 异常
【发布时间】:2012-09-18 22:07:00
【问题描述】:

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

任何帮助将不胜感激!

【问题讨论】:

  • 您的应用拥有所需的所有蓝牙权限?
  • 是的,它有 android.permissions.INTERNET [用于创建套接字] 以及 android.permissions.BLUETOOTH & BLUETOOTH_ADMIN
  • 还没有解决方案,但我相信我在运行 4.0.3 的 Galaxy Tab 2 上会遇到类似的行为,而我的代码在 HTC One X 和 Samsung Nexus 上成功运行。回到办公室时将在 Galaxy S3 上进行测试,我怀疑这也不起作用。
  • 在运行 4.0.4 的 Galaxy S3 上测试,抛出相同的异常。

标签: android samsung-mobile galaxy-tab


【解决方案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)
  • 感谢大卫。我尝试使用:stackoverflow.com/questions/11003280/… 获取 UUID。然后使用 createInsecureRfcommSocketToServiceRecord。但是,我从 Galaxy 选项卡 2 中获得了一个空列表。注意:这是在成功扫描和设备检测之后。
  • NULL 列表问题似乎是由于我正在使用的其他设备。所以你的解决方案是正确的。我很好奇为什么三星强迫我们使用这些方法?从 DEV 的角度来看,这似乎很烦人。特别是因为我们使用自己的加密等[使不安全的系统安全]
【解决方案2】:

我在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多