Open stg-andrei opened 4 years ago
Hi. I am facing the same problem in my application. With older android versions works fine, but when my app is instaled in a Android 10 device the bluetooth search don't work. I tested this with the Printooth sample app and the same problem happened. Debbuging the project I noticed that the function bluetoothAdapter.startDiscovery(); is not return a True value. I debbuged the Printooth source code but I couldn't see what is causing the trobble.
Bluetooth.java:
public void startScanning() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); context.registerReceiver(scanReceiver, filter); bluetoothAdapter.startDiscovery(); }
(P.S. If I search the devices in the smartphone bluetooth screen, then the devices appears in the app list) .
I think there is problem with DiscoveryCallback. This is my discoveryCallback function in kotlin :
fun discoveryCallback() {
bluetooth.setDiscoveryCallback(object : DiscoveryCallback {
override fun onDiscoveryStarted() {
Log.d("Bluetooth","Discovery started")
var pairedPrinter = PairedPrinter.getPairedPrinter()
devices.clear() // array of devices
printers.clear() // array of printers, recycler view
for ( (index, device) in bluetooth.pairedDevices.withIndex() ){
var printer = Printer()
printer.name = device.name
printer.address = device.address
if (pairedPrinter!= null){
printer.status = pairedPrinter.address == printer.address
}
printers.add(index, printer)
Log.d("printer", printer.name) // log printer's name
}
adapter.notifyDataSetChanged()
}
override fun onDiscoveryFinished() {
Log.d("Bluetooth","Discovery ended")
}
override fun onDeviceFound(device: BluetoothDevice) {
Log.d("Bluetooth", "Device found" + device.address)
}
override fun onDevicePaired(device: BluetoothDevice) {
Log.d("Bluetooth", "Device Paired " + device.address)
adapter.notifyDataSetChanged()
}
override fun onDeviceUnpaired(device: BluetoothDevice) {
Log.d("Bluetooth", "Device unpaired " + device.address)
adapter.notifyDataSetChanged()
}
override fun onError(message: String) {
Toast.makeText(context, "Maaf! Terjadi error.", Toast.LENGTH_SHORT)
}
})
}
It's working just fine and able to print paired device's names in Pie (real device, Mi 6X), but got a problem with android 10 (real device, Redmi 2 Prime, LineageOS), unable to log anything on discovery callback.
(P.S. I'm using version 1.2.2)
I actually solved my problem with android 10. For that i had to ask for permissions in run-time and ask for LOCATION_SERVICE. ` if((ContextCompat.checkSelfPermission(getApplicationContext() ,Manifest.permission.ACCESS_FINE_LOCATION)) == PackageManager.PERMISSION_GRANTED){ // Function that do some stuff } else { ActivityCompat.requestPermissions(Activity.this, new String[] {ACCESS_FINE_LOCATION,Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN,Manifest.permission.WRITE_EXTERNAL_STORAGE},1); }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!lm.isLocationEnabled()) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}`
This is a snipet of what I had to do in order to the device search work properly. This was made in my app, in the function that use the printer.
Hello, I am trying to connect to my DPP-250 http://www.datecs.bg/en/products/61 But when I try to pair with the printer I get Scanning and it never stops + if I hit back I get an error: I tried to debug this for hours without succes sadly
This is my fragment:
I call Printooth.init(this)
In my mainActivity as such: