Android Bluetooth socket freeze application

后端 未结 6 2378
南旧
南旧 2021-02-14 23:46

I have a strange issue with bluetooth socket. If I create socket and later close application, android device freeze with very hight CPU load.

Here my sample code:

<
6条回答
  •  北荒
    北荒 (楼主)
    2021-02-15 00:17

    Did the socket get successfully created before calling the Close() method ? I would try initializing the socket to null before the call to createRfcommSocketToServiceRecord(UUID) The Bluetooth Chat example does this .. here is a snippet .

    public ConnectThread(BluetoothDevice device) {
            mmDevice = device;
            BluetoothSocket tmp = null;
    
            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try { 
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "create() failed", e);
            }
            mmSocket = tmp;
        }
    
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    

提交回复
热议问题