android.bluetooth.BluetoothSocket Java Examples

The following examples show how to use android.bluetooth.BluetoothSocket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BluetoothManager.java    From retrowatch with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: BluetoothClientService.java    From tilt-game-android with MIT License 6 votes vote down vote up
/**
 * Start the CommunicationThread 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, final String socketType) {
    if (_debug) Log.d(TAG, "connected: ");

    cancelConnectThread();
    cancelCommunicationThread(_communicationThread);

    _communicationThread = createCommunicationThread(socket, device);

    // Send the name of the connected device back to the UI Activity
    Bundle bundle = new Bundle();
    bundle.putString(ServiceMessageKeys.DEVICE_NAME, device.getName());
    bundle.putString(ServiceMessageKeys.DEVICE_ADDRESS, device.getAddress());
    sendMessage(ServiceMessageType.MESSAGE_DEVICE_CONNECTED, bundle);

    setState(STATE_CONNECTED);
}
 
Example #3
Source File: PBluetoothClient.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
    MLog.d(TAG, "connected");

    changeStatus(CONNECTED, device);
    mDevice = device;

    // Cancel the thread that completed the connection
    if (mConnectingThread != null) {
        mConnectingThread.cancel();
        mConnectingThread = null;
    }

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

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket, device);
    mConnectedThread.start();
}
 
Example #4
Source File: BluetoothService.java    From UnityBluetoothPlugin with Apache License 2.0 6 votes vote down vote up
public ConnectedThread(BluetoothSocket socket) {
    Log.d(TAG, "create ConnectedThread");
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}
 
Example #5
Source File: ConnectedThread.java    From SmartOrnament with Apache License 2.0 6 votes vote down vote up
public ConnectedThread(BluetoothSocket socket, Context context,Handler handler) {
    vibrator = (Vibrator) context.getSystemService(VIBRATOR_SERVICE);
    mSocket = socket;//被管理的Socket
    mHandler=handler;
    this.context=context;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;
    //无论是客户端的Socket还是服务端的Socket,都有输入输出流
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {  }
    mInStream = tmpIn;
    mOutStream = tmpOut;
    // 获得远程设备的输出缓存字符流,相当于找了一个大的管道,可以通向远程设备,发数据的必经管道
    mBw = new BufferedWriter(new PrintWriter(mOutStream));

    //mHandler=new Handler(Looper.getMainLooper());
}
 
Example #6
Source File: BTService.java    From esc-pos-android with Apache License 2.0 6 votes vote down vote up
private synchronized void onDeviceConnected(BluetoothSocket socket, BluetoothDevice device) {
  cancelAllThreads();

  listenThread = new ListenThread(socket);
  listenThread.start();

  listenThread = new ListenThread(socket);
  listenThread.start();

  if (deviceCallbacks != null) {
    mainHandler.post(new Runnable() {
      @Override
      public void run() {
        deviceCallbacks.onConnected();
      }
    });
  }
  Log.e(TAG, "Device " + device.getName() + " [" + device.getAddress() + "] — connected");
  this.setState(STATE_CONNECTED);
}
 
Example #7
Source File: ConnectedThread.java    From Android-Simple-Bluetooth-Example with MIT License 6 votes vote down vote up
public ConnectedThread(BluetoothSocket socket, Handler handler) {
    mmSocket = socket;
    mHandler = handler;
    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) { }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}
 
Example #8
Source File: BluetoothService.java    From Android-HC05-App with MIT License 6 votes vote down vote up
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;
}
 
Example #9
Source File: BluetoothManager.java    From retroband with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #10
Source File: BluetoothManager.java    From retrowatch with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #11
Source File: ClientThread.java    From BluetoothStudy with Apache License 2.0 5 votes vote down vote up
public ClientThread(BluetoothAdapter bluetoothAdapter, BluetoothDevice device,
                    Handler handler) {
    this.bluetoothAdapter = bluetoothAdapter;
    this.device = device;
    this.uiHandler = handler;
    BluetoothSocket tmp = null;
    try {
        tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(Params.UUID));
    } catch (IOException e) {
        e.printStackTrace();
    }
    socket = tmp;
}
 
Example #12
Source File: BluetoothWrapper.java    From phonegap-bluetooth-plugin with MIT License 5 votes vote down vote up
@Override
protected BluetoothSocket doInBackground(Void... params) 
{
	if(this._socket == null)
	{
		this._error = "Socket not created correctly.";
		return null;
	}
	
	try
	{
		_socket.connect();
		return _socket;
	}
	catch(IOException eConnect)
	{
		this._error = eConnect.getMessage();
		
		try
		{
			_socket.close();
		}
		catch(IOException eClose)
		{
			this._error += " " + eClose.getMessage();
			Log.e(LOG_TAG, "Failed to close socket. " + eClose.getMessage());
		}
		return null;
	}
}
 
Example #13
Source File: NTRIPService.java    From api-ntrip-java-client with MIT License 5 votes vote down vote up
public synchronized void BTconnected(BluetoothSocket socket, BluetoothDevice device, String autoconfigmodel) {
    //Log.i(BTAG, "Connected");
    // Cancel the thread that completed the connection
    if (mBTConnectThread != null) {mBTConnectThread.cancel(); mBTConnectThread = null;}
    // Cancel any thread currently running a connection
    if (mBTConnectedThread != null) {mBTConnectedThread.cancel(); mBTConnectedThread = null;}

    // Start the thread to manage the connection and perform transmissions
    mBTConnectedThread = new BTConnectedThread(socket, autoconfigmodel);
    mBTConnectedThread.start();

    setBTState(STATE_CONNECTED);
}
 
Example #14
Source File: MainActivity.java    From Arduino_BT_Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onPostExecute(BluetoothSocket result) {
	
	btSocket = result;
	//Enable Button
	btToggle.setEnabled(true);
	
}
 
Example #15
Source File: BCLServiceClient.java    From unity-bluetooth with MIT License 5 votes vote down vote up
protected ConnectThread(BluetoothDevice device) {
    BluetoothSocket tmp = null;
    mmDevice = device;

    try {
        tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(CHARACTERISTIC_UUID));
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
    mmSocket = tmp;
}
 
Example #16
Source File: Client_Fragment.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
    mmDevice = device;
    BluetoothSocket tmp = null;

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(MainActivity.MY_UUID);
    } catch (IOException e) {
        mkmsg("Client connection failed: " + e.getMessage() + "\n");
    }
    socket = tmp;

}
 
Example #17
Source File: PBluetoothServer.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public void run() {
    MLog.d(TAG, "bbt run " + mServerStarted);
    mServerStarted = true;
    MLog.d(TAG, "bbt run " + mServerStarted);

    while (mServerStarted) {
        try {
            BluetoothSocket serverSocket = mBluetoothServer.accept();
            MLog.d(TAG, "bbt accepting connection " + serverSocket.getRemoteDevice().getAddress());
            connectToClient(serverSocket); // since this is blocking, once we get a client we connect it
        } catch (IOException e) {
            MLog.d(TAG, "bbt BLUETOOTH error:" + e.getMessage());
        }
    }
}
 
Example #18
Source File: BluetoothHelper.java    From BluetoothHelper with GNU General Public License v3.0 5 votes vote down vote up
public ConnectedThreadClass_Read(BluetoothSocket socket) {
    mmSocket = socket;
    InputStream tmpIn = null;
    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = mmSocket.getInputStream();
    } catch (IOException e) {
    }
    mmInStream = tmpIn;
    isInStreamConnected = true;
}
 
Example #19
Source File: LinkDeviceFragment.java    From SmartOrnament with Apache License 2.0 5 votes vote down vote up
@Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
//                case TASK_RECV_MSG://接收到对方的蓝牙信息
//                    String rcv= (String) msg.obj;  // 获得远程设备发送的消息
//                    Toast.makeText(getContext(),rcv,Toast.LENGTH_SHORT).show();
//                    if (!mApplication.getPhoneState())
//                        handleReceivedBleMsg(rcv);
//                    break;
                case TASK_GET_SOCKET:
                    mApplication.setBluetoothSocket((BluetoothSocket) msg.obj);
                    mApplication.setHandler(mHandler);
//                    mangerThread= new ConnectedThread(app.getBluetoothSocket(),getContext(),app.getHandler());
//                    mangerThread.start();
                    iv_connect.setBackgroundResource(R.drawable.connect_success);
                   // mBtn_searchBle.setText("一切准备就绪");
                    mCpb_connect.setProgress(100);
                    mRequestThread=null;//置空连接线程
                    break;
                case TASK_GET_SOCKET_FAILED:
                   // iv_connect.setBackgroundResource(R.drawable.);
                    mCpb_connect.setProgress(-1);
                    mRequestThread=null;//置空连接线程,为重新请求连接做准备
                    Toast.makeText(getActivity(), "连接失败,请重试!", Toast.LENGTH_SHORT).show();
                    break;
                case TASK_DISCONNECT:
                    mCpb_connect.setProgress(0);
                    break;
            }
        }
 
Example #20
Source File: BluetoothService.java    From UnityBluetoothPlugin with Apache License 2.0 5 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
    this.mmDevice = device;
    BluetoothSocket tmp = null;

    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        Log.e(TAG, "create() failed", e);
    }
    mmSocket = tmp;
}
 
Example #21
Source File: BluetoothService.java    From UnityBluetoothPlugin with Apache License 2.0 5 votes vote down vote up
@RequiresPermission("android.permission.BLUETOOTH")
public synchronized void connected(BluetoothSocket socket,
                                   BluetoothDevice device) {
    Log.d(TAG, "connected");

    // Cancel the thread that completed the connection
    if (this.mConnectThread != null) {
        this.mConnectThread.cancel();
        this.mConnectThread = null;
    }

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

    // Cancel
    if(this.mAcceptThread != null) {
        this.mAcceptThread.cancel();
        this.mAcceptThread = null;
    }

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new BluetoothService.ConnectedThread(socket);
    mConnectedThread.start();
    Message msg = this.mHandler.obtainMessage(BluetoothPlugin.MESSAGE_DEVICE_NAME);
    Bundle bundle = new Bundle();
    bundle.putString(DEVICE_NAME, device.getName());
    msg.setData(bundle);
    this.mHandler.sendMessage(msg);
    setState(STATE_CONNECTED);
}
 
Example #22
Source File: SyncStreamingService.java    From boogie-board-sync-sdk-android with MIT License 5 votes vote down vote up
/**
 * Start the ConnectedThread to begin managing a Bluetooth connection
 *
 * @param socket The BluetoothSocket on which the connection was made
 */
private synchronized void connected(BluetoothSocket socket) {
    if (DEBUG) Log.d(TAG, "connected");

    // 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 listening thread if there is no one already running.
    if (mAcceptThread == null) {
        mAcceptThread = new AcceptThread();
        mAcceptThread.start();
    }

    // Start the thread to manage the connection and perform transmissions.
    mConnectedThread = new ConnectedThread(socket);
    mConnectedThread.start();

    startBluetoothHack();

    updateDeviceState(STATE_CONNECTED);
}
 
Example #23
Source File: BtCommService.java    From AndrOBD with GNU General Public License v3.0 5 votes vote down vote up
BtConnectThread(BluetoothDevice device, boolean secure)
{
	mmDevice = device;
	BluetoothSocket tmp = null;
	mSocketType = secure ? "Secure" : "Insecure";

	// Modified to work with SPP Devices
	final UUID SPP_UUID = UUID
		.fromString("00001101-0000-1000-8000-00805F9B34FB");
	
	// Get a BluetoothSocket for a connection with the
	// given BluetoothDevice
	try
	{
		if (secure)
		{
			tmp = device.createRfcommSocketToServiceRecord(SPP_UUID);
		} else
		{
			tmp = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
		}
	} catch (IOException e)
	{
		log.log(Level.SEVERE, "Socket Type: " + mSocketType + "create() failed", e);
	}
	mmSocket = tmp;

	logSocketUuids(mmSocket, "BT socket");
}
 
Example #24
Source File: BluetoothConnectionManager.java    From mytracks with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the ConnectedThread to read data.
 * 
 * @param bluetoothSocket the bluetooth socket
 * @param bluetoothDevice the bluetooth device
 */
private synchronized void connected(
    BluetoothSocket bluetoothSocket, BluetoothDevice bluetoothDevice) {
  cancelThreads();

  connectedThread = new ConnectedThread(bluetoothSocket);
  connectedThread.start();

  // Send the device name to the handler
  Message message = handler.obtainMessage(MESSAGE_DEVICE_NAME);
  Bundle bundle = new Bundle();
  bundle.putString(KEY_DEVICE_NAME, bluetoothDevice.getName());
  message.setData(bundle);
  handler.sendMessage(message);

  setState(Sensor.SensorState.CONNECTED);
}
 
Example #25
Source File: BluetoothManager.java    From retroband with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 */
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
	Log.d(TAG, "connected");

    // 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;}

    // Cancel the accept thread because we only want to connect to one device
    if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;}

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket);
    mConnectedThread.start();

    // Send the name of the connected device back to the UI Activity
    Message msg = mHandler.obtainMessage(MESSAGE_DEVICE_NAME);
    Bundle bundle = new Bundle();
    bundle.putString(SERVICE_HANDLER_MSG_KEY_DEVICE_ADDRESS, device.getAddress());
    bundle.putString(SERVICE_HANDLER_MSG_KEY_DEVICE_NAME, device.getName());
    msg.setData(bundle);
    mHandler.sendMessage(msg);

    setState(STATE_CONNECTED);
}
 
Example #26
Source File: BluetoothManager.java    From retrowatch with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 */
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
	Logs.d(TAG, "connected");

    // 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;}

    // Cancel the accept thread because we only want to connect to one device
    if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;}

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket);
    mConnectedThread.start();

    // Send the name of the connected device back to the UI Activity
    Message msg = mHandler.obtainMessage(MESSAGE_DEVICE_NAME);
    Bundle bundle = new Bundle();
    bundle.putString(Constants.SERVICE_HANDLER_MSG_KEY_DEVICE_ADDRESS, device.getAddress());
    bundle.putString(Constants.SERVICE_HANDLER_MSG_KEY_DEVICE_NAME, device.getName());
    msg.setData(bundle);
    mHandler.sendMessage(msg);

    setState(STATE_CONNECTED);
}
 
Example #27
Source File: BluetoothManager.java    From BTChat with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #28
Source File: BluetoothServer.java    From BluetoothChatting with Apache License 2.0 5 votes vote down vote up
@Override
        public void run() {

            //只要当前状态不等连接状态
            while (mBluetoothServer != null && (mBluetoothServer.getCurrentState() == BluetoothState.LISTEN == true)) {
                try {
//                    Log.i("Infoss", "开始监听 state:" + mBluetoothServer.getCurrentState() + "  thread:" + Thread.currentThread().getName());
                    mBluetoothServerSocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(name, BluetoothServer.UUID_SECURE);
//                    Log.i("Infoss", "等待连接...");
                    BluetoothSocket mBluetoothSocket;
                    mBluetoothSocket = mBluetoothServerSocket.accept(); //Thread come to block
                    if (mBluetoothSocket != null) {

                        mBluetoothServer.connected(mBluetoothSocket);

                    }
                } catch (Exception e) {
//                    Log.i("Infoss", "接受失败.." + e);
                    e.printStackTrace();

                } finally {
                    try {
//                        Log.i("Infoss", "关闭了... state:" + mBluetoothServer.getCurrentState());
                        if (mBluetoothServerSocket != null) {
                            mBluetoothServerSocket.close();

                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
//                        Log.i("Infoss", "失败...");
                        break;
                    }
                }

            }
        }
 
Example #29
Source File: AbstractBluetoothService.java    From tilt-game-android with MIT License 5 votes vote down vote up
protected BluetoothCommunicationThread createCommunicationThread(BluetoothSocket socket, final BluetoothDevice device) {
    BluetoothCommunicationThread communicationThread = new BluetoothCommunicationThread(socket, device.getAddress(), _handler, _debug);
    communicationThread.setDebug(_debug);
    communicationThread.setConnectionLostListener(new BluetoothCommunicationThread.ConnectionLostListener() {
        @Override
        public void onConnectionLost() {
            connectionLost(device);
        }
    });
    communicationThread.start();

    return communicationThread;
}
 
Example #30
Source File: SerialIOThread.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public SerialIOThread(BluetoothSocket rfcommSocket, MessageHashTableBase hashTable) {
    super();
    this.hashTable = hashTable;

    mRfCommSocket = rfcommSocket;
    try {
        mOutputStream = mRfCommSocket.getOutputStream();
        mInputStream = mRfCommSocket.getInputStream();
    } catch (IOException e) {
        log.error("Unhandled exception", e);
    }
    this.start();
}