我似乎在 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。我的代码在其他设备/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();
}
任何帮助将不胜感激!
警告:以下代码可能不安全,使用风险自负
就我而言,我能够使用连接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
请注意,虽然不安全的连接并不完美,但在我们的例子中,不安全的连接比没有连接更好。我发布了这个答案,以便您可以尝试替代代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)