NullPointer Exception on socket.connect() Galaxy Tab 2 running Android 4.04

后端 未结 2 714
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 10:02

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/Syst         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 10:20

    I had the same problem with the Galaxy tab 2 and I solved with:

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

    Hope this helps =D

提交回复
热议问题