2

I'm a little bit desperated. I develope an android app, which is connectable to an Adafruit Bluefruit.I watched a lot of tutorials and try out many thinks. With the nRF Connect Application I can connect smartphone and the bluefruit, but with my own app it's not possible. I don't understand why an don't find a solution.

This is the part of the code for connecting:

  private class ConnectToDevice(c: Context) : AsyncTask<Void, Void, String>() {
    //create member variables
    private var connectSuccess: Boolean = true
    private val context: Context

    //create an constructor
    init {
        this.context = c
    }

    override fun onPreExecute() {
        super.onPreExecute()
        m_progress = ProgressDialog.show(context, "Connecting...", "please wait")
    }
    override fun doInBackground(vararg p0: Void?): String? {
        try {
            if (m_bluetoothSocket == null || !m_isConnected){
                m_bluetoothAdapter= BluetoothAdapter.getDefaultAdapter()
                //create bluetooth device, by mac-address
                val device: BluetoothDevice = m_bluetoothAdapter.getRemoteDevice(m_address)
                //sets up connection between phone and bluetooth device
                m_bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(m_myUUID)
                //stopps looking for devices
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery()
                //connects bluetooth device
                m_bluetoothSocket!!.connect()
            }
        } catch (e: IOException){
            connectSuccess = false
            e.printStackTrace()
        }
        return null
    }

The code is based on this great tutorial: https://github.com/appsinthesky/Kotlin-Bluetooth

I get the following Logcat:

W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
...
W/System.err: ControlActivity$ConnectToDevice.doInBackground(ControlActivity.kt:104)
W/System.err: at ControlActivity$ConnectToDevice.doInBackground(ControlActivity.kt:79)
...
I/data: couldn't connect

I hope, that anybody could help me! Thanks a lot in advance!

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bluetoothle">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher_round"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SelectDeviceActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ControlActivity"
        android:screenOrientation="portrait"/>
</application>

Rike
  • 21
  • 3

0 Answers0