There is one very serious problem. My application should recognize when the connection is broken (so far only due to the fact that the user knocked out bluetooth).

I did this:

This is a separate stream (to check if the bluetooth works every 5 seconds).

@Override public void run() { while(true) { try { newCheck = new BluetoothCheck(bluetooth); if (!newCheck.isBluetoothEnabled()) textView.setText("Отсутствует подключение..."); else textView.setText("Подключение установлено"); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } 

And here the necessary module is initialized (in my case, hc-06) and starts the stream I need.

 public void tryConnectToWatch() { if (bluetooth.isEnabled()) { try { BluetoothDevice device = bluetooth.getRemoteDevice("20:16:08:16:14:57"); Method m = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class}); clientSocket = (BluetoothSocket) m.invoke(device, 1); clientSocket.connect(); Toast.makeText(getApplicationContext(), "Браслет подключен успешно.", Toast.LENGTH_LONG).show(); Thread thread = new Thread(this); thread.start(); } } } 

There are no syntax errors, but nothing happens either. It puts me once - the connection is established and everything, then every 5 seconds there is no verification.

The BluetoothCheck class was written by myself and yes, I know that it is not needed, I created it after the last attempt. Here is the code:

 class BluetoothCheck{ BluetoothAdapter bluetoothAdapter; public BluetoothCheck(BluetoothAdapter bluetoothAdapter) { this.bluetoothAdapter = bluetoothAdapter; } public boolean isBluetoothEnabled() { if (bluetoothAdapter.isEnabled()) { return true; } else {return false;} } 

}

    1 answer 1

    Worked with the Bluetooth module HC-06 connected to the Arduino, while using the public createRfcommSocketToServiceRecord method. The resulting BluetoothSocket correctly threw out an IOException in the event that the Bluetooth module was turned off.

    As an example, below is part of the service code for work with bluetooth, written in Kotlin. Disassemble what is happening is easy, if you do not pay attention to the question marks and exclamation marks (the use block is an analogue of try-with-resources).

    After connection, we start the btEventThread stream, which constantly tries to read something from the socket with a blocking call to reader.readLine() , and then process the received data as you like. In the event of a disconnection (including when you turn off the Bluetooth module of the phone module or Arduino), just handle an IOException .

    Bonus: for transferring the data received from the socket for further processing, as well as for notifying the state of the service, used EventBus .

     class BtService : Service() { private val sppUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private var bluetoothSocket: BluetoothSocket? = null; fun connect(deviceAddress: String) { val device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress) if (bluetoothSocket == null || !bluetoothSocket!!.isConnected) { bluetoothSocket = device.createRfcommSocketToServiceRecord(sppUuid) try { if (!bluetoothSocket!!.isConnected) { bluetoothSocket!!.connect() } btEventThread.start() //TODO: Notify socket connected } catch(e: Exception) { e.printStackTrace() //TODO: Report error } } else { //TODO: Notify already connected } } private val btEventThread = object: Thread() { override fun run() { if (bluetoothSocket != null) { try { bluetoothSocket!!.inputStream.bufferedReader().use { reader -> while (!isInterrupted) { val message = reader.readLine() //TODO: Do something with it } } } catch(e: IOException) { e.printStackTrace() stopSelf() //TODO: Notify that server has stopped } } } } override fun onDestroy() { btEventThread.interrupt() bluetoothSocket?.close() } }