java类android.bluetooth.BluetoothSocket的实例源码

BluetoothChatService.java 文件源码 项目:bluetooth-chat-appliction 阅读 44 收藏 0 点赞 0 评论 0
public ConnectedThread(BluetoothSocket socket, String socketType) {
    Log.d(TAG, "create ConnectedThread: " + socketType);
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the BluetoothSocket input and output streams
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
    mState = STATE_CONNECTED;
}
BluetoothUtil.java 文件源码 项目:BluetoothAPP 阅读 47 收藏 0 点赞 0 评论 0
/**
     * 连接蓝牙(2)
     *
     * @return
     */
    public static BluetoothSocket connectDevice(final Handler handler) {
        BluetoothSocket socket = null;
        try {
            Comment.SPP_UUID = UUID.fromString("00001106-0000-1000-8000-00805F9B34FB");
            socket = Comment.bluetoothDevice.createRfcommSocketToServiceRecord(Comment.SPP_UUID);
            socket.connect();
            handler.sendEmptyMessage(Comment.CONNECT);
        } catch (Exception e) {
            handler.sendEmptyMessage(2);
            try {
                if (socket!=null)
                socket.close();
            } catch (Exception closeException) {

            }
        }
        return socket;
}
BluetoothIOGateway.java 文件源码 项目:ELM327 阅读 35 收藏 0 点赞 0 评论 0
public ConnectThread(BluetoothDevice device, boolean secure)
{
    mmDevice = device;
    BluetoothSocket tmp = null;
    mSocketType = secure ? "Secure" : "Insecure";

    // Get a BluetoothSocket for a connection with the given BluetoothDevice
    try
    {
        if (secure)
        {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
        }
        else
        {
            tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
        }
    }
    catch (IOException e)
    {
        MyLog.e(TAG, "Socket Type: " + mSocketType + " create() failed", e);
    }

    mmSocket = tmp;
}
BluetoothChatService.java 文件源码 项目:dab-iot 阅读 35 收藏 0 点赞 0 评论 0
public ConnectedThread(BluetoothSocket socket, String socketType) {
    Log.d(TAG, "create ConnectedThread: " + socketType);
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the BluetoothSocket input and output streams
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
    mState = STATE_CONNECTED;
}
BluetoothListener.java 文件源码 项目:VR-One 阅读 29 收藏 0 点赞 0 评论 0
public ConnectThread(BluetoothDevice device, boolean secure) {
    mmDevice = device;
    BluetoothSocket tmp = null;
    mSocketType = secure ? "Secure" : "Insecure";

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        if (secure) {
            tmp = device.createRfcommSocketToServiceRecord(
                    MY_UUID_SECURE);
        } else {
            tmp = device.createInsecureRfcommSocketToServiceRecord(
                    MY_UUID_INSECURE);
        }
    } catch (IOException e) {
        Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
    }
    mmSocket = tmp;
    mState = STATE_CONNECTING;
}
BluetoothListener.java 文件源码 项目:VR-One 阅读 32 收藏 0 点赞 0 评论 0
public ConnectedThread(BluetoothSocket socket, String socketType) {
    Log.d(TAG, "create ConnectedThread: " + socketType);
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the BluetoothSocket input and output streams
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
    mState = STATE_CONNECTED;
}
BTAdt.java 文件源码 项目:AndroidSDK2.0 阅读 35 收藏 0 点赞 0 评论 0
/**
 * Bind boolean.
 *
 * @param socket the socket
 * @return the boolean
 */
public boolean bind( BluetoothSocket socket )
{

    mmSocket = socket;
    macAddress = mmSocket.getRemoteDevice().getAddress();

    // Get the BluetoothSocket input and output streams
    try
    {
        mmInStream = socket.getInputStream();
        mmOutStream = socket.getOutputStream();

        this.isRunning = true;
        // 펜 연결 후 맨 처음 펜정보를 요청한다.(프로토콜 2.0)
        startConnect();
        return true;
    }
    catch ( IOException e )
    {
        NLog.e( "[BTAdt/ConnectedThread] temporary sockets is not created", e );
    }

    return false;
}
BluetoothPresenterControl.java 文件源码 项目:Presenter-Client-Android 阅读 40 收藏 0 点赞 0 评论 0
/**
 * Creates the connection thread that will connect to a given device.
 *
 * @param device The device to connect to
 */
ConnectThread(BluetoothDevice device) {
    mmDevice = device;
    BluetoothSocket tmp = null;

    // Get a BluetoothSocket for a connection with the given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(SERVICE_UUID);
    } catch (IOException e) {
        Log.e(TAG, "create socket failed", e);
    }
    mmSocket = tmp;
    mState = ServiceState.CONNECTING;

    // Notifiy the user that we are now connecting
    android.os.Message userNotification
            = mHandler.obtainMessage(ServiceState.CONNECTING.ordinal());
    mHandler.sendMessage(userNotification);
}
BluetoothChatService.java 文件源码 项目:SmartRC 阅读 44 收藏 0 点赞 0 评论 0
public ConnectThread(BluetoothDevice device, boolean secure) {
    mmDevice = device;
    BluetoothSocket tmp = null;
    mSocketType = secure ? "Secure" : "Insecure";

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        if (secure) {
            tmp = device.createRfcommSocketToServiceRecord(
                    MY_UUID_SECURE);
        } else {
            tmp = device.createInsecureRfcommSocketToServiceRecord(
                    MY_UUID_INSECURE);
        }
    } catch (IOException e) {
        Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
    }
    mmSocket = tmp;
}
BluetoothPresenterControl.java 文件源码 项目:Presenter-Client-Android 阅读 35 收藏 0 点赞 0 评论 0
/**
 * Start the ConnectedThread to begin managing a Bluetooth connection
 *
 * @param socket The BluetoothSocket on which the connection was made
 * @param device The BluetoothDevice that has been connected
 */
private synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
    // Cancel the thread that completed the connection
    if (mConnectThread != null) {
        mConnectThread.cancel();
        mConnectThread = null;
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket, this, device);
    mConnectedThread.start();
}
DroidtoothPlugin.java 文件源码 项目:Nird2 阅读 35 收藏 0 点赞 0 评论 0
@Override
public DuplexTransportConnection createKeyAgreementConnection(
        byte[] commitment, BdfList descriptor, long timeout) {
    if (!isRunning()) return null;
    String address;
    try {
        address = parseAddress(descriptor);
    } catch (FormatException e) {
        LOG.info("Invalid address in key agreement descriptor");
        return null;
    }
    // No truncation necessary because COMMIT_LENGTH = 16
    UUID uuid = UUID.nameUUIDFromBytes(commitment);
    if (LOG.isLoggable(INFO))
        LOG.info("Connecting to key agreement UUID " + uuid);
    BluetoothSocket s = connect(address, uuid.toString());
    if (s == null) return null;
    return new DroidtoothTransportConnection(this, s);
}
DroidtoothPlugin.java 文件源码 项目:Nird2 阅读 33 收藏 0 点赞 0 评论 0
private void acceptContactConnections() {
    while (isRunning()) {
        BluetoothSocket s;
        try {
            s = socket.accept();
        } catch (IOException e) {
            // This is expected when the socket is closed
            if (LOG.isLoggable(INFO)) LOG.info(e.toString());
            return;
        }
        if (LOG.isLoggable(INFO)) {
            String address = s.getRemoteDevice().getAddress();
            LOG.info("Connection from " + scrubMacAddress(address));
        }
        backoff.reset();
        callback.incomingConnectionCreated(wrapSocket(s));
    }
}
DroidtoothPlugin.java 文件源码 项目:Nird2 阅读 32 收藏 0 点赞 0 评论 0
@Override
public DuplexTransportConnection createKeyAgreementConnection(
        byte[] commitment, BdfList descriptor, long timeout) {
    if (!isRunning()) return null;
    String address;
    try {
        address = parseAddress(descriptor);
    } catch (FormatException e) {
        LOG.info("Invalid address in key agreement descriptor");
        return null;
    }
    // No truncation necessary because COMMIT_LENGTH = 16
    UUID uuid = UUID.nameUUIDFromBytes(commitment);
    if (LOG.isLoggable(INFO))
        LOG.info("Connecting to key agreement UUID " + uuid);
    BluetoothSocket s = connect(address, uuid.toString());
    if (s == null) return null;
    return new DroidtoothTransportConnection(this, s);
}
DroidtoothPlugin.java 文件源码 项目:Nird2 阅读 39 收藏 0 点赞 0 评论 0
@Override
public BluetoothSocket call() throws Exception {
    // Repeat discovery until we connect or get interrupted
    while (true) {
        // Discover nearby devices
        LOG.info("Discovering nearby devices");
        List<String> addresses = discoverDevices();
        if (addresses.isEmpty()) {
            LOG.info("No devices discovered");
            continue;
        }
        // Connect to any device with the right UUID
        for (String address : addresses) {
            BluetoothSocket s = connect(address, uuid);
            if (s != null) {
                LOG.info("Outgoing connection");
                return s;
            }
        }
    }
}
BluetoothActivity.java 文件源码 项目:Snach-Android 阅读 48 收藏 0 点赞 0 评论 0
public ConnectThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    BluetoothSocket tmp = null;
    mmDevice = device;

    /*try {
        tmp = createBluetoothSocket(device);
    } catch (IOException e1) {
        e1.printStackTrace();
    }*/

    try {
        tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) { e.printStackTrace(); }

    /*try {
        tmp = createBluetoothSocket(device);
    } catch (IOException e) {
        e.printStackTrace();
    }*/

    mmSocket = tmp;
}
PBluetoothServer.java 文件源码 项目:phonk 阅读 36 收藏 0 点赞 0 评论 0
private void connectToClient(final BluetoothSocket btSocketClient) throws IOException {
    MLog.d(TAG, "bbt connection to device: " + btSocketClient.getRemoteDevice().getName());

    final ConnectedDevice connectedDevice = new ConnectedDevice(btSocketClient);
    mServerConnections.add(connectedDevice);

    // callback
    if (mCallbackOnNewConnection != null) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                ReturnObject ret = new ReturnObject();
                ret.put("device", connectedDevice);
                ret.put("name", btSocketClient.getRemoteDevice().getName());
                ret.put("mac", btSocketClient.getRemoteDevice().getAddress());
                mCallbackOnNewConnection.event(ret);
            }
        });

    }

    connectedDevice.startThread();
}
BluetoothChatService.java 文件源码 项目:SecretTalk 阅读 37 收藏 0 点赞 0 评论 0
public ConnectThread(BluetoothDevice device, boolean secure) {
    mmDevice = device;
    BluetoothSocket tmp = null;
    mSocketType = secure ? "Secure" : "Insecure";

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        if (secure) {
            tmp = device.createRfcommSocketToServiceRecord(
                    MY_UUID_SECURE);
        } else {
            tmp = device.createInsecureRfcommSocketToServiceRecord(
                    MY_UUID_INSECURE);
        }
    } catch (IOException e) {
        Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
    }
    mmSocket = tmp;
    mState = STATE_CONNECTING;
}
SerialThreadService.java 文件源码 项目:grblcontroller 阅读 33 收藏 0 点赞 0 评论 0
public ConnectThread(BluetoothDevice device, boolean secure) {
    mmDevice = device;
    BluetoothSocket tmp = null;
    mSocketType = secure ? "Secure" : "Insecure";

    try {
        if (secure) {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
        } else {
            tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
        }
    } catch (IOException | NullPointerException e) {
        Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
    }
    mmSocket = tmp;
    mState = STATE_CONNECTING;
}
SerialThreadService.java 文件源码 项目:grblcontroller 阅读 32 收藏 0 点赞 0 评论 0
public ConnectedThread(BluetoothSocket socket, String socketType) {
    Log.d(TAG, "create ConnectedThread: " + socketType);
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the BluetoothSocket input and output streams
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException | NullPointerException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
    mState = STATE_CONNECTED;
}
BluetoothService.java 文件源码 项目:Lazy-Switches 阅读 46 收藏 0 点赞 0 评论 0
public ConnectThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    BluetoothSocket tmp = null;
    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        UUID uuid = Constants.myUUID;
        tmp = device.createRfcommSocketToServiceRecord(uuid);
    } catch (IOException e) {
        Log.e(Constants.TAG, "Create RFcomm socket failed", e);
    }
    mmSocket = tmp;
}
BluetoothService.java 文件源码 项目:Lazy-Switches 阅读 45 收藏 0 点赞 0 评论 0
public ConnectedThread(BluetoothSocket socket) {
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e(Constants.TAG, "Temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}
ClientThread.java 文件源码 项目:LittleBitLouder 阅读 36 收藏 0 点赞 0 评论 0
public ClientThread(BluetoothDevice device, Handler handler, boolean secure) {
    BluetoothSocket tempSocket = null;
    this.handler = handler;
    this.secure = secure;
    this.device = device;

    try {
        if (secure)
            tempSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(Constants.UUID_STRING));
        else
            tempSocket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString(Constants.UUID_STRING));

    } catch (Exception e) {
        Log.e(TAG, e.toString());
    }
    this.socket = tempSocket;
}
BluetoothConnector.java 文件源码 项目:LittleBitLouder 阅读 42 收藏 0 点赞 0 评论 0
private boolean selectSocket() throws IOException {
    if (candidate >= uuidCandidates.size()) {
        return false;
    }

    BluetoothSocket tmp;
    UUID uuid = uuidCandidates.get(candidate++);

    Log.e("BT", "===> Attempting to connect to Protocol: " + uuid);
    if (secure) {
        tmp = device.createRfcommSocketToServiceRecord(uuid);
    } else {
        tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
    }
    bluetoothSocket = new NativeBluetoothSocket(tmp);

    return true;
}
BluetoothConnector.java 文件源码 项目:LittleBitLouder 阅读 46 收藏 0 点赞 0 评论 0
public FallbackBluetoothSocket(BluetoothSocket tmp, boolean isSecure) throws FallbackException {
    super(tmp);
    try {
        Class<?> clazz = tmp.getRemoteDevice().getClass();
        Class<?>[] paramTypes = new Class<?>[]{Integer.TYPE};

        String methodName = "createInsecureRfcommSocket";
        if (isSecure)
            methodName = "createRfcommSocket";

        Method m = clazz.getMethod(methodName, paramTypes);
        Object[] params = new Object[]{Integer.valueOf(1)};
        fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
    } catch (Exception e) {
        throw new FallbackException(e);
    }
}
BluetoothChatService.java 文件源码 项目:dab-iot 阅读 45 收藏 0 点赞 0 评论 0
public ConnectThread(BluetoothDevice device, boolean secure) {
    mmDevice = device;
    BluetoothSocket tmp = null;
    mSocketType = "Insecure";

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        tmp = device.createInsecureRfcommSocketToServiceRecord(
                    MY_UUID_INSECURE);

    } catch (IOException e) {
        Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
    }
    mmSocket = tmp;
    mState = STATE_CONNECTING;
}
BluetoothChatService.java 文件源码 项目:buildAPKsApps 阅读 38 收藏 0 点赞 0 评论 0
public void run() {
    if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
    setName("AcceptThread");
    BluetoothSocket socket = null;
    try {
        // Listen for all 7 UUIDs
        for (int i = 0; i < 7; i++) {
            serverSocket = mAdapter.listenUsingRfcommWithServiceRecord(NAME, mUuids.get(i));
            socket = serverSocket.accept();
            if (socket != null) {
                String address = socket.getRemoteDevice().getAddress();
             mSockets.add(socket);
             mDeviceAddresses.add(address);
             connected(socket, socket.getRemoteDevice());
            }                       
        }
    } catch (IOException e) {
        Log.e(TAG, "accept() failed", e);
    }
    if (D) Log.i(TAG, "END mAcceptThread");
}
BluetoothChatService.java 文件源码 项目:buildAPKsApps 阅读 38 收藏 0 点赞 0 评论 0
public ConnectedThread(BluetoothSocket socket) {
    Log.d(TAG, "create ConnectedThread");
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the BluetoothSocket input and output streams
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}
BLEDeviceConnector.java 文件源码 项目:xlight_android_native 阅读 40 收藏 0 点赞 0 评论 0
public ConnectedThread(BluetoothSocket socket) {
    Log.d(TAG, "create ConnectedThread");
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the BluetoothSocket input and output streams
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}
ReceptionConnectionsThread.java 文件源码 项目:pass_the_bomb 阅读 32 收藏 0 点赞 0 评论 0
/**
 * Set the socket to listen and accept incoming connections
 */
public void run() {
    BluetoothSocket socket = null;
    // Keep listening until exception occurs or a socket is returned
    while (true) {
        try {
            socket = mmServerSocket.accept();
        } catch (IOException e) {
            break;
        }

        // If a connection was accepted
        if (socket != null) {
            // Do work to manage the connection (in a separate thread)
            new MessagingThread(socket).start();
            System.out.println("Socket accepted");
        }
    }
    cancel();
}
ConnectionThread.java 文件源码 项目:pass_the_bomb 阅读 37 收藏 0 点赞 0 评论 0
/**
 * Creates the thread
 * @param playerToConnect player to connect to
 */
public ConnectionThread(Player playerToConnect) {
    //gets the device to connect to (the player's device)
    BluetoothDevice deviceToConnect = BluetoothServices.getmBluetoothAdapter().getRemoteDevice(playerToConnect.getMAC());
    System.out.println("device to connect to");
    System.out.println(deviceToConnect.getAddress());
    System.out.println(deviceToConnect.getName());
    System.out.println(deviceToConnect.toString());
    System.out.println("device to connect to");
    // 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 (that is a player)
    try {
        // MY_UUID is the app's UUID string, also used by the reception part of the code
        // creates and insecure RF socket
        tmp = deviceToConnect.createInsecureRfcommSocketToServiceRecord(MainActivity.uuid);
        System.out.println("Socket found!");

    } catch (IOException e) { }
    mmSocket = tmp;
}


问题


面经


文章

微信
公众号

扫码关注公众号