douglasjunior / AndroidBluetoothLibrary

A Library for easy implementation of Serial Bluetooth Classic and Low Energy on Android. 💙
MIT License
224 stars 68 forks source link

can't start scan #30

Closed XXXXzzzz000 closed 1 year ago

XXXXzzzz000 commented 5 years ago

when i start scan it will stop immediately.

    private fun initializeBluetoothService() {
        val config = BluetoothConfiguration()
        config.context = applicationContext
        config.bluetoothServiceClass = BluetoothClassicService::class.java // BluetoothClassicService.class or BluetoothLeService.class
        config.bufferSize = 1024
        config.characterDelimiter = '\n'
        config.deviceName = "Test"
        config.callListenersInMainThread = true

// Bluetooth Classic
        config.uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb") // Required\

        BluetoothService.init(config)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        //init bt
        initializeBluetoothService()
        mservice = BluetoothService.getDefaultInstance()

        //start scan
        mservice!!.setOnScanCallback(object : BluetoothService.OnBluetoothScanCallback {
            override fun onDeviceDiscovered(device: BluetoothDevice, rssi: Int) {
                if(device.name=="test_device")
                {
                    showToast("connect ${device.name},${device.uuids}");
                    bt_device=device;
                    mservice!!.connect(device) // See also service.disconnect();
                }
                else
                {
                    showToast(device.name);
                }
            }

            override fun onStartScan() {
                showToast("start scan");
            }

            override fun onStopScan() {
                showToast("stop scan");
            }
        })

        //connect callback
        mservice!!.setOnEventCallback(this)

        BtConnectBT.setOnClickListener {
            mservice!!.startScan() // See also service.stopScan();
        }

    }

    override fun onResume() {
        super.onResume()
        mservice!!.setOnEventCallback(this)
    }

    override fun onDataRead(p0: ByteArray?, p1: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onStatusChange(p0: BluetoothStatus?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onDataWrite(p0: ByteArray?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onToast(p0: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onDeviceName(p0: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

it work should like : button down->start scan->find device name "test device"->connect

but when i press button ,i will see start scan and stop scan immediately

douglasjunior commented 5 years ago

What is the device model and android version? Did you check if bluetooth and location service are enable?

XXXXzzzz000 commented 5 years ago

now

config.uuid = null

and it will start ,but can't scan device. the callback

            override fun onDeviceDiscovered(device: BluetoothDevice, rssi: Int) {
                if(device.name=="test_device")
                {
                    showToast("connect ${device.name},${device.uuids}");
                    bt_device=device;
                    mservice!!.connect(device) // See also service.disconnect();
                }
                else
                {
                    showToast(device.name);
                }
            }

wasn't invoke

XXXXzzzz000 commented 5 years ago

device: one plus 3t android version :android 8.0

embedded device : esp32

bluetooth is enable . location service should be add into manifest? or i need open location service?

douglasjunior commented 5 years ago

config.uuid is required for the classic mode.

You need to make sure if the location service is enable: know issue.

XXXXzzzz000 commented 5 years ago

Now I solved these problems .

  1. the classic bt uuid https://developer.android.com/guide/topics/connectivity/bluetooth#QueryingPairedDevices and it said :
    The UUID passed here must match the UUID that the server device uses to open its BluetoothServerSocket with listenUsingRfcommWithServiceRecord(String, UUID) . To use the same UUID, simply encode the UUID string into the application and then reference the string through the server code and client code.
  2. in android 6.0 i also need open the location service
  3. i need open bluetooth and location by myself mybe it should be a part of example?

and thanks for your help! @douglasjunior

XXXXzzzz000 commented 1 year ago

您的邮件我已收到。                                                             宣泽