【问题标题】:Android insecure bluetooth connectionAndroid 不安全的蓝牙连接
【发布时间】:2015-05-02 17:26:45
【问题描述】:

我想将 android mobile 连接到一个电子设备,但我想不安全地连接它。但是它给了我错误“java.io.IOException:读取失败,套接字可能关闭或超时,读取 ret:-1 "

这是我的代码

包 net.londatiga.android.bluetooth;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

public class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;

    private final UUID WELL_KNOWN_UUID = UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB");

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,because
        // mmSocket is final
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // tmp = device.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
            tmp = device
                    .createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
            Object[] params = new Object[] {Integer.valueOf(1)};
            // This is the trick
            Method m = device.getClass().getMethod("createInsecureRfcommSocket",
                    new Class[] { int.class });
            tmp = (BluetoothSocket) m.invoke(device, params);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mmSocket = tmp;
    }

    @SuppressLint("NewApi")
    public void run() {
        // DebugLog.i(TAG, "Trying to connect...");
        // Cancel discovery because it will slow down the connection
        MainActivity.mBluetoothAdapter.cancelDiscovery();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();

            boolean val=mmSocket.isConnected();
            Log.v("val", ""+val);
            // DebugLog.i(TAG, "Connection stablished");
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            // DebugLog.e(TAG, "Fail to connect!", connectException);
            try {
                mmSocket.close();
            } catch (IOException closeException) {
                // DebugLog.e(TAG, "Fail to close connection", closeException);
            }
            return;
        }
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
        }
    }
}

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    您必须实现另一个线程,如下所示,它始终接收您的调用并执行操作。启动应用程序时启动此线程。

    class AcceptThread extends
     Thread {
        private final BluetoothServerSocket serverSocket;
        private boolean flag = true;
    
        public AcceptThread() {
            BluetoothServerSocket tmp = null;
    
            try {
                tmp = bluetoothAdapter
                        .listenUsingInsecureRfcommWithServiceRecord(
                                NAME_UUID, uuid);
    
            } catch (IOException e) {
            }
            serverSocket = tmp;
        }
    
        public void run() {
            BluetoothSocket socket = null;
            while (true) {
                try {
                    socket = serverSocket.accept();
                    if (socket.isConnected()) {
                        Log.d(TAG, "run:  connection successfull");
                        Log.d(TAG, "run: " +     socket.getRemoteDevice().getName() + "  " +
                                socket.getRemoteDevice().getAddress());
                    }
    
                } catch (IOException e) {
    
                    try {
                        socket.close();
                    } catch (Exception eee) {
                        Log.d(TAG, "run: " + eee.getMessage());
                    }
                    break;
                }
            }
        }
    
        public void cancel() {
            try {
                serverSocket.close();
            } catch (IOException e) {
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 2014-02-18
      相关资源
      最近更新 更多